Logbook (logbook v4.0.0)
View SourceA 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")
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.
Types
Functions
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.
Like enabled?/2
checks if the given module has a configured log level equal
or lower than the given level.
@spec reset() :: :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.