Timber v2.8.1 Timber.Integrations.ErrorLogger View Source

Handles error reports from the :error_logger application.

Timber can automatically log the exceptions that occur in your application as exception events with all the necessary metadata by registering as an :error_logger handler. To activate Timber’s error logging system, you need to add a few configuration lines and add the ErrorLogger handler during application start:

# Enable Timber's error capturing system
config :timber, :capture_errors, true

# Disable Elixir's default error capturing system
config :logger, :handle_otp_reports, false
def start(_type, _opts) do
  children = [
    supervisor(MyApp.Repo, []),
    supervisor(MyAppWeb.Endpoint, [])
  ]

  opts = [strategy: :one_for_one, name: MyApp.Supervisor]

  :ok = :error_logger.add_report_handler(Timber.Integrations.ErrorLogger)

  Supervisor.start_link(children, opts)
end

Users should be aware that report handlers can be added multiple times, which can result in duplicated logs. This concern is most relevant to umbrella applications or applications with multiple entry-points. To solve this, the handler should be added only at the primary application entry-point. If that is not feasible, the other solution is to check if the handler exists before adding it. Example:

if !(Timber.Integrations.ErrorLogger in :gen_event.which_handlers(:error_logger)) do
  :ok = :error_logger.add_report_handler(Timber.Integrations.ErrorLogger)
end

Elixir Logger’s OTP Report Handler

The Logger application (which is distributed with Elixir) has an OTP report handler which logs errors and will activate it by default. However, the logs it writes only contain textual information without any metadata. Keeping this handler active will cause duplicate errors to be reported to the log, which is why we recommend disabling it using the following configuration option:

config :logger, :handle_otp_reports, false

However, the OTP report handler handles additional report types as well. If you find that you would like these reports to be logged, just be aware that every exception will be displayed twice in the log. This module also consolidates many error reports into a single line that by default would be logged across multiple lines.

Since the OTP report handler does not add the requisite metadata, Timber’s console will not identify the errors it logs as exception events when you search.

Elixir Logger’s SASL Report Handler

The Elixir Logger application also comes with a SASL (System Architecture Support Libraries) report handler. Timber does not currently handle these reports, so activating the standard handler will not cause duplicate logs.

:error_logger Output

When Timber’s error capturing system is activated, it will also disable :error_logger’s tty output. In most cases, this is what you want, otherwise, otherwise it will print out reports to the tty in a plain text (and rather ugly) format.

If you do not want the tty output to be disabled, you can keep it on using the following config:

config :timber, :disable_kernel_error_tty, false

Link to this section Summary

Functions

Callback implementation for c::gen_event.code_change/3

Callback implementation for c::gen_event.handle_call/2

Callback implementation for c::gen_event.handle_info/2

Callback implementation for c::gen_event.terminate/2

Link to this section Functions

Link to this function code_change(old, state, extra) View Source

Callback implementation for c::gen_event.code_change/3.

Link to this function get_metadata(arg1, arg2) View Source

Callback implementation for c::gen_event.handle_call/2.

Callback implementation for c::gen_event.handle_info/2.

Link to this function terminate(reason, state) View Source

Callback implementation for c::gen_event.terminate/2.