-
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.
Implement compliant centralised logger
Rewrite the existing `Appsignal.Logger` to comply with the specs in our integration guide, specifically regarding the log levels, the log message format, logging to a file, and logging to stderr. Fixes #747, although this commit does not actually make it so the logger is used. Most logging statements throughout the application still use Elixir's `Logger`. See [the integration guide specification][ig] for more information. [ig]: https://github.com/appsignal/integration-guide/blob/main/build/logging.md
- Loading branch information
Showing
11 changed files
with
387 additions
and
95 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,8 @@ | ||
--- | ||
bump: "patch" | ||
type: "add" | ||
--- | ||
|
||
Log messages are now sent through a centralised logger, defaulting to logging | ||
to the `/tmp/appsignal.log` file. | ||
To log to standard output instead, set the `log` config property to `"stdout"`. |
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
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 |
---|---|---|
@@ -1,16 +1,100 @@ | ||
defmodule Appsignal.Logger do | ||
require Logger | ||
require Appsignal.Utils | ||
|
||
@logger Appsignal.Utils.compile_env(:appsignal, :logger, Logger) | ||
@io Appsignal.Utils.compile_env(:appsignal, :io, IO) | ||
@file_module Appsignal.Utils.compile_env(:appsignal, :file, File) | ||
|
||
@spec debug(any()) :: :ok | ||
@doc """ | ||
Passes the debug message to `Logger.debug/1` if `Appsignal.Config.debug?/0` | ||
is `true`. | ||
""" | ||
def debug(log) do | ||
Appsignal.Config.debug?() && @logger.debug(log) | ||
:ok | ||
@log_levels [:trace, :debug, :info, :warn, :error] | ||
|
||
def trace(message) do | ||
log(:trace, message, []) | ||
end | ||
|
||
def debug(message) do | ||
log(:debug, message, []) | ||
end | ||
|
||
def info(message) do | ||
log(:info, message, []) | ||
end | ||
|
||
def warn(message, options \\ []) do | ||
log(:warn, message, options) | ||
end | ||
|
||
def error(message, options \\ []) do | ||
log(:error, message, options) | ||
end | ||
|
||
defp log(level, message, options) do | ||
threshold = Appsignal.Config.log_level() | ||
|
||
if log_level?(level, threshold) do | ||
case {device(), Keyword.get(options, :stderr, false)} do | ||
{device, false} -> | ||
do_log(device, level, message) | ||
|
||
{:stdio, true} -> | ||
do_log(:stderr, level, message) | ||
|
||
{:file, true} -> | ||
do_log(:file, level, message) | ||
do_log(:stderr, level, message) | ||
end | ||
end | ||
end | ||
|
||
defp device do | ||
case Application.fetch_env!(:appsignal, :config)[:log] do | ||
"stdout" -> :stdio | ||
_ -> :file | ||
end | ||
end | ||
|
||
defp log_level?(level, threshold) do | ||
Enum.find_index(@log_levels, &(&1 == level)) >= | ||
Enum.find_index(@log_levels, &(&1 == threshold)) | ||
end | ||
|
||
defp do_log(device, level, message) do | ||
time = NaiveDateTime.from_erl!(:calendar.local_time()) | ||
pid = System.pid() | ||
|
||
puts(device, format(device, time, pid, level, message)) | ||
end | ||
|
||
defp puts(:file, content) do | ||
@file_module.write(Appsignal.Config.log_file_path(), content <> "\n", [:append, :utf8]) | ||
end | ||
|
||
defp puts(device, content) do | ||
@io.puts(device, content) | ||
end | ||
|
||
defp format(device, time, pid, level, message) do | ||
time = format_time(time) | ||
level = format_level(level) | ||
|
||
case device do | ||
:stdio -> "\n[#{time} (process) ##{pid}][appsignal][#{level}] #{message}" | ||
:stderr -> "\n[appsignal][#{level}] #{message}" | ||
:file -> "[#{time} (process) ##{pid}][#{level}] #{message}" | ||
end | ||
end | ||
|
||
defp format_level(level) do | ||
case level do | ||
:trace -> "TRACE" | ||
:debug -> "DEBUG" | ||
:info -> "INFO" | ||
:warn -> "WARNING" | ||
:error -> "ERROR" | ||
end | ||
end | ||
|
||
defp format_time(time) do | ||
time | ||
|> NaiveDateTime.truncate(:second) | ||
|> NaiveDateTime.to_iso8601() | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.