View Source Logger (Logger v1.15.5)

A logger for Elixir applications.

This application is mostly a wrapper around Erlang's :logger functionality, to provide message translation and formatting to Elixir terms.

Overall, you will find that Logger:

  • Provides all 7 syslog levels (although debug, info, warning, and error are the most commonly used).

  • Supports both message-based and structural logging.

  • 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.

  • Support for custom filters and handlers as provided by Erlang's :logger.

  • Allows overriding the logging level for a specific module, application or process.

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 :warning, :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 importance, are:

  • :emergency - when system is unusable, panics
  • :alert - for alerts, actions that must be taken immediately, ex. corrupted database
  • :critical - for critical conditions
  • :error - for errors
  • :warning - for warnings
  • :notice - for normal, but significant, messages
  • :info - for information of any kind
  • :debug - for debug-related messages

For example, :info takes precedence over :debug. If your log level is set to :info, then all :info, :notice and above will be passed to backends. If your log level is set to :alert, only :alert and :emergency will be printed.

Message

Logger can be used for logging both unstructured and structured data.

Unstructured data is a string or a list of strings:

Logger.info("hello world!")
Logger.info(["hello ", "world!"])

Structured data, also known as reports, are keyword lists and maps:

Logger.info([new_user: user.id, account_type: :admin])
Logger.info(%{new_user: user.id, account_type: :admin})

Log functions also accept a zero-arity anonymous function as a message:

Logger.info(fn -> "hello world!" end)

The anonymous function can return a message or a tuple containing the message and additional metadata (to be described in the next section).

In all cases, the arguments given to the Logger macros are only evaluated if required by the current log level. The exception is the bare_log/3 function, which is the raw mechanism for logging.

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 an 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 an Exception 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

There are two special metadata keys, :module and :function, which extract the relevant bits from :mfa.

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.

For example, you might wish to include a custom :error_code metadata in your logs:

Logger.error("We have a problem", [error_code: :pc_load_letter])

By default, no metadata is logged. We will learn how to enable that over the next sections.

Configuration

Logger supports a wide range of configurations.

This configuration is split in three categories:

  • Boot configuration - this configuration is read when logger starts and configures how Elixir hooks into Erlang's own logger

  • Compile configuration - this must be set before your code is compiled

  • Runtime configuration - can be set before the :logger application is started, but may be changed during runtime

Boot configuration

When Logger starts, it configures the :default log handler from Erlang to translate and format Elixir terms. As a developer, you are able to customize the default handler, the default formatter, and many other options.

The following configuration must be set via config files (such as config/config.exs), under the :logger key, before your application is started:

  • :default_formatter - a keyword list which configures the default formatter used by the default handler. See Logger.Formatter for the full list of configuration.

  • :default_handler - this option configures the default handler used for logging. The default handler is a :logger_std_h instance which also supports file logging and log rotation. You can set it to false to disable the default logging altogether. See the examples below for more information.

  • :handle_otp_reports - if Erlang/OTP message should be logged. Defaults to true.

  • :handle_sasl_reports - if supervisor, crash, and progress reports should be logged. Defaults to false. This option only has an effect if :handle_otp_reports is true.

  • :metadata - global primary metadata to be included in all log messages. Defaults to []. This can be overridden at the process level with metadata/1 or each on log call as desired.

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

To configure the default formatter, for example, to use a different format and include some metadata:

config :logger, :default_formatter,
  format: "[$level] $message $metadata\n",
  metadata: [:error_code, :file]

Or to configure default handler, for instance, to log into a file with built-in support for log rotation and compression:

config :logger, :default_handler,
  config: [
    file: ~c"system.log",
    filesync_repeat_interval: 5000,
    file_check: 5000,
    max_no_bytes: 10_000_000,
    max_no_files: 5,
    compress_on_rotate: true
  ]

See :logger_std_h for all relevant configuration, including overload protection. Or set :default_handler to false to disable the default logging altogether:

config :logger, :default_handler, false

How to add new handlers is covered in later sections.

Keywords or maps

