Logger
A logger for Elixir applications.
It includes many features:
Provides debug, info, warn, and error levels.
Supports multiple backends which are automatically supervised when plugged into
Logger
.Formats and truncates messages on the client to avoid clogging
Logger
backends.Alternates between sync and async modes to remain performant when required but also apply backpressure when under stress.
Integrates with Erlang's
:logger
to convert terms to Elixir syntax.
Logging is useful for tracking when an event of interest happens in your system. For example, it may be helpful to log whenever a user is deleted.
def delete_user(user) do Logger.info("Deleting user from the system: #{inspect(user)}") # ... end
The Logger.info/2
macro emits the provided message at the :info
level. Note the arguments given to info/2
will only be evaluated if a message is logged. For instance, if the Logger level is set to :warn
, :info
messages are never logged and therefore the arguments given above won't even be executed.
There are additional macros for other levels.
Logger also allows log commands to be removed altogether via the :compile_time_purge_matching
option (see below).
For dynamically logging messages, see bare_log/3
. But note that bare_log/3
always evaluates its arguments (unless the argument is an anonymous function).
Levels
The supported levels, ordered by precedence, are:
-
:debug
- for debug-related messages -
:info
- for information of any kind -
:warn
- for warnings -
:error
- for errors
For example, :info
takes precedence over :debug
. If your log level is set to :info
, :info
, :warn
, and :error
will be printed to the console. If your log level is set to :warn
, only :warn
and :error
will be printed.
Metadata
Whenever a message is logged, additional information can be given via metadata. Each log operation, such as Logger.info/2
, allows metadata to be given as argument.
Furthermore, metadata can be set per process with Logger.metadata/1
.
Some metadata, however, is always added automatically by Logger whenever possible. Those are:
:application
- the current application:mfa
- the current module, function and arity:file
- the current file:line
- the current line:pid
- the current process identifier:initial_call
- the initial call that started the process:registered_name
- the process registered name as an atom:domain
- a list of domains for the logged message. For example, all Elixir reports default to[:elixir]
. Erlang reports may start with[:otp]
or[:sasl]
:crash_reason
- a two-element tuple with the throw/error/exit reason as first argument and the stacktrace as second. A throw will always be{:nocatch, term}
. An error is always anException
struct. All other entries are exits. The console backend ignores this metadata by default but it can be useful to other backends, such as the ones that report errors to third-party services
Note that all metadata is optional and may not always be available. The :mfa
, :file
, :line
, and similar metadata are automatically included when using Logger
macros. Logger.bare_log/3
does not include any metadata beyond the :pid
by default. Other metadata, such as :crash_reason
, :initial_call
, and :registered_name
are available only inside behaviours such as GenServer, Supervisor, and others.
Configuration
Logger
supports a wide range of configurations.
This configuration is split in three categories:
Application configuration - must be set before the
:logger
application is startedRuntime configuration - can be set before the
:logger
application is started, but may be changed during runtimeErlang configuration - options that handle integration with Erlang's logging facilities
Application configuration
The following configuration must be set via config files (such as config/config.exs
) before the :logger
application is started.
:backends
- the backends to be used. Defaults to[:console]
. See the "Backends" section for more information.:compile_time_application
- sets the:application
metadata value to the configured value at compilation time. This configuration is automatically set by Mix and made available as metadata when logging.-
:compile_time_purge_matching
- purges at compilation time all calls that match the given conditions. This means thatLogger
calls with level lower than this option will be completely removed at compile time, accruing no overhead at runtime. This configuration expects a list of keyword lists. Each keyword list contains a metadata key and the matching value that should be purged. Some special keys are supported:-
:level_lower_than
- purges all messages with a lower logger level -
:module
- purges all messages with the matching module -
:function
- purges all messages with the "function/arity" Remember that if you want to purge log calls from a dependency, the dependency must be recompiled.
-
:start_options
- passes start options to Logger's main process, such as:spawn_opt
and:hibernate_after
. All options int:GenServer.option
are accepted, except:name
.
For example, to configure the :backends
and purge all calls that happen at compile time with level lower than :info
in a config/config.exs
file:
config :logger, backends: [:console], compile_time_purge_matching: [ [level_lower_than: :info] ]
If you want to purge all log calls from an application named :foo
and only keep errors from Bar.foo/3
, you can set up two different matches:
config :logger, compile_time_purge_matching: [ [application: :foo], [module: Bar, function: "foo/3", level_lower_than: :error] ]
Runtime Configuration
All configuration below can be set via config files (such as config/config.exs
) but also changed dynamically during runtime via Logger.configure/1
.
:level
- the logging level. Attempting to log any message with severity less than the configured level will simply cause the message to be ignored. Keep in mind that each backend may have its specific level, too.:utc_log
- whentrue
, uses UTC in logs. By default it uses local time (i.e., it defaults tofalse
).:truncate
- the maximum message size to be logged (in bytes). Defaults to 8192 bytes. Note this configuration is approximate. Truncated messages will have" (truncated)"
at the end. The atom:infinity
can be passed to disable this behavior.:sync_threshold
- if theLogger
manager has more than:sync_threshold
messages in its queue,Logger
will change to sync mode, to apply backpressure to the clients.Logger
will return to async mode once the number of messages in the queue is reduced to one below thesync_threshold
. Defaults to 20 messages.:sync_threshold
can be set to0
to force sync mode.:discard_threshold
- if theLogger
manager has more than:discard_threshold
messages in its queue,Logger
will change to discard mode and messages will be discarded directly in the clients.Logger
will return to sync mode once the number of messages in the queue is reduced to one below thediscard_threshold
. Defaults to 500 messages.:discard_threshold_periodic_check
- a periodic check that checks and reports if logger is discarding messages. It logs a warn message whenever the system is (or continues) in discard mode and it logs a warn message whenever if the system was discarding messages but stopped doing so after the previous check. By default it runs every30_000
milliseconds.:translator_inspect_opts
- when translating OTP reports and errors, the last message and state must be inspected in the error reports. This configuration allow developers to change how much and how the data should be inspected.
For example, to configure the :level
and :truncate
options in a config/config.exs
file:
config :logger, level: :warn, truncate: 4096
Erlang/OTP integration
From Elixir v1.10, Elixir's Logger is fully integrated with Erlang's logger. They share the same Logger.level/0
, any metadata set with Logger.metadata/1
applies to both, and so on.
Elixir also supports formatting Erlang reports using Elixir syntax. This can be controlled with two configurations:
:handle_otp_reports
- redirects OTP reports toLogger
so they are formatted in Elixir terms. This effectively disables Erlang standard logger. Defaults totrue
.:handle_sasl_reports
- redirects supervisor, crash and progress reports toLogger
so they are formatted in Elixir terms. Your application must guarantee:sasl
is started before:logger
. This means you may see some initial reports written in Erlang syntax until the Logger application kicks in. Defaults tofalse
. This option only has an effect if:handle_otp_reports
is true.
For example, to configure Logger
to redirect all Erlang messages using a config/config.exs
file:
config :logger, handle_otp_reports: true, handle_sasl_reports: true
Furthermore, Logger
allows messages sent by Erlang to be translated into an Elixir format via translators. Translators can be added at any time with the add_translator/1
and remove_translator/1
APIs. Check Logger.Translator
for more information.
Backends
Logger
supports different backends where log messages are written to.
The available backends by default are:
-
:console
- logs messages to the console (enabled by default)
Developers may also implement their own backends, an option that is explored in more detail below.
The initial backends are loaded via the :backends
configuration, which must be set before the :logger
application is started. Backends can also be added dynamically through add_backend/2
.
Console backend
The console backend logs messages by printing them to the console. It supports the following options:
:level
- the level to be logged by this backend. Note that messages are filtered by the general:level
configuration for the:logger
application first.:format
- the format message used to print logs. Defaults to:"\n$time $metadata[$level] $levelpad$message\n"
. It may also be a{module, function}
tuple that is invoked with the log level, the message, the current timestamp and the metadata.:metadata
- the metadata to be printed by$metadata
. Defaults to an empty list (no metadata). Setting:metadata
to:all
prints all metadata. See the "Metadata" section for more information.:colors
- a keyword list of coloring options.:device
- the device to log error messages to. Defaults to:user
but can be changed to something else such as:standard_error
.:max_buffer
- maximum events to buffer while waiting for a confirmation from the IO device (default: 32). Once the buffer is full, the backend will block until a confirmation is received.
The supported keys in the :colors
keyword list are:
:enabled
- boolean value that allows for switching the coloring on and off. Defaults to:IO.ANSI.enabled?/0
:debug
- color for debug messages. Defaults to::cyan
:info
- color for info messages. Defaults to::normal
:warn
- color for warn messages. Defaults to::yellow
:error
- color for error messages. Defaults to::red
See the IO.ANSI
module for a list of colors and attributes.
Here is an example of how to configure the :console
backend in a config/config.exs
file:
config :logger, :console, format: "\n$time $metadata[$level] $levelpad$message\n", metadata: [:user_id]
Custom formatting
The console backend allows you to customize the format of your log messages with the :format
option.
You may set :format
to either a string or a {module, function}
tuple if you wish to provide your own format function. Here is an example of how to configure the :console
backend in a config/config.exs
file:
config :logger, :console, format: {MyConsoleLogger, :format}
And here is an example of how you can define MyConsoleLogger.format/4
from the above configuration:
defmodule MyConsoleLogger do def format(level, message, timestamp, metadata) do # Custom formatting logic... end end
It is extremely important that the formatting function does not fail, as it will bring that particular logger instance down, causing your system to temporarily lose messages. If necessary, wrap the function in a rescue
and log a default message instead:
defmodule MyConsoleLogger do def format(level, message, timestamp, metadata) do # Custom formatting logic... rescue _ -> "could not format: #{inspect({level, message, metadata})}" end end
The {module, function}
will be invoked with four arguments:
- the log level: an atom
- the message: this is usually chardata, but in some cases it may contain invalid data. Since the formatting function should never fail, you need to prepare for the message being anything
- the current timestamp: a term of type
Logger.Formatter.time/0
- the metadata: a keyword list
You can read more about formatting in Logger.Formatter
, especially if you want to support custom formatting in a custom backend.
Custom backends
Any developer can create their own Logger
backend. Since Logger
is an event manager powered by :gen_event
, writing a new backend is a matter of creating an event handler, as described in the :gen_event
documentation.
From now on, we will be using the term "event handler" to refer to your custom backend, as we head into implementation details.
Once the :logger
application starts, it installs all event handlers listed under the :backends
configuration into the Logger
event manager. The event manager and all added event handlers are automatically supervised by Logger
.
Note that if a backend fails to start by returning {:error, :ignore}
from its init/1
callback, then it's not added to the backends but nothing fails. If a backend fails to start by returning {:error, reason}
from its init/1
callback, the :logger
application will fail to start.
Once initialized, the handler should be designed to handle the following events:
-
{level, group_leader, {Logger, message, timestamp, metadata}}
where:-
level
is one of:debug
,:info
,:warn
, or:error
, as previously described -
group_leader
is the group leader of the process which logged the message -
{Logger, message, timestamp, metadata}
is a tuple containing information about the logged message:- the first element is always the atom
Logger
-
message
is the actual message (as chardata) -
timestamp
is the timestamp for when the message was logged, as a{{year, month, day}, {hour, minute, second, millisecond}}
tuple -
metadata
is a keyword list of metadata used when logging the message
- the first element is always the atom
-
:flush
It is recommended that handlers ignore messages where the group leader is in a different node than the one where the handler is installed. For example:
def handle_event({_level, gl, {Logger, _, _, _}}, state) when node(gl) != node() do {:ok, state} end
In the case of the event :flush
handlers should flush any pending data. This event is triggered by Logger.flush/0
.
Furthermore, backends can be configured via the configure_backend/2
function which requires event handlers to handle calls of the following format:
{:configure, options}
where options
is a keyword list. The result of the call is the result returned by configure_backend/2
. The recommended return value for successful configuration is :ok
. For example:
def handle_call({:configure, options}, state) do new_state = reconfigure_state(state, options) {:ok, :ok, new_state} end
It is recommended that backends support at least the following configuration options:
-
:level
- the logging level for that backend -
:format
- the logging format for that backend -
:metadata
- the metadata to include in that backend
Check Logger.Backends.Console
's implementation for examples on how to handle the recommendations in this section and how to process the existing options.
Erlang/OTP handlers
While Elixir Logger provides backends, Erlang/OTP logger provides handlers. They represent the same concept: the ability to integrate into the logging system to handle each logged message/event.
However, implementation-wise, they have the following differences:
Elixir backends run in a separate process which comes with overload protection. However, because this process is a single GenEvent, any long running action should be avoided, as it can lead to bottlenecks in the system
Erlang handlers run in the same process as the process logging the message/event. This gives developers more flexibility but they should avoid perform any long running action in such handlers, as it may slow down the action being executed considerably. At the moment, there is no built-in overload protection for Erlang handlers, so it is your responsibility to implement it
The good news is that developers can use third-party implementations of both Elixir backends and Erlang handlers.
Elixir backends can be configured directly under the :logger
application in your config/config.exs
:
config :logger, backends: [ACustomBackend]
Erlang/OTP handlers must be listed under your own application:
config :my_app, :logger, [:handler, :name_of_the_handler, ACustomHandler, configuration = %{}]
And then explicitly attached in your Application.start/2
callback:
:logger.add_handlers(:my_app)
Note we do not recommend configuring Erlang/OTP's logger directly under the :kernel
application in your config/config.exs
, like this:
config :kernel, :logger, ...
This is because by the time Elixir starts, Erlang's kernel has already been started, which means the configuration above would have no effect.
Summary
Types
Functions
- add_backend(backend, opts \\ [])
Adds a new backend.
- add_translator(translator)
Adds a new translator.
- bare_log(level, chardata_or_fun, metadata \\ [])
Logs a message dynamically.
- compare_levels(left, right)
Compares log levels.
- configure(options)
Configures the logger.
- configure_backend(backend, options)
Configures the given backend.
- debug(chardata_or_fun, metadata \\ [])
Logs a debug message.
- disable(pid)
Disables logging for the current process.
- enable(pid)
Enables logging for the current process.
- enabled?(pid)
Returns whether the logging is enabled for given process.
- error(chardata_or_fun, metadata \\ [])
Logs an error message.
- flush()
Flushes the logger.
- info(chardata_or_fun, metadata \\ [])
Logs an info message.
- log(level, chardata_or_fun, metadata \\ [])
Logs a message with the given
level
.- metadata()
Reads the current process metadata.
- metadata(keyword)
Alters the current process metadata according the given keyword list.
- remove_backend(backend, opts \\ [])
Removes a backend.
- remove_translator(translator)
Removes a translator.
- reset_metadata(keyword \\ [])
Resets the current process metadata to the given keyword list.
- warn(chardata_or_fun, metadata \\ [])
Logs a warning message.
Types
backend()
Specs
backend() :: :gen_event.handler()
level()
Specs
level() :: :error | :warn | :info | :debug
message()
Specs
message() :: IO.chardata() | String.Chars.t()
metadata()
Specs
metadata() :: keyword()
Functions
add_backend(backend, opts \\ [])
Specs
add_backend(backend(), keyword()) :: Supervisor.on_start_child()
Adds a new backend.
Adding a backend calls the init/1
function in that backend with the name of the backend as its argument. For example, calling
Logger.add_backend(MyBackend)
will call MyBackend.init(MyBackend)
to initialize the new backend. If the backend's init/1
callback returns {:ok, _}
, then this function returns {:ok, pid}
. If the handler returns {:error, :ignore}
from init/1
, this function still returns {:ok, pid}
but the handler is not started. If the handler returns {:error, reason}
from init/1
, this function returns {:error, {reason, info}}
where info
is more information on the backend that failed to start.
Backends added by this function are not persisted. Therefore if the Logger application or supervision tree is restarted, the backend won't be available. If you need this guarantee, then configure the backend via the application environment:
config :logger, :backends, [MyBackend]
Options
-
:flush
- whentrue
, guarantees all messages currently sent toLogger
are processed before the backend is added
Examples
{:ok, _pid} = Logger.add_backend(MyBackend, flush: true)
add_translator(translator)
Specs
add_translator({module(), function :: atom()}) :: :ok
Adds a new translator.
bare_log(level, chardata_or_fun, metadata \\ [])
Specs
bare_log( level(), message() | (() -> message() | {message(), keyword()}), keyword() ) :: :ok
Logs a message dynamically.
Opposite to log/3
, debug/2
, info/2
, and friends, the arguments given to bare_log/3
are always evaluated. However, you can pass anonymous functions to bare_log/3
and they will only be evaluated if there is something to be logged.
compare_levels(left, right)
Specs
compare_levels(level(), level()) :: :lt | :eq | :gt
Compares log levels.
Receives two log levels and compares the left
level against the right
level and returns:
-
:lt
ifleft
is less thanright
-
:eq
ifleft
andright
are equal -
:gt
ifleft
is greater thanright
Examples
iex> Logger.compare_levels(:debug, :warn) :lt iex> Logger.compare_levels(:error, :info) :gt
configure(options)
Specs
configure(keyword()) :: :ok
Configures the logger.
See the "Runtime Configuration" section in the Logger
module documentation for the available options. The changes done here are automatically persisted to the :logger
application environment.
configure_backend(backend, options)
Specs
configure_backend(backend(), keyword()) :: term()
Configures the given backend.
The backend needs to be started and running in order to be configured at runtime.
debug(chardata_or_fun, metadata \\ [])
Logs a debug message.
Returns :ok
.
Examples
Logger.debug("hello?")
disable(pid)
Specs
disable(pid()) :: :ok
Disables logging for the current process.
Currently the only accepted PID is self()
.
enable(pid)
Specs
enable(pid()) :: :ok
Enables logging for the current process.
Currently the only accepted PID is self()
.
enabled?(pid)
Specs
enabled?(pid()) :: boolean()
Returns whether the logging is enabled for given process.
Currently the only accepted PID is self()
.
error(chardata_or_fun, metadata \\ [])
Logs an error message.
Returns :ok
.
Examples
Logger.error("oops")
flush()
Specs
flush() :: :ok
Flushes the logger.
This guarantees all messages sent to Logger
prior to this call will be processed. This is useful for testing and it should not be called in production code.
info(chardata_or_fun, metadata \\ [])
Logs an info message.
Returns :ok
.
Examples
Logger.info("mission accomplished")
level()
Specs
level() :: level()
Retrieves the Logger
level.
The Logger
level can be changed via configure/1
.
log(level, chardata_or_fun, metadata \\ [])
Logs a message with the given level
.
Returns :ok
.
The macros debug/2
, warn/2
, info/2
, and error/2
are preferred over this macro as they can automatically eliminate the call to Logger
altogether at compile time if desired (see the documentation for the Logger
module).
metadata()
Specs
metadata() :: metadata()
Reads the current process metadata.
metadata(keyword)
Specs
metadata(metadata()) :: :ok
Alters the current process metadata according the given keyword list.
This function will merge the given keyword list into the existing metadata, with the exception of setting a key to nil
, which will remove that key from the metadata.
remove_backend(backend, opts \\ [])
Specs
remove_backend(backend(), keyword()) :: :ok | {:error, term()}
Removes a backend.
Options
-
:flush
- whentrue
, guarantees all messages currently sent toLogger
are processed before the backend is removed
remove_translator(translator)
Specs
remove_translator({module(), function :: atom()}) :: :ok
Removes a translator.
reset_metadata(keyword \\ [])
Specs
reset_metadata(metadata()) :: :ok
Resets the current process metadata to the given keyword list.
warn(chardata_or_fun, metadata \\ [])
Logs a warning message.
Returns :ok
.
Examples
Logger.warn("knob turned too far to the right")
© 2012 Plataformatec
Licensed under the Apache License, Version 2.0.
https://hexdocs.pm/logger/1.10.4/Logger.html