-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Expose Appsignal.Logger.log/4 Previously, Appsignal.Logger only exposed the named functions (like debug/3). Since we need the log/4 function to provide a backend, this patch eposes it. * Add Appsignal.Logger.Backend Appsignal.Logger.Backend is attached to Elixir's logger to send logs to AppSignal. Use Logger.add_backend/1 to set it up: Logger.add_backend({Appsignal.Logger.Backend, "phoenix"}) The second argument in the tuple is the log group name, which could be "phoenix" for a Phoenix application, for example. * Add changeset to describe logger backend > Add Logger backend to redirect Elixir logs to AppSignal. * Update lib/appsignal/logger/backend.ex Co-authored-by: Noemi <45180344+unflxw@users.noreply.github.com> * Use keyword list in Logger.Backend Instead of taking a string as the log group, take a keyword list with a single key, for possible extension later. * Handle :warn warnings in Appsignal.Logger Deprecated, but still used in older versions of Elixir, warnings named :warn previously broke the logger. This patch accepts them as warnings. * Handle unexpected log levels Although this shouldn't happen, not finding a severity in the list caused Appsignal.Logger.Backend to crash. This patch adds a rescue clause to turn everything that doesn't match the list to a severity of 3 (info). --------- Co-authored-by: Noemi <45180344+unflxw@users.noreply.github.com>
- Loading branch information
1 parent
79b1ac2
commit 6462f80
Showing
5 changed files
with
108 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
bump: "minor" | ||
type: "add" | ||
--- | ||
|
||
Add Logger backend to redirect Elixir logs to AppSignal. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule Appsignal.Logger.Backend do | ||
@behaviour :gen_event | ||
|
||
def init({__MODULE__, options}) do | ||
{:ok, Keyword.merge([group: "app"], options)} | ||
end | ||
|
||
def handle_event({level, _gl, {Logger, message, _timestamp, metadata}}, options) do | ||
Appsignal.Logger.log( | ||
level, | ||
options[:group], | ||
IO.chardata_to_string(message), | ||
Enum.into(metadata, %{}) | ||
) | ||
|
||
{:ok, options} | ||
end | ||
|
||
def handle_call(_messsage, options) do | ||
{:ok, nil, options} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
defmodule Appsignal.Logger.BackendTest do | ||
use ExUnit.Case | ||
|
||
setup do | ||
start_supervised!(Appsignal.Test.Nif) | ||
:ok | ||
end | ||
|
||
test "handle_event/2 sends logs to the extension" do | ||
Appsignal.Logger.Backend.handle_event( | ||
{ | ||
:info, | ||
self(), | ||
{ | ||
Logger, | ||
~c"foo bar baz", | ||
{{2023, 2, 23}, {14, 25, 56, 241}}, | ||
[ | ||
erl_level: :info, | ||
application: :phoenix, | ||
domain: [:elixir], | ||
file: "lib/phoenix/logger.ex", | ||
function: "phoenix_endpoint_start/4", | ||
gl: self(), | ||
line: 211, | ||
mfa: {Phoenix.Logger, :phoenix_endpoint_start, 4}, | ||
module: Phoenix.Logger, | ||
pid: self(), | ||
request_id: "F0Z3jGx7KNZWD9gAAAFD", | ||
time: 1_677_159_356_241_326 | ||
] | ||
} | ||
}, | ||
group: "phoenix" | ||
) | ||
|
||
assert [{"phoenix", 3, "foo bar baz", _}] = Appsignal.Test.Nif.get!(:log) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters