LoggerIexBackend v1.0.0 LoggerIexBackend

A Logger Backend for IEx interactive sessions.

LoggerIexBackend enables logging debugging in IEx interactive sessions. See start/0 and set_rules/1 to learn how to use this module.

Example

Given the following code example:

defmodule Example do
  require Logger

  def foo() do
    Logger.debug("A foo message")
  end

  defmodule Bar do
    require Logger

    def bar() do
      Logger.info("A bar message")
    end
  end
end

We can use LoggerIexBackend in a interactive shell like this:

iex> Example.foo()
00:00:01.000 [debug] A foo message
:ok
iex> LoggerIexBackend.start()
{:ok, #PID<0.42.0>}
iex> Example.foo()
:ok # NOTE that by default, all logs are disabled by LoggerIexBackend
iex> LoggerIexBackend.set_rules(allow: :info)
:ok
iex> Example.foo()
:ok # No logs here yet
iex> Example.Bar.bar()
:ok
00:00:02.000 [info]  A bar message
iex> LoggerIexBackend.set_rules(allow: ~r/foo/) # Enable logs by message
iex> LoggerIexBackend.set_rules(allow: [module: Example]) # Enable logs by module
:ok
iex> Example.foo()
:ok
00:00:03.000 [debug] A foo message

Link to this section Summary

Types

A condition used to filter logs.

A predicate to allow or disallow a set of log entries.

A list of rule/0s.

Functions

Set the current rules for log filtering.

Starts the IEx Logger Backend.

Link to this section Types

Link to this type

argument()

argument() ::
  :all
  | Logger.level()
  | [Logger.level()]
  | String.t()
  | Regex.t()
  | {atom(), Regex.t()}
  | {atom(), term()}

A condition used to filter logs.

Any of the following arguments can be used as rule to allow or disallow matching logs:

  • all: matches all logs.
  • :debug, :info, :error, etc. Matches the specified log level.
  • A list of the previous values.
  • A string contained in log's messages.
  • A regular expression to test log's messages.
  • A tuple to match any other metadata value.

Examples

# Enable all logs:
LoggerIexBackend.set_rules(allow: :all)

# Enable logs by level:
LoggerIexBackend.set_rules(allow: :debug)
LoggerIexBackend.set_rules(allow: [:warn, :error])

# Enable logs by messages:
LoggerIexBackend.set_rules(allow: "foo") # All messages containing foo.
LoggerIexBackend.set_rules(allow: ~r/foo/)

# Enable logs by metadata:
LoggerIexBackend.set_rules(allow: {:module, MyModule})
LoggerIexBackend.set_rules(allow: {:file, "my_file.ex"})
Link to this type

rule()

rule() :: {:allow, argument()} | {:disallow, argument()}

A predicate to allow or disallow a set of log entries.

Link to this type

rules()

rules() :: [rule()]

A list of rule/0s.

Link to this section Functions

Link to this function

set_rules(rule_or_rules)

set_rules(rule() | rules()) :: :ok

Set the current rules for log filtering.

This function will remove previous rules for filtering log messages and apply the given new ones. More information about the kind of rules can be found at argument/0.

Link to this function

start()

start() :: :ok | {:error, term()}

Starts the IEx Logger Backend.

This function will stop the current console backend, if enabled, and start the IEx Backend. All logs will be filtered out by default. Use set_rules/1 to allow log messages to be printed.