View Source Logger (Logger v1.18.0-dev)

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.

  • Integrate with Erlang's :logger and support custom filters and handlers.

  • Formats and truncates messages on the client to avoid clogging Logger handlers.

  • Provides multiple forms of overload protection:

    • keeps track of its message queue and switches to sync mode to apply back pressure or even drop messages
    • limits the number of logs emitted defaulting to 500 per second
    • optionally allows to terminate and restart it if the message queue length or memory thresholds are exceeded
  • 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 handlers. 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, may be 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

  • :process_label - (available from Erlang/OTP 27+) an arbitrary term which can be added to a process with Process.set_label/1 for debugging purposes

  • :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 default formatter ignores this metadata by default but it can be useful to certain handlers, 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.

The metadata keys above may not always be available. The :mfa, :file, :line, and similar metadata are automatically included when using Logger macros, but not when using Logger.bare_log/3. Other metadata, such as :crash_reason, :initial_call, and :registered_name are available only inside behaviours such as GenServer, Supervisor, and others.

It is also possible to pass metadata on a particular Logger invocation. 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 - key-value pairs of global primary metadata to be included in all log messages. Defaults to []. The default formatter writes to standard out and therefore cannot print all metadata. See Logger.Formatter's documentation for more information.

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
  ]

You can find a complete reference on all handler options on Erlang/OTP docs. Here is Elixir's default configuration for the default handler:

[
  # Do not log messages from other nodes
  filters: [{&:logger_filters.remote_gl/2, :stop}],
  filter_default: :log,
  formatter: &Logger.default_formatter/0,
  level: :all,
  module: :logger_std_h
]

The :config customizes a specific handler module. The default handler is :logger_std_h, which logs to standard IO, and you call find all relevant configuration in its module documentation, including information overload protection.

You may also set :default_handler to false to disable the default logging altogether:

config :logger, :default_handler, false

How to add more handlers besides the default one 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 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 handler 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's Logger automatically sets a default handler based on Erlang's :logger_std_h, which you can configure using the :default_handler boot configuration outlined above. You may also attach additional handlers when you boot your application.

To do so, you must list a series of handlers under the :logger key of your application configuration. For example, to setup an additional handler that writes to a 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 add, remove, and update handlers at runtime with the help of the Erlang's :logger module.

You may 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.

Filtering

You can add filters to any handler. For example, to filter out logs that contain a particular string, you could create a module:

defmodule LogFilter do
  def filter(log_event, _opts) do
    case log_event do
      %{msg: msg} when is_binary(msg) ->
        if msg =~ "password" do
          :stop
        else
          :ignore
        end

      _ ->
        :ignore
    end
  end
end

It may return :log (to log the message), :stop (to not log the message), or :ignore (to ignore the filter).

Then you can attach the filter, either as a primary filter (which applies to all handlers), or to a specific handler, when you start your application, such as in the Application.start/2 callback:

:logger.add_primary_filter(:word_filter, {&LogFilter.filter/2, []})

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 API is 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 mapped 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 cases, as Logger.Backends.Console itself will be deprecated in future releases

  • Logger.Backends only receive :debug, :info, :warning, and :error messages. :notice maps to :info. :warn maps to :warnings. All others map to :error

Summary

Functions

Adds a new backend.

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

Adds a new translator.

Logs an alert message.

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 an emergency message.

enable(pid) deprecated

Enables logging for the current process.

enabled?(pid) deprecated

Returns whether the logging is enabled for a given process.

Logs an error message.

Flushes the logger.

Gets logging level for given module.

Gets logging level for the current process.

Logs an info message.

Retrieves the Logger level.

Returns all the available levels.

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

level()

@type level() ::
  :emergency
  | :alert
  | :critical
  | :error
  | :warning
  | :warn
  | :notice
  | :info
  | :debug

message()

@type message() :: :unicode.chardata() | String.Chars.t() | report()

metadata()

@type metadata() :: keyword()

report()

@type report() :: map() | keyword()

Functions

add_backend(backend, opts \\ [])

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

Adds a new backend.

add_handlers(app)

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

add_translator(translator)

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

Adds a new translator.

alert(message_or_fun, metadata \\ [])

(since 1.11.0) (macro)

Logs an alert message.

Returns :ok.

Examples

Logging a message (string or iodata):

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

Report message (maps or keywords):

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

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

Report message with metadata (maps or keywords):

