Skip to content

Commit

Permalink
refactor logger, add ignore list as config
Browse files Browse the repository at this point in the history
fix ignore list


more ignore fixes
  • Loading branch information
whossname committed Jun 18, 2022
1 parent 07ad014 commit b7b6391
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 43 deletions.
74 changes: 33 additions & 41 deletions lib/slack_logger_backend/logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,44 @@ defmodule SlackLoggerBackend.Logger do
@default_log_levels [:error]

@doc false
def init(__MODULE__) do
{:ok, %{levels: []}}
end

def init({__MODULE__, levels}) when is_atom(levels) do
{:ok, %{levels: [levels]}}
end
def init(__MODULE__), do: do_init([])
def init({__MODULE__, level}) when is_atom(level), do: do_init([level])
def init({__MODULE__, levels}) when is_list(levels), do: do_init(levels)

def init({__MODULE__, levels}) when is_list(levels) do
{:ok, %{levels: levels}}
end

@doc false
def handle_call(_request, state) do
{:ok, state}
end

@doc false
def handle_event({level, _pid, {_, message, _timestamp, detail}}, %{levels: []} = state) do
defp do_init(levels) do
levels =
case get_env(:levels) do
nil -> @default_log_levels
case levels do
[] -> get_env(:levels, @default_log_levels)
levels -> levels
end

if level in levels do
do_handle_event(level, message, detail)
end
ignore =
get_env(:ignore, [])
|> Enum.map(&String.replace(&1, ~r/\s+/, " "))

{:ok, %{state | levels: levels}}
opts = [ignore: ignore]
{:ok, %{levels: levels, opts: opts}}
end

@doc false
def handle_event({level, _pid, {_, message, _timestamp, detail}}, %{levels: levels} = state) do
if level in levels do
do_handle_event(level, message, detail)
end
def handle_call(_request, state), do: {:ok, state}

{:ok, state}
end
@doc false
def handle_info(_message, state), do: {:ok, state}

@doc false
def handle_event(:flush, state) do
{:ok, state}
end
def handle_event(:flush, state), do: {:ok, state}

@doc false
def handle_info(_message, state) do
def handle_event({level, _pid, {_, message, _timestamp, detail}}, %{levels: levels} = state) do
if level in levels do
do_handle_event(level, message, detail, state.opts)
end

{:ok, state}
end

defp do_handle_event(level, message, detail) when is_list(detail) do
defp do_handle_event(level, message, detail, opts) when is_list(detail) do
detail
|> Keyword.take([
:application,
Expand All @@ -70,24 +56,30 @@ defmodule SlackLoggerBackend.Logger do
:line
])
|> Enum.into(%{level: level, message: message})
|> send_event()
|> send_event(opts)
end

defp do_handle_event(_level, _message, _detail) do
defp do_handle_event(_level, _message, _detail, _opts) do
:noop
end

defp send_event(event) do
defp send_event(event, opts) do
scrubber = get_env(:scrubber)
ignore_list = opts[:ignore] || []

message =
event.message
|> to_string()
|> FormatHelper.scrub(scrubber)
|> String.trim()

compare_message = String.replace(message, ~r/\s+/, " ")

event
|> Map.put(:message, message)
|> Producer.add_event()
unless Enum.member?(ignore_list, compare_message) do
event
|> Map.put(:message, message)
|> Producer.add_event()
end
end

defp get_env(key, default \\ nil) do
Expand Down
5 changes: 3 additions & 2 deletions test/slack_logger_backend/format_helper_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ defmodule SlackLoggerBackend.FormatHelperTest do
scrubber = [
{~r/(password|token|secret)(:\s+\")(.+?)(\")/, "\\1\\2--redacted--\\4"},
{~r/(^\w+\s+\|\s+)/, ""},
{~r/\#(PID|Reference)\<(\d|\.)+\>/, "#\\1\<\>"}
]

message =
"process_id | username: \"user\", password: \"password\", access_token: \"token\", client_secret: \"secret\""
"process_name | process_id: #PID<0.1234>, ref: #Reference<0.3570562526.2163015681.71254>, username: \"user\", password: \"password\", access_token: \"token\", client_secret: \"secret\""
|> FormatHelper.scrub(scrubber)

assert message ==
"username: \"user\", password: \"--redacted--\", access_token: \"--redacted--\", client_secret: \"--redacted--\""
"process_id: #PID<>, ref: #Reference<>, username: \"user\", password: \"--redacted--\", access_token: \"--redacted--\", client_secret: \"--redacted--\""
end
end
28 changes: 28 additions & 0 deletions test/slack_logger_backend_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,32 @@ defmodule SlackLoggerBackendTest do

Application.delete_env(:slack_logger_backend, :debounce_seconds)
end

test "ignores errors in the ignore list", %{bypass: bypass} do
msg1 = "I don't want to see this"
msg2 = "I also don't want to see this"
msg3 = "But I do want to see this"

Application.put_env(:slack_logger_backend, :ignore, [msg1, msg2])
start()

Bypass.expect_once(bypass, fn conn ->
assert "/hook" == conn.request_path
assert "POST" == conn.method
Plug.Conn.resp(conn, 200, "ok")
end)

Logger.error(msg1)
Logger.error(String.replace(msg2, ~r/\s+/, " "))

Logger.error("""
I don't want
to see this
""")

Logger.error(msg3)

Logger.flush()
:timer.sleep(100)
end
end

0 comments on commit b7b6391

Please sign in to comment.