Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add metadata to log #6

Merged
merged 1 commit into from
Jul 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/logster/plugs/logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ defmodule Logster.Plugs.Logger do
|> Keyword.put(:status, conn.status)
|> Keyword.put(:duration, formatted_duration(duration))
|> Keyword.put(:state, conn.state)
|> Keyword.merge(Logger.metadata())
|> formatter.format
end
conn
Expand Down
22 changes: 22 additions & 0 deletions test/logster/plugs/logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ defmodule Logster.Plugs.LoggerTest do
end
end

defmodule MyCustomLogMetadata do
use Plug.Builder
plug Logster.Plugs.Logger
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
pass: ["*/*"],
json_decoder: Poison
plug :passthrough

defp passthrough(conn, _) do
Logger.metadata(%{custom_metadata: "OK"})
Plug.Conn.send_resp(conn, 200, "Passthrough")
end
end

defp capture_log(fun) do
data = capture_io(:user, fn ->
Process.put(:capture_log, fun.())
Expand Down Expand Up @@ -158,4 +173,11 @@ defmodule Logster.Plugs.LoggerTest do
%{"duration" => duration} = decoded
assert is_float(duration)
end

test "dump metadata into logs" do
{_conn, message} = capture_log fn ->
conn(:get, "/good") |> MyCustomLogMetadata.call([])
end
assert message =~ "custom_metadata=OK"
end
end