Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions lib/plug/logger_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ defmodule Plug.LoggerJSON do
}
```

To use it, just plug it into the desired module.
To use it, set json_encoder parameter in your config:

```
config :plug_logger_json, json_encoder: Jason
```

and plug it into the desired module:
plug Plug.LoggerJSON, log: :debug
## Options
* `:log` - The log level at which this plug should log its request info.
Expand Down Expand Up @@ -105,12 +111,11 @@ defmodule Plug.LoggerJSON do
def log_error(kind, reason, stacktrace) do
_ =
Logger.log(:error, fn ->
%{
encode!(%{
"log_type" => "error",
"message" => Exception.format(kind, reason, stacktrace),
"request_id" => Logger.metadata()[:request_id]
}
|> Poison.encode!()
})
end)
end

Expand All @@ -122,7 +127,7 @@ defmodule Plug.LoggerJSON do
|> Map.merge(debug_logging(conn, opts))
|> Map.merge(phoenix_attributes(conn))
|> Map.merge(extra_attributes(conn, opts))
|> Poison.encode!()
|> encode!()
end)
end

Expand Down Expand Up @@ -254,4 +259,9 @@ defmodule Plug.LoggerJSON do
num = Integer.to_string(val)
:binary.copy("0", count - byte_size(num)) <> num
end

def encode!(value) do
encoder = Application.get_env(:plug_logger_json, :json_encoder)
encoder.encode!(value)
end
end
10 changes: 8 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ defmodule PlugLoggerJson.Mixfile do
homepage_url: "https://github.com/bleacherreport/plug_logger_json",
name: "Plug Logger JSON",
package: package(),
preferred_cli_env: [coveralls: :test, "coveralls.detail": :test, "coveralls.post": :test, "coveralls.html": :test],
preferred_cli_env: [
coveralls: :test,
"coveralls.detail": :test,
"coveralls.post": :test,
"coveralls.html": :test
],
source_url: "https://github.com/bleacherreport/plug_logger_json",
start_permanent: Mix.env() == :prod,
test_coverage: [tool: ExCoveralls],
Expand All @@ -35,7 +40,8 @@ defmodule PlugLoggerJson.Mixfile do
{:ex_doc, "~> 0.19.3", only: [:dev]},
{:excoveralls, "~> 0.10.5", only: [:test]},
{:plug, "~> 1.0"},
{:poison, "~> 1.5 or ~> 2.0 or ~> 3.0 or ~> 4.0"}
{:poison, "~> 1.5 or ~> 2.0 or ~> 3.0 or ~> 4.0", optional: true},
{:jason, "~> 1.1", optional: true}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"},
}

4 changes: 4 additions & 0 deletions test/plug/logger_json_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ defmodule Plug.LoggerJSONTest do
{Process.get(:get_log), data}
end

setup do
Application.put_env(:plug_logger_json, :json_encoder, Jason)
end

test "correct output - no params or headers" do
{_conn, message} =
conn(:get, "/")
Expand Down