NAME
Mojo::Log::Role::AttachLogger - Use other loggers for Mojo::Log
SYNOPSIS
use Mojo::Log;
my $log = Mojo::Log->with_roles('+AttachLogger')->new;
# Log::Any
use Log::Any::Adapter {category => 'Mojo::Log'}, '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 behavior of the Mojo::Log object to filter messages by level, keep history, prepend a timestamp, and write log messages to a file or STDERR will be suppressed. It is expected that the logging framework output handler will be configured to handle these details as necessary. The log level, however, will be prepended to the message in brackets before passing it on (except when passing to another Mojo::Log object which normally does this).
Mojolicious::Plugin::Log::Any can be used to apply this role and attach a logger to the Mojolicious application logger.
METHODS
Mojo::Log::Role::AttachLogger composes the following methods.
attach_logger
$log = $log->attach_logger($logger, $category);
Suppresses the default logging behavior and passes log messages to the given logging framework or object, with an optional category for Log::Any and Log::Log4perl (defaults to Mojo::Log
). 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
. - 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. - 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.
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