Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ end
Once that's done you can replace `Plug.Logger` with `Plug.QuietLogger` in your
`endpoint.ex` file and you're ready to go.

If you need to customize the request path you want to suppress logging for, you
If you need to customize the request path you want to suppress logging for, you
can pass it with the `plug` call:

```elixir
plug Plug.QuietLogger, path: "/api/status"
```

You can also pass a list of paths to filter out, like:

```elixir
plug Plug.QuietLogger, path: [ "/api/status", "/api/readiness" ]
```

If you want to change your logging level you can also set it the same way you
would with `Plug.Logger`:

Expand Down
6 changes: 5 additions & 1 deletion lib/plug/quiet_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ defmodule Plug.QuietLogger do
%{log: log, path: path}
end

def call(%{request_path: path} = conn, %{log: log, path: paths}) when is_list(paths) do
if path in paths, do: conn, else: Plug.Logger.call(conn, log)
end

def call(%{request_path: path} = conn, %{log: :info, path: path}), do: conn

def call(conn, %{log: log}) do
def call(conn, %{log: log}) do
Plug.Logger.call(conn, log)
end
end
33 changes: 33 additions & 0 deletions test/plug/quiet_logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ defmodule Plug.QuietLoggerTest do
end
end


defmodule MyMultiPathPlug do
use Plug.Builder

plug Plug.QuietLogger, path: ["/api/status", "/api/health"]
plug :passthrough

defp passthrough(conn, _) do
Plug.Conn.send_resp(conn, 200, "Passthrough")
end
end

defp call(conn) do
MyPlug.call(conn, [])
end
Expand All @@ -51,6 +63,10 @@ defmodule Plug.QuietLoggerTest do
MyCustomPathPlug.call(conn, [])
end

defp multi_path_call(conn) do
MyMultiPathPlug.call(conn, [])
end

describe "Plug.QuietLogger.call/2" do
test "it suppresses logging on the designated health check and log level" do
log = capture_log_lines fn ->
Expand Down Expand Up @@ -85,6 +101,23 @@ defmodule Plug.QuietLoggerTest do

assert log == ""
end

test "it suppresses logging for multiple path" do
log = capture_log_lines fn ->
multi_path_call(conn(:get, "/api/status"))
end
assert log == ""

log = capture_log_lines fn ->
multi_path_call(conn(:get, "/api/health"))
end
assert log == ""

log = capture_log_lines fn ->
multi_path_call(conn(:get, "/api/something"))
end
assert Regex.match?(~r/api\/something/, log)
end
end

defp capture_log_lines(fun) do
Expand Down