exercism/elixir/log-level/lib/log_level.ex

48 lines
1.3 KiB
Elixir
Raw Permalink Normal View History

2023-12-17 05:26:14 +00:00
defmodule LogLevel do
@doc """
Log code Log label Supported in legacy apps?
0 trace no
1 debug yes
2 info yes
3 warning yes
4 error yes
5 fatal no
other unknown -
case {level, legacy?} do
{0, false} -> :trace
{1, _} -> :debug
{2, _} -> :info
{3, _} -> :warning
{4, _} -> :error
{5, false} -> :fatal
_ -> :unknown
end
"""
def to_label(level, legacy?) do
cond do
level == 0 and not legacy? -> :trace
level == 1 -> :debug
level == 2 -> :info
level == 3 -> :warning
level == 4 -> :error
level == 5 and not legacy? -> :fatal
true -> :unknown
end
end
@doc """
If the log label is error or fatal, send the alert to the ops team. If you receive a log with an unknown label from a legacy system, send the alert to the dev1 team, other unknown labels should be sent to the dev2 team. All other log labels can be safely ignored by returning false.
"""
def alert_recipient(level, legacy?) do
label = to_label(level, legacy?)
cond do
label in [:error, :fatal] -> :ops
label == :unknown and legacy? -> :dev1
label == :unknown -> :dev2
true -> false
end
end
end