Plug for writing access logs.


To use the plug in your projects, edit your mix.exs file and add the project as a dependency:

defp deps do
  [ { :plug_accesslog, "~> 0.1" } ]

You should also update your applications to start the plug:

def application do
  [ applications: [ :plug_accesslog ] ]


The easiest way to use the plug is to add it to your existing router:

defmodule AppRouter do
  use Plug.Router

  plug Plug.AccessLog,
    format: :clf,
    file: "/path/to/your/logs/access.log"

  plug :match
  plug :dispatch

  get "/hello" do
    send_resp(conn, 200, "world")

  match _ do
    send_resp(conn, 404, "oops")

Log Format

The default format is CLF.

Available formats

Besides a self defined format you can use one of the predefined aliases:

> %{User-Agent}i
> curl/7.35.0

> %h %l %u %t "%r" %>s %b
> - - [10/Jan/2015:14:46:18 +0100] "GET / HTTP/1.1" 200 31337

> %v %h %l %u %t "%r" %>s %b
> - - [10/Jan/2015:14:46:18 +0100] "GET / HTTP/1.1" 200 31337

> %h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"
> - - [22/Jan/2015:19:33:58 +0100] "GET / HTTP/1.1" 200 2 "" "curl/7.35.0"

> %v %h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"
> - - [22/Jan/2015:19:33:58 +0100] "GET / HTTP/1.1" 200 2 "" "curl/7.35.0"

> %{Referer}i -> %U
> -> /

Formatting directives

The following formatting directives are available:

Note for %B and %b: To determine the size of the response the “Content-Length” (exact case match required for now!) will be inspected and, if available, returned unverified. If the header is not present the response body will be inspected using byte_size/1.

Note for %h: The hostname will always be the ip of the client.

Note for %l: Always a dash (“-“).

Note for %r: For now the http version is always logged as “HTTP/1.1”, regardless of the true http version.


Apache License, Version 2.0