Skip to content

Commit e716ddf

Browse files
authored
Allow to configure whether all errors should be logged (#68)
1 parent cafad51 commit e716ddf

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

lib/plug/cowboy.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ defmodule Plug.Cowboy do
9898
- Your app is running in production without a reverse proxy, using Cowboy's
9999
SSL support.
100100
101+
## Logging
102+
103+
You can configure which exceptions are logged via `:log_exceptions_with_status_code`
104+
application environment variable. If the status code returned by `Plug.Exception.status/1`
105+
for the exception falls into any of the configured ranges, the exception is logged.
106+
By default it's set to `[500..599]`.
107+
108+
config :plug_cowboy,
109+
log_exceptions_with_status_code: [400..599]
110+
101111
## Instrumentation
102112
103113
Plug.Cowboy uses the `:telemetry` library for instrumentation. The following

lib/plug/cowboy/translator.ex

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ defmodule Plug.Cowboy.Translator do
2828
{reason, {mod, :call, [%Plug.Conn{} = conn, _opts]}},
2929
_stack
3030
) do
31-
if non_500_exception?(reason) do
32-
:skip
33-
else
31+
if log_exception?(reason) do
3432
{:ok,
3533
[
3634
inspect(pid),
@@ -41,6 +39,8 @@ defmodule Plug.Cowboy.Translator do
4139
conn_info(min_level, conn)
4240
| Exception.format(:exit, reason, [])
4341
], conn: conn, crash_reason: reason, domain: [:cowboy]}
42+
else
43+
:skip
4444
end
4545
end
4646

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

60-
defp non_500_exception?({%{__exception__: true} = exception, _}) do
61-
Plug.Exception.status(exception) < 500
60+
defp log_exception?({%{__exception__: true} = exception, _}) do
61+
status_ranges =
62+
Application.get_env(:plug_cowboy, :log_exceptions_with_status_code, [500..599])
63+
64+
status = Plug.Exception.status(exception)
65+
66+
Enum.any?(status_ranges, &(status in &1))
6267
end
6368

64-
defp non_500_exception?(_), do: false
69+
defp log_exception?(_), do: true
6570

6671
defp conn_info(_min_level, conn) do
6772
[server_info(conn), request_info(conn)]

test/plug/cowboy/translator_test.exs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,37 @@ defmodule Plug.Cowboy.TranslatorTest do
5656
refute output =~ "** (exit) an exception was raised:"
5757
end
5858

59+
test "ranch/cowboy logs configured statuses" do
60+
Application.put_env(:plug_cowboy, :log_exceptions_with_status_code, [400..499])
61+
on_exit(fn -> Application.delete_env(:plug_cowboy, :log_exceptions_with_status_code) end)
62+
63+
{:ok, _pid} = Plug.Cowboy.http(__MODULE__, [], port: 9002)
64+
65+
output =
66+
capture_log(fn ->
67+
:hackney.get("http://127.0.0.1:9002/warn", [], "", [])
68+
Plug.Cowboy.shutdown(__MODULE__.HTTP)
69+
end)
70+
71+
assert output =~ ~r"#PID<0\.\d+\.0> running Plug\.Cowboy\.TranslatorTest \(.*\) terminated"
72+
assert output =~ "Server: 127.0.0.1:9002 (http)"
73+
assert output =~ "Request: GET /"
74+
assert output =~ "** (exit) an exception was raised:"
75+
assert output =~ "** (Plug.Parsers.UnsupportedMediaTypeError) unsupported media type foo/bar"
76+
77+
output =
78+
capture_log(fn ->
79+
:hackney.get("http://127.0.0.1:9002/error", [], "", [])
80+
Plug.Cowboy.shutdown(__MODULE__.HTTP)
81+
end)
82+
83+
refute output =~ ~r"#PID<0\.\d+\.0> running Plug\.Cowboy\.TranslatorTest \(.*\) terminated"
84+
refute output =~ "Server: 127.0.0.1:9001 (http)"
85+
refute output =~ "Request: GET /"
86+
refute output =~ "** (exit) an exception was raised:"
87+
refute output =~ "** (RuntimeError) oops"
88+
end
89+
5990
test "ranch/cowboy linked logs" do
6091
{:ok, _pid} = Plug.Cowboy.http(__MODULE__, [], port: 9003)
6192

0 commit comments

Comments
 (0)