Skip to content

Commit

Permalink
Allow to configure whether all errors should be logged (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkgil authored Apr 22, 2021
1 parent cafad51 commit e716ddf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
10 changes: 10 additions & 0 deletions lib/plug/cowboy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ defmodule Plug.Cowboy do
- Your app is running in production without a reverse proxy, using Cowboy's
SSL support.
## Logging
You can configure which exceptions are logged via `:log_exceptions_with_status_code`
application environment variable. If the status code returned by `Plug.Exception.status/1`
for the exception falls into any of the configured ranges, the exception is logged.
By default it's set to `[500..599]`.
config :plug_cowboy,
log_exceptions_with_status_code: [400..599]
## Instrumentation
Plug.Cowboy uses the `:telemetry` library for instrumentation. The following
Expand Down
17 changes: 11 additions & 6 deletions lib/plug/cowboy/translator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ defmodule Plug.Cowboy.Translator do
{reason, {mod, :call, [%Plug.Conn{} = conn, _opts]}},
_stack
) do
if non_500_exception?(reason) do
:skip
else
if log_exception?(reason) do
{:ok,
[
inspect(pid),
Expand All @@ -41,6 +39,8 @@ defmodule Plug.Cowboy.Translator do
conn_info(min_level, conn)
| Exception.format(:exit, reason, [])
], conn: conn, crash_reason: reason, domain: [:cowboy]}
else
:skip
end
end

Expand All @@ -57,11 +57,16 @@ defmodule Plug.Cowboy.Translator do
], crash_reason: reason, domain: [:cowboy]}
end

defp non_500_exception?({%{__exception__: true} = exception, _}) do
Plug.Exception.status(exception) < 500
defp log_exception?({%{__exception__: true} = exception, _}) do
status_ranges =
Application.get_env(:plug_cowboy, :log_exceptions_with_status_code, [500..599])

status = Plug.Exception.status(exception)

Enum.any?(status_ranges, &(status in &1))
end

defp non_500_exception?(_), do: false
defp log_exception?(_), do: true

defp conn_info(_min_level, conn) do
[server_info(conn), request_info(conn)]
Expand Down
31 changes: 31 additions & 0 deletions test/plug/cowboy/translator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,37 @@ defmodule Plug.Cowboy.TranslatorTest do
refute output =~ "** (exit) an exception was raised:"
end

test "ranch/cowboy logs configured statuses" do
Application.put_env(:plug_cowboy, :log_exceptions_with_status_code, [400..499])
on_exit(fn -> Application.delete_env(:plug_cowboy, :log_exceptions_with_status_code) end)

{:ok, _pid} = Plug.Cowboy.http(__MODULE__, [], port: 9002)

output =
capture_log(fn ->
:hackney.get("http://127.0.0.1:9002/warn", [], "", [])
Plug.Cowboy.shutdown(__MODULE__.HTTP)
end)

assert output =~ ~r"#PID<0\.\d+\.0> running Plug\.Cowboy\.TranslatorTest \(.*\) terminated"
assert output =~ "Server: 127.0.0.1:9002 (http)"
assert output =~ "Request: GET /"
assert output =~ "** (exit) an exception was raised:"
assert output =~ "** (Plug.Parsers.UnsupportedMediaTypeError) unsupported media type foo/bar"

output =
capture_log(fn ->
:hackney.get("http://127.0.0.1:9002/error", [], "", [])
Plug.Cowboy.shutdown(__MODULE__.HTTP)
end)

refute output =~ ~r"#PID<0\.\d+\.0> running Plug\.Cowboy\.TranslatorTest \(.*\) terminated"
refute output =~ "Server: 127.0.0.1:9001 (http)"
refute output =~ "Request: GET /"
refute output =~ "** (exit) an exception was raised:"
refute output =~ "** (RuntimeError) oops"
end

test "ranch/cowboy linked logs" do
{:ok, _pid} = Plug.Cowboy.http(__MODULE__, [], port: 9003)

Expand Down

0 comments on commit e716ddf

Please sign in to comment.