View Source Logbook (logbook v2.0.3)

A category (or tags) based logger for Elixir.

Logbook is a wrapper aroud Elixir Logger that enables a to specify one or more tags for each invocation in order to be able to set different log levels for each tag.

Tagging logs is useful to track log informations around different modules and enable only one (or more) specific tags at different log levels than the default Logger instead of having (for example) all debug logs enabled.

In the following example when calling delete_all and having the :audit tag level set to at least :info, both "Deleting user..." and "Deleting domain" logs will be produced. If only :domain or :user tags have log level set to :info only the corresponding logs will be produced.

## Example
require Logbook

def delete_user(user) do
  Logbook.info([:audit, :user], "Deleting user #{inspect(user)}")
  # ...
end

def delete_domain(domain) do
  Logbook.info([:audit, :domain], "Deleting domain #{inspect(domain)}")
  # ...
end

def delete_all(user, domain) do
  delete_domain(domain)
  delete_user(user)
end

Log levels for each tag can be set using Logbook.set_level/2:

  # For a single tag
  Logbook.set_level(:audit, :info)

  # or for many tags at once
  Logbook.set_level([:audit, :user, :domain], :info)

Is possible to set the default level for all tags, by setting the :default_tag_level config option for :logbook app (defaults to :warning):

  import Config

  config :logbook, :default_tag_level, :warning

The :default_tag_level option is used when Logbook sees tags for the first time during runtime and set them internally with the above level.

As a bonus, Logbook also creates a module-level tag automatically, in order to be able to enable log statements at once in a single module:

defmodule Foo
  require Logbook

  def delete_user(user) do
    Logbook.info([:audit, :user], "Deleting user #{inspect(user)}")
    # ...
  end

  def delete_domain(domain) do
    Logbook.info([:audit, :domain], "Deleting domain #{inspect(domain)}")
    # ...
  end
end

With the above example is possible to Logbook.set_level(Foo, :info) to enable all Logbook calls inside the module Foo.

As with :default_tag_level is possible to set also default module-level logging with:

  import Config

  config :logbook, :default_module_level, :warning

By default :default_module_level is set to :none (no module-level logging).

Logbook supports all Logger levels, along with the additional :none level that disables it for the specified tag/module.

Being a wrapper for Logger, if the Logger log level is less that Logbook log level, the logs will not be produced, because are filtered by Logger log levels. Example:

  Logger.configure(level: :warning)
  Logbook.set_level(:foo, :debug)

  Logbook.debug(:foo, "This will not be emitted")

Link to this section Summary

Functions

Checks wheter the tag has the specified log level equal o higher than the configured one.

Like enabled?/2 checks if the given module has a configured log level equal or lower than the given level.

Sets log level for the specific tag or list of tags.

Returns a map containing the tags/modules seen at runtime with the corresponding configured log level. This list is built at runtime, so if a Logbook loggin fun has never be called, the corresponding tag will not be shown here.

Link to this section Types

@type level() ::
  :emergency
  | :alert
  | :critical
  | :error
  | :warn
  | :warning
  | :notice
  | :info
  | :debug
  | :none
@type tag_or_tags() :: atom() | [atom()]

Link to this section Functions

Link to this macro

alert(tag_or_tags, chardata_or_fun, metadata \\ [])

View Source (since 2.0.0) (macro)
Link to this macro

critical(tag_or_tags, chardata_or_fun, metadata \\ [])

View Source (since 2.0.0) (macro)
Link to this macro

debug(tag_or_tags, chardata_or_fun, metadata \\ [])

View Source (since 2.0.0) (macro)
Link to this macro

emergency(tag_or_tags, chardata_or_fun, metadata \\ [])

View Source (since 2.0.0) (macro)
Link to this function

enabled?(tag_or_tags, level)

View Source
@spec enabled?(tag_or_tags(), level()) :: boolean()

Checks wheter the tag has the specified log level equal o higher than the configured one.

iex> Logbook.set_level(:foo, :info)
:ok
iex> Logbook.enabled?(:foo, :debug)
false
iex> Logbook.enabled?(:foo, :warning)
true

If a list of tags is passed, returns true if any of the tag log level is equal or lower than the passed one.

Link to this macro

error(tag_or_tags, chardata_or_fun, metadata \\ [])

View Source (since 2.0.0) (macro)
Link to this macro

info(tag_or_tags, chardata_or_fun, metadata \\ [])

View Source (since 2.0.0) (macro)
Link to this function

module_enabled?(module, level)

View Source
@spec module_enabled?(module(), level()) :: boolean()

Like enabled?/2 checks if the given module has a configured log level equal or lower than the given level.

Link to this macro

notice(tag_or_tags, chardata_or_fun, metadata \\ [])

View Source (since 2.0.0) (macro)
@spec reset() :: :ok
Link to this function

set_level(tag_or_tags, level)

View Source
@spec set_level(tag_or_tags(), level()) :: :ok

Sets log level for the specific tag or list of tags.

@spec tags() :: map()

Returns a map containing the tags/modules seen at runtime with the corresponding configured log level. This list is built at runtime, so if a Logbook loggin fun has never be called, the corresponding tag will not be shown here.

Link to this macro

warn(tag_or_tags, chardata_or_fun, metadata \\ [])

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

warning(tag_or_tags, chardata_or_fun, metadata \\ [])

View Source (since 2.0.0) (macro)