While Erlang's logger expects :config to be a map, Elixir's Logger allows the default handler configuration to be set with keyword lists. For example, this allows your config/*.exs files, such as config/dev.exs, to override individual keys defined in config/config.exs.

When reading the handler configuration using Erlang's APIs, the configuration will always be read (and written) as a map.

Compile configuration

The following configuration must be set via config files (such as config/config.exs) under the :logger application before your code is compiled:

  • :always_evaluate_messages - if messages should be evaluated even if the log level is lower than the minimum configured level. Defaults to false. This is useful for cases where the log level in your test environment is high (such as :error), which is common in order to avoid logs mixed with the test output. In such, cases, you might discover log messages that contain runtime errors only when your code is deployed to production, where the log level is lower (such as :info). These runtime errors could be caused by, for example, interpolating something that doesn't implement the String.Chars protocol in the log message, such as "PID: #{self()}" (since PIDs cannot be converted to strings with String.Chars).

  • :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 that Logger 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.

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,
  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. In addition to levels mentioned above it also supports 2 "meta-levels":

    • :all - all messages will be logged, conceptually identical to :debug
    • :none - no messages will be logged at all
  • :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 options in a config/config.exs file:

config :logger, level: :warning

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.

Erlang/OTP handlers

Handlers represent the ability to integrate into the logging system to handle each logged message/event. Elixir automatically configures the default handler, but you can use Erlang's :logger module to add other handlers too.

Erlang/OTP handlers must be listed under your own application. For example, to setup an additional handler, so you write to console and file:

config :my_app, :logger, [
  {:handler, :file_log, :logger_std_h, %{
     config: %{
       file: ~c"system.log",
       filesync_repeat_interval: 5000,
       file_check: 5000,
       max_no_bytes: 10_000_000,
       max_no_files: 5,
       compress_on_rotate: true
     },
     formatter: Logger.Formatter.new()
   }}
]

Each handler has the shape {:handler, name, handler_module, config_map}. Once defined, a handler can be explicitly attached in your Application.start/2 callback with add_handlers/1:

Logger.add_handlers(:my_app)

You can also develop your own handlers. Handlers run in the same process as the process logging the message/event. This gives developers flexibility but they should avoid performing 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.

Alternatively, you can use the :logger_backends project. It sets up a log handler with overload protection and allows incoming events to be dispatched to multiple backends.

Backends and backwards compatibility

Prior to Elixir v1.15, custom logging could be achieved with Logger backends. The main API for writing Logger backends have been moved to the :logger_backends project. However, the backends are still part of Elixir for backwards compatibility.

Important remarks:

  • If the :backends key is set and it doesn't have the :console entry, we assume that you want to disable the built-in logging. You can force logging by setting config :logger, :default_handler, []

  • The :console backend configuration is automatically maped to the default handler and default formatter. Previously, you would set:

    config :logger, :console,
      level: :error,
      format: "$time $message $metadata"

    This is now equivalent to:

    config :logger, :default_handler,
      level: :error
    
    config :logger, :default_formatter,
      format: "$time $message $metadata"

    All previous console configuration, except for :level, now go under :default_formatter.

  • If you want to use the previous :console implementation based on Logger Backends, you can still set backends: [Logger.Backends.Console] and place the configuration under config :logger, Logger.Backends.Console. Although consider using the :logger_backends project in such case, as Logger.Backends.Console itself will be deprecated in future releases

Summary

Functions

Adds a new backend.

Adds the handlers configured in the :logger application parameter of the given app.

Adds a new translator.

Compares log levels.

Configures the logger.

Configures the given backend.

Logs a critical message.

Returns the default formatter used by Logger.

Resets the logging level for all modules to the primary level.

Resets logging level for all modules in the given application to the primary level.

Resets the logging level for a given module to the primary level.

Resets logging level for the current process to the primary level.

disable(pid) deprecated

Disables logging for the current process.

Logs a emergency message.

enable(pid) deprecated

Enables logging for the current process.

enabled?(pid) deprecated

Returns whether the logging is enabled for a given process.

Flushes the logger.

Gets logging level for given module.

Gets logging level for the current process.

Retrieves the Logger level.

Logs a message with the given level.

Reads the current process metadata.

Alters the current process metadata according to the given keyword list.

Puts logging level for modules in a given application.

Puts logging level for given module.

Puts logging level for the current process.

Removes a backend.

Removes a translator.

Resets the current process metadata to the given keyword list.

Logs a warning message.

Types

@type level() ::
  :emergency
  | :alert
  | :critical
  | :error
  | :warning
  | :warn
  | :notice
  | :info
  | :debug
@type message() :: :unicode.chardata() | String.Chars.t() | report()
@type metadata() :: keyword()
@type report() :: map() | keyword()

Functions

Link to this function

add_backend(backend, opts \\ [])

View Source
This function is deprecated. Use LoggerBackends.add/2 from :logger_backends dependency.

Adds a new backend.

Link to this function

add_handlers(app)

View Source (since 1.15.0)
@spec add_handlers(atom()) :: :ok | {:error, term()}

Adds the handlers configured in the :logger application parameter of the given app.

This is used to register new handlers into the logging system. See the module documentation for more information.

Link to this function

add_translator(translator)

View Source
@spec add_translator({module(), function :: atom()}) :: :ok

Adds a new translator.

Link to this macro

alert(message_or_fun, metadata \\ [])

View Source (since 1.11.0) (macro)

Logs a alert message.

Returns :ok.

This is reported as "error" in Elixir's logger backends for backwards compatibility reasons.

Examples

Logging a message (string or iodata):

Logger.alert("this is a alert message")

Report message (maps or keywords):

# as keyword list
Logger.alert([something: :reported, this: :alert])

# as map
Logger.alert(%{this: :alert, something: :reported})
Link to this function

bare_log(level, message_or_fun, metadata \\ [])

View Source
@spec 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.

Link to this function

compare_levels(left, right)

View Source
@spec 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 if left is less than right
  • :eq if left and right are equal
  • :gt if left is greater than right

Examples

iex> Logger.compare_levels(:debug, :warning)
:lt
iex> Logger.compare_levels(:error, :info)
:gt
@spec 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.

Link to this function

configure_backend(backend, options)

View Source
This function is deprecated. Use LoggerBackends.configure/2 from :logger_backends dependency.

Configures the given backend.

Link to this macro

critical(message_or_fun, metadata \\ [])

View Source (since 1.11.0) (macro)

Logs a critical message.

Returns :ok.

This is reported as "error" in Elixir's logger backends for backwards compatibility reasons.

Examples

Logging a message (string or iodata):

Logger.critical("this is a critical message")

Report message (maps or keywords):

# as keyword list
Logger.critical([something: :reported, this: :critical])

# as map
Logger.critical(%{this: :critical, something: :reported})
Link to this macro

debug(message_or_fun, metadata \\ [])

View Source (macro)

Logs a debug message.

Returns :ok.

Examples

Logging a message (string or iodata):

Logger.debug("this is a debug message")

Report message (maps or keywords):

# as keyword list
Logger.debug([something: :reported, this: :debug])

# as map
Logger.debug(%{this: :debug, something: :reported})
Link to this function

default_formatter(overrides \\ [])

View Source (since 1.15.0)
@spec default_formatter(keyword()) :: {module(), :logger.formatter_config()}

Returns the default formatter used by Logger.

It returns a Logger.Formatter built on the :default_formatter configuration:

config :logger, :default_formatter,
  format: "\n$time $metadata[$level] $message\n",
  metadata: [:user_id]

In case of a list, a set of overrides can be given to merge into the list. See Logger.Formatter.new/1 for all options.

Examples

Logger will automatically load a default formatter into the default handler on boot. However, you can use this function if you wish to programatically replace a handler formatter. For example, inside tests, you might want to change the formatter settings:

setup tags do
  formatter = Logger.default_formatter(colors: [enabled: false])
  :logger.update_handler_config(:default, :formatter, formatter)

  on_exit(fn ->
    :logger.update_handler_config(:default, :formatter, Logger.default_formatter())
  end)
end

However, note you should not invoke this function inside config files, as this function expects Logger to already be configured and started. To start a brand new handler with this formatter, use Logger.Formatter.new/1 instead.

Link to this function

delete_all_module_levels()

View Source (since 1.11.0)
@spec delete_all_module_levels() :: :ok

Resets the logging level for all modules to the primary level.

Link to this function

delete_application_level(appname)

View Source (since 1.13.0)
@spec delete_application_level(application) ::
  :ok | {:error, {:not_loaded, application}}
when application: atom()

Resets logging level for all modules in the given application to the primary level.

Equivalent of:

appname |> Application.spec(:modules) |> Logger.delete_module_level()
Link to this function

delete_module_level(module)

View Source (since 1.11.0)
@spec delete_module_level(module() | [module()]) :: :ok

Resets the logging level for a given module to the primary level.

Link to this function

delete_process_level(pid)

View Source
@spec delete_process_level(pid()) :: :ok

Resets logging level for the current process to the primary level.

Currently the only accepted PID is self().

This function is deprecated. Use Logger.put_process_level(pid, :none) instead.
@spec disable(pid()) :: :ok

Disables logging for the current process.

Currently the only accepted PID is self().

Equivalent of:

put_process_level(pid, :none)
Link to this macro

emergency(message_or_fun, metadata \\ [])

View Source (since 1.11.0) (macro)

Logs a emergency message.

Returns :ok.

This is reported as "error" in Elixir's logger backends for backwards compatibility reasons.

Examples

Logging a message (string or iodata):

Logger.emergency("this is a emergency message")

Report message (maps or keywords):

# as keyword list
Logger.emergency([something: :reported, this: :emergency])

# as map
Logger.emergency(%{this: :emergency, something: :reported})
This function is deprecated. Use Logger.delete_process_level(pid) instead.
@spec enable(pid()) :: :ok

Enables logging for the current process.

Currently the only accepted PID is self().

Equivalent of:

delete_process_level(pid)
This function is deprecated. Use Logger.get_process_level(pid) instead.
@spec enabled?(pid()) :: boolean()

Returns whether the logging is enabled for a given process.

Currently the only accepted PID is self().

Link to this macro

error(message_or_fun, metadata \\ [])

View Source (macro)

Logs a error message.

Returns :ok.

Examples

Logging a message (string or iodata):

Logger.error("this is a error message")

Report message (maps or keywords):

# as keyword list
Logger.error([something: :reported, this: :error])

# as map
Logger.error(%{this: :error, something: :reported})
@spec flush() :: :ok

Flushes the logger.

This guarantees all log handlers are flushed. This is useful for testing and it should not be called in production code.

Link to this function

get_module_level(mod)

View Source (since 1.11.0)
@spec get_module_level(module() | [module()]) :: [{module(), level() | :all | :none}]

Gets logging level for given module.

The returned value will be the effective value used. If no value was set for a given module, then it will not be present in the returned list.

@spec get_process_level(pid()) :: level() | :all | :none | nil

Gets logging level for the current process.

Currently the only accepted PID is self().

The returned value will be the effective value used. If no value was set for a given process, then nil is returned.

Link to this macro

info(message_or_fun, metadata \\ [])

View Source (macro)

Logs a info message.

Returns :ok.

Examples

Logging a message (string or iodata):

Logger.info("this is a info message")

Report message (maps or keywords):

# as keyword list
Logger.info([something: :reported, this: :info])

# as map
Logger.info(%{this: :info, something: :reported})
@spec level() :: level()

Retrieves the Logger level.

The Logger level can be changed via configure/1.

Link to this macro

log(level, message_or_fun, metadata \\ [])

View Source (macro)

Logs a message with the given level.

Returns :ok.

The macros debug/2, info/2, notice/2, warning/2, error/2, critical/2, alert/2, and emergency/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).

@spec metadata() :: metadata()

Reads the current process metadata.

This does not return the "global" logger metadata (set via the :metadata key in the :logger application config), but only the process metadata.

@spec metadata(metadata()) :: :ok

Alters the current process metadata according to 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.

Note not all keys can be set as metadata. The metadata automatically added by Logger, as declared in the module documentation, will always override custom one.

Link to this macro

notice(message_or_fun, metadata \\ [])

View Source (since 1.11.0) (macro)

Logs a notice message.

Returns :ok.

This is reported as "info" in Elixir's logger backends for backwards compatibility reasons.

Examples

Logging a message (string or iodata):

Logger.notice("this is a notice message")

Report message (maps or keywords):

# as keyword list
Logger.notice([something: :reported, this: :notice])

# as map
Logger.notice(%{this: :notice, something: :reported})
Link to this function

put_application_level(appname, level)

View Source (since 1.13.0)
@spec put_application_level(atom(), level() | :all | :none) ::
  :ok | {:error, :not_loaded}

Puts logging level for modules in a given application.

This will take priority over the primary level set, so it can be used to increase or decrease verbosity of some parts of the project.

Equivalent of:

appname |> Application.spec(:modules) |> Logger.put_module_level(level)
Link to this function

put_module_level(mod, level)

View Source (since 1.11.0)
@spec put_module_level(module() | [module()], level() | :all | :none) ::
  :ok | {:error, term()}

Puts logging level for given module.

This will take priority over the primary level set, so it can be used to increase or decrease verbosity of some parts of the project.

Example

defmodule Foo do
  require Logger

  def log, do: Logger.debug("foo")
end

Logger.configure(level: :error)
Logger.put_module_level(Foo, :all)

Foo.log()
# This will print the message even if global level is :error
Link to this function

put_process_level(pid, level)

View Source
@spec put_process_level(pid(), level() | :all | :none) :: :ok

Puts logging level for the current process.

Currently the only accepted PID is self().

This will take priority over the primary level set, so it can be used to increase or decrease verbosity of some parts of the running system.

Link to this function

remove_backend(backend, opts \\ [])

View Source
This function is deprecated. Use LoggerBackends.remove/2 from :logger_backends dependency.

Removes a backend.

Link to this function

remove_translator(translator)

View Source
@spec remove_translator({module(), function :: atom()}) :: :ok

Removes a translator.

Link to this function

reset_metadata(keyword \\ [])

View Source
@spec reset_metadata(metadata()) :: :ok

Resets the current process metadata to the given keyword list.

Link to this macro

warn(message_or_fun, metadata \\ [])

View Source (macro)
This macro is deprecated. Use Logger.warning/2 instead.
Link to this macro

warning(message_or_fun, metadata \\ [])

View Source (since 1.11.0) (macro)

Logs a warning message.

Returns :ok.

Examples

Logging a message (string or iodata):

Logger.warning("this is a warning message")

Report message (maps or keywords):

# as keyword list
Logger.warning([something: :reported, this: :warning])

# as map
Logger.warning(%{this: :warning, something: :reported})