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
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.
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.
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. Seeformatter()
.
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.
Type for timestamps
Link to this section 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
.