NAME
Mojo::Log::Role::AttachLogger - Use other loggers for Mojo::Log
SYNOPSIS
use Mojo::Log;
my $log = Mojo::Log->with_roles('+AttachLogger')->new->unsubscribe('message');
# Log::Any
use Log::Any::Adapter {category => 'Mojo::Log', message_separator => ' '}, 'Syslog';
$log->attach_logger('Log::Any', 'Some::Category');
# Log::Contextual
use Log::Contextual::WarnLogger;
use Log::Contextual -logger => Log::Contextual::WarnLogger->new({env_prefix => 'MYAPP'});
$log->attach_logger('Log::Contextual');
# Log::Dispatch
use Log::Dispatch;
my $logger = Log::Dispatch->new(outputs => ['File::Locked',
min_level => 'warning',
filename => '/path/to/file.log',
mode => 'append',
newline => 1,
callbacks => sub { my %p = @_; '[' . localtime() . '] ' . $p{message} },
]);
$log->attach_logger($logger);
# Log::Dispatchouli
use Log::Dispatchouli;
my $logger = Log::Dispatchouli->new({ident => 'MyApp', facility => 'daemon', to_file => 1});
$log->attach_logger($logger);
# Log::Log4perl
use Log::Log4perl;
Log::Log4perl->init('/path/to/log.conf');
$log->attach_logger('Log::Log4perl', 'Some::Category');
DESCRIPTION
Mojo::Log::Role::AttachLogger is a Role::Tiny role for Mojo::Log that redirects log messages to an external logging framework. "attach_logger" currently recognizes the strings Log::Any
, Log::Contextual
, Log::Log4perl
, and objects of the classes Log::Any::Proxy
, Log::Dispatch
, Log::Dispatchouli
, and Mojo::Log
.
The default "message" in Mojo::Log event handler is not suppressed by "attach_logger", so if you want to suppress the default behavior, you should unsubscribe from the message event first. Unsubscribing from the message event will also remove any loggers attached by "attach_logger".
Since Mojolicious 8.06, the "message" in Mojo::Log event will not be sent for messages below the log level set in the Mojo::Log object, so the attached logger will only receive log messages exceeding the configured level.
Since Mojolicious 9.20, the trace
log level is supported though it may be mapped to debug
on some loggers.
Mojolicious::Plugin::Log::Any can be used to attach a logger to the Mojolicious application logger and suppress the default message event handler.
METHODS
Mojo::Log::Role::AttachLogger composes the following methods.
attach_logger
$log = $log->attach_logger($logger, $options);
Subscribes to "message" in Mojo::Log and passes log messages to the given logging framework or object. The second argument is optionally a category (default Mojo::Log
) or hashref of options. The log level will be prepended to the message in square brackets (except when passing to another Mojo::Log object, or "prepend_level" is false).
The following loggers are recognized:
- Log::Any
-
The string
Log::Any
will use a global Log::Any logger with the specified category (defaults toMojo::Log
). - Log::Any::Proxy
-
A Log::Any::Proxy object can be passed directly and will be used for logging in the standard manner, using the object's existing category.
- Log::Contextual
-
The string
Log::Contextual
will use the global Log::Contextual logger. Package loggers are not supported. Note that "with_logger" in Log::Contextual may be difficult to use with Mojolicious logging due to the asynchronous nature of the dispatch cycle. - Log::Dispatch
-
A Log::Dispatch object can be passed to be used for logging. The
fatal
log level will be mapped tocritical
, and thetrace
anddebug
log levels will both be mapped todebug
. - Log::Dispatchouli
-
A Log::Dispatchouli object can be passed to be used for logging. The
fatal
log level will log messages even if the object ismuted
, but an exception will not be thrown as "log_fatal" in Log::Dispatchouli normally does. Thetrace
anddebug
log levels will be logged with "log_debug" in Log::Dispatchouli. - Log::Log4perl
-
The string
Log::Log4perl
will use a global Log::Log4perl logger with the specified category (defaults toMojo::Log
). - Mojo::Log
-
Another Mojo::Log object can be passed to be used for logging.
The following options are supported:
- category
-
Category name (defaults to Mojo::Log).
- prepend_level
-
Prepend the log level to messages in the form
[$level]
(default for non-Mojo::Log loggers). Set false to disable. - message_separator
-
String to separate multiple messages. Defaults to newline.
BUGS
Report any issues on the public bugtracker.
AUTHOR
Dan Book <dbook@cpan.org>
COPYRIGHT AND LICENSE
This software is Copyright (c) 2017 by Dan Book.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
SEE ALSO
Mojo::Log, Log::Any, Log::Contextual, Log::Dispatch, Log::Dispatchouli, Log::Log4perl