From 57e34ee635da78b7df917fef03e952577722b70d Mon Sep 17 00:00:00 2001 From: Roman Heinrich Date: Mon, 16 Jul 2018 16:27:22 +0200 Subject: [PATCH 1/3] introduce multi-path configuration problem: - sometimes you might have more than one healthcheck, eg. in Kubernetes we have readiness / liveliness checks solution: - extend the api to accept list of paths to ignore + tests --- README.md | 8 +++++++- lib/plug/quiet_logger.ex | 11 ++++++++++- test/plug/quiet_logger_test.exs | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fab73d1..b8eb46c 100644 --- a/README.md +++ b/README.md @@ -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: + +``` +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`: diff --git a/lib/plug/quiet_logger.ex b/lib/plug/quiet_logger.ex index c70d992..9335e39 100644 --- a/lib/plug/quiet_logger.ex +++ b/lib/plug/quiet_logger.ex @@ -8,9 +8,18 @@ defmodule Plug.QuietLogger do %{log: log, path: path} end + def call(%{request_path: path} = conn, %{log: :info, path: paths}) when is_list(paths) do + cond do + path in paths -> conn + true -> Plug.Logger.call(conn, :info) + end + + conn + 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 diff --git a/test/plug/quiet_logger_test.exs b/test/plug/quiet_logger_test.exs index cd47a30..bd03174 100644 --- a/test/plug/quiet_logger_test.exs +++ b/test/plug/quiet_logger_test.exs @@ -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 @@ -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 -> @@ -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 From 5776391a21c53c66bfb9451992649dbc4bc2512d Mon Sep 17 00:00:00 2001 From: Roman Heinrich Date: Mon, 16 Jul 2018 16:38:50 +0200 Subject: [PATCH 2/3] readme codesnippet is elixir :) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b8eb46c..954ff45 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ 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" ] ``` From 3ce97b665d651d28bf1c5cb5bf9f8df2840f2f24 Mon Sep 17 00:00:00 2001 From: Roman Heinrich Date: Tue, 24 Jul 2018 13:04:46 +0200 Subject: [PATCH 3/3] make multipath `call` function simpler --- lib/plug/quiet_logger.ex | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/plug/quiet_logger.ex b/lib/plug/quiet_logger.ex index 9335e39..9eb0172 100644 --- a/lib/plug/quiet_logger.ex +++ b/lib/plug/quiet_logger.ex @@ -8,13 +8,8 @@ defmodule Plug.QuietLogger do %{log: log, path: path} end - def call(%{request_path: path} = conn, %{log: :info, path: paths}) when is_list(paths) do - cond do - path in paths -> conn - true -> Plug.Logger.call(conn, :info) - end - - conn + 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