uni_logger_backend v0.1.0 UniLoggerBackend

A logger backend that forwards log messages to a process.

Usage

First add the logger to the backends:

# config/config.exs

config :logger, :backends, [{UniLoggerBackend, :console}]

config :logger, level: :info

Then configure the pid of the process that should receive the log messages by configuring the backend at runtime. This can be done for example from a GenServer that should receive the log messages:

Logger.configure_backend({UniLoggerBackend, :console}, pid: self())

receive do
  {level, msg, timestamp, meta} -> IO.puts "Received log"
  :flush -> IO.puts "Received flush"
end

The registered process will then receive messages when the logger is invoked. Therefore the registered process should implement handle_info/2 for tuples like {level, msg, timestamp, meta} and for :flush. :flush is received when the logger is flushed by calling Logger.flush/0.

Link to this section Summary

Types

A formatter to format the log msg before sending. It can be either a function or a tuple with a module and a function name

Type for log levels

Type for metadata

Type for messages

Options to configure the backend

Collection type for opt

Serves as internal state of the UniLoggerBackend and as config

Type for targes

Type for timestamps

Functions

Callback implementation for c::gen_event.handle_call/2

Callback implementation for c::gen_event.handle_event/2

Callback implementation for c::gen_event.init/1

Link to this section Types

Link to this type formatter()
formatter() ::
  {module(), atom()} | (level(), msg(), timestamp(), metadata() -> any())

A formatter to format the log msg before sending. It can be either a function or a tuple with a module and a function name.

The functions receives the log msg, a timestamp as a erlang time tuple and the metadata as arguments and should return the formatted log msg.

Link to this type level()
level() :: Logger.level()

Type for log levels

Link to this type metadata()
metadata() :: Logger.metadata()

Type for metadata

Link to this type msg()
msg() :: any()

Type for messages

Link to this type opt()
opt() ::
  {:level, level()}
  | {:target, target()}
  | {:meta, metadata()}
  | {:formatter, formatter()}

Options to configure the backend

Link to this type opts()
opts() :: [opt()]

Collection type for opt

Link to this type state()
state() :: %UniLoggerBackend.Config{
  formatter: nil | formatter(),
  level: level(),
  metadata: metadata(),
  name: atom(),
  target: target()
}

Serves as internal state of the UniLoggerBackend and as config.

  • level - Specifies the log level.
  • target - Specifies the target for the log messages.
  • meta - Additional metadata that will be added to the metadata before formatting.
  • name - The name of the lggger. This cannot be overridden.
  • formatter - A optional function that is used to format the log messages before sending. See formatter().
Link to this type target()
target() ::
  GenServer.name() | (level(), msg(), timestamp(), metadata() -> any())

Type for targes.

A target can either be a pid, a registered process name or a function with arity 4. The function receives the log level, the message, a timestamp and the metadata as arguments. Processes need to implement handle_info/2 and with receive the same info as a tuple. Processes are also expected to implement handle_info/2 for :flush messages. These messages are intended to flush the all pending messages.

Link to this type timestamp()
timestamp() :: Logger.Formatter.time()

Type for timestamps

Link to this section Functions

Link to this function handle_call(arg, map)

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

Link to this function handle_event(arg1, state)

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

Link to this function init(arg)
init({module(), atom()}) :: {:ok, state()}

Callback implementation for c::gen_event.init/1.