Skip to content

Instantly share code, notes, and snippets.

Created June 2, 2015 00:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8a940db2772cdb9e85a3 to your computer and use it in GitHub Desktop.
Save anonymous/8a940db2772cdb9e85a3 to your computer and use it in GitHub Desktop.
defmodule Saturn.Logger do
use Timex
require Logger
alias Plug.Conn
@behaviour Plug
def init(opts) do
Keyword.get(opts, :log, :info)
end
def call(conn, level) do
time = Date.now |> Date.universal |> DateFormat.format! "{ISOz}"
before_time = :erlang.now
Logger.metadata(request_id: request_id)
conn
|> Conn.register_before_send(fn conn ->
Logger.log level, fn ->
after_time = :erlang.now
diff = :timer.now_diff(after_time, before_time)
Logfmt.encode [time: time,
method: conn.method,
path: Conn.full_path(conn),
status: conn.status,
elapsed: "#{diff / 1000}ms"]
end
conn
end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment