Skip to content

Commit

Permalink
Update Logger levels to match with Elixir standard
Browse files Browse the repository at this point in the history
The Logger levels now match with the ones in the language standard
Logger.
  • Loading branch information
luismiramirez committed Nov 16, 2022
1 parent beb0c43 commit 5b69883
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 23 deletions.
43 changes: 31 additions & 12 deletions lib/appsignal/logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ defmodule Appsignal.Logger do

@nif Appsignal.Utils.compile_env(:appsignal, :appsignal_tracer_nif, Appsignal.Nif)
@severity %{
trace: 1,
debug: 2,
info: 3,
warn: 5,
error: 6
notice: 4,
warning: 5,
error: 6,
critical: 7,
alert: 8,
emergency: 9
}

@type log_level :: :trace | :debug | :info | :warn | :error

@spec trace(String.t(), String.t(), %{}) :: :ok
def trace(group, message, metadata \\ %{}) do
log(:trace, group, message, metadata)
end
@type log_level ::
:debug | :info | :notice | :warning | :error | :critical | :alert | :emergency

@spec debug(String.t(), String.t(), %{}) :: :ok
def debug(group, message, metadata \\ %{}) do
Expand All @@ -27,16 +26,36 @@ defmodule Appsignal.Logger do
log(:info, group, message, metadata)
end

@spec warn(String.t(), String.t(), %{}) :: :ok
def warn(group, message, metadata \\ %{}) do
log(:warn, group, message, metadata)
@spec notice(String.t(), String.t(), %{}) :: :ok
def notice(group, message, metadata \\ %{}) do
log(:notice, group, message, metadata)
end

@spec warning(String.t(), String.t(), %{}) :: :ok
def warning(group, message, metadata \\ %{}) do
log(:warning, group, message, metadata)
end

@spec error(String.t(), String.t(), %{}) :: :ok
def error(group, message, metadata \\ %{}) do
log(:error, group, message, metadata)
end

@spec critical(String.t(), String.t(), %{}) :: :ok
def critical(group, message, metadata \\ %{}) do
log(:critical, group, message, metadata)
end

@spec alert(String.t(), String.t(), %{}) :: :ok
def alert(group, message, metadata \\ %{}) do
log(:alert, group, message, metadata)
end

@spec emergency(String.t(), String.t(), %{}) :: :ok
def emergency(group, message, metadata \\ %{}) do
log(:emergency, group, message, metadata)
end

@spec log(log_level(), String.t(), String.t(), %{}) :: :ok
defp log(log_level, group, message, metadata) do
severity = @severity[log_level]
Expand Down
46 changes: 35 additions & 11 deletions test/appsignal/logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ defmodule Appsignal.LoggerTest do
:ok
end

test "trace/3 sends the trace log call through the extension" do
metadata = %{some: "metadata"}

Logger.trace("app", "This is a trace", metadata)

assert [{"app", 1, "This is a trace", _encoded_metadata}] = Test.Nif.get!(:log)
end

test "debug/3 sends the debug log call through the extension" do
metadata = %{some: "metadata"}

Expand All @@ -31,12 +23,20 @@ defmodule Appsignal.LoggerTest do
assert [{"app", 3, "This is an info", _encoded_metadata}] = Test.Nif.get!(:log)
end

test "warn/3 sends the warn log call through the extension" do
test "notice/3 sends the notice log call through the extension" do
metadata = %{some: "metadata"}

Logger.warn("app", "This is a warn", metadata)
Logger.notice("app", "This is a notice", metadata)

assert [{"app", 5, "This is a warn", _encoded_metadata}] = Test.Nif.get!(:log)
assert [{"app", 4, "This is a notice", _encoded_metadata}] = Test.Nif.get!(:log)
end

test "warning/3 sends the warning log call through the extension" do
metadata = %{some: "metadata"}

Logger.warning("app", "This is a warning", metadata)

assert [{"app", 5, "This is a warning", _encoded_metadata}] = Test.Nif.get!(:log)
end

test "error/3 sends the error log call through the extension" do
Expand All @@ -46,4 +46,28 @@ defmodule Appsignal.LoggerTest do

assert [{"app", 6, "This is an error", _encoded_metadata}] = Test.Nif.get!(:log)
end

test "critical/3 sends the critical log call through the extension" do
metadata = %{some: "metadata"}

Logger.critical("app", "This is a critical", metadata)

assert [{"app", 7, "This is a critical", _encoded_metadata}] = Test.Nif.get!(:log)
end

test "alert/3 sends the alert log call through the extension" do
metadata = %{some: "metadata"}

Logger.alert("app", "This is an alert", metadata)

assert [{"app", 8, "This is an alert", _encoded_metadata}] = Test.Nif.get!(:log)
end

test "emergency/3 sends the emergency log call through the extension" do
metadata = %{some: "metadata"}

Logger.emergency("app", "This is an emergency", metadata)

assert [{"app", 9, "This is an emergency", _encoded_metadata}] = Test.Nif.get!(:log)
end
end

0 comments on commit 5b69883

Please sign in to comment.