# as a keyword list
Logger.alert("this is an alert message", [user_id: 42, request_id: "xU32kFa"])

# as map
Logger.alert("this is an alert message", %{user_id: 42, request_id: "xU32kFa"})

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

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

compare_levels(left, right)

@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

configure(options)

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

configure_backend(backend, options)

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

Configures the given backend.

critical(message_or_fun, metadata \\ [])

(since 1.11.0) (macro)

Logs a critical message.

Returns :ok.

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})

Report message with metadata (maps or keywords):

# as a keyword list
Logger.critical("this is a critical message", [user_id: 42, request_id: "xU32kFa"])

# as map
Logger.critical("this is a critical message", %{user_id: 42, request_id: "xU32kFa"})

debug(message_or_fun, metadata \\ [])

(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})

Report message with metadata (maps or keywords):

# as a keyword list
Logger.debug("this is a debug message", [user_id: 42, request_id: "xU32kFa"])

# as map
Logger.debug("this is a debug message", %{user_id: 42, request_id: "xU32kFa"})

default_formatter(overrides \\ [])

(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 programmatically 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.

delete_all_module_levels()

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

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

delete_application_level(appname)

(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()

delete_module_level(module)

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

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

delete_process_level(pid)

(since 1.15.0)
@spec delete_process_level(pid()) :: :ok

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

Currently the only accepted PID is self().

disable(pid)

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)

emergency(message_or_fun, metadata \\ [])

(since 1.11.0) (macro)

Logs an emergency message.

Returns :ok.

Examples

Logging a message (string or iodata):

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

Report message (maps or keywords):

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

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

Report message with metadata (maps or keywords):

# as a keyword list
Logger.emergency("this is an emergency message", [user_id: 42, request_id: "xU32kFa"])

# as map
Logger.emergency("this is an emergency message", %{user_id: 42, request_id: "xU32kFa"})

enable(pid)

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)

enabled?(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().

error(message_or_fun, metadata \\ [])

(macro)

Logs an error message.

Returns :ok.

Examples

Logging a message (string or iodata):

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

Report message (maps or keywords):

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

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

Report message with metadata (maps or keywords):

# as a keyword list
Logger.error("this is an error message", [user_id: 42, request_id: "xU32kFa"])

# as map
Logger.error("this is an error message", %{user_id: 42, request_id: "xU32kFa"})

flush()

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

get_module_level(mod)

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

get_process_level(pid)

(since 1.15.0)
@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.

info(message_or_fun, metadata \\ [])

(macro)

Logs an info message.

Returns :ok.

Examples

Logging a message (string or iodata):

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

Report message (maps or keywords):

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

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

Report message with metadata (maps or keywords):

# as a keyword list
Logger.info("this is an info message", [user_id: 42, request_id: "xU32kFa"])

# as map
Logger.info("this is an info message", %{user_id: 42, request_id: "xU32kFa"})

level()

@spec level() :: level() | :all | :none

Retrieves the Logger level.

The Logger level can be changed via configure/1.

levels()

(since 1.16.0)
@spec levels() :: [level(), ...]

Returns all the available levels.

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

(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).

metadata()

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

metadata(keyword)

@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 some metadata keys are reserved and cannot be overridden. See the module documentation for more information.

notice(message_or_fun, metadata \\ [])

(since 1.11.0) (macro)

Logs a notice message.

Returns :ok.

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})

Report message with metadata (maps or keywords):

# as a keyword list
Logger.notice("this is a notice message", [user_id: 42, request_id: "xU32kFa"])

# as map
Logger.notice("this is a notice message", %{user_id: 42, request_id: "xU32kFa"})

put_application_level(appname, level)

(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)

put_module_level(mod, level)

(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

put_process_level(pid, level)

(since 1.15.0)
@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.

remove_backend(backend, opts \\ [])

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

Removes a backend.

remove_translator(translator)

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

Removes a translator.

reset_metadata(keyword \\ [])

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

Resets the current process metadata to the given keyword list.

warn(message_or_fun, metadata \\ [])

(macro)
This macro is deprecated. Use Logger.warning/2 instead.

warning(message_or_fun, metadata \\ [])

(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})

Report message with metadata (maps or keywords):

# as a keyword list
Logger.warning("this is a warning message", [user_id: 42, request_id: "xU32kFa"])

# as map
Logger.warning("this is a warning message", %{user_id: 42, request_id: "xU32kFa"})