Mojo::Log::Role::AttachLogger - Use other loggers for Mojo::Log
use Mojo::Log; my $log = Mojo::Log->with_roles('+AttachLogger')->new->unsubscribe('message'); # 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');
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.
Log::Any
Log::Contextual
Log::Log4perl
Log::Any::Proxy
Log::Dispatch
Log::Dispatchouli
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".
Mojolicious::Plugin::Log::Any can be used to attach a logger to the Mojolicious application logger and suppress the default message event handler.
Mojo::Log::Role::AttachLogger composes the following methods.
$log = $log->attach_logger($logger, $category);
Subscribes to "message" in Mojo::Log 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 log level will be prepended to the message in square brackets (except when passing to another Mojo::Log object). The following loggers are recognized:
The string Log::Any will use a global Log::Any logger with the specified category (defaults to Mojo::Log).
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.
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.
A Log::Dispatch object can be passed to be used for logging. The fatal log level will be mapped to critical.
fatal
critical
A Log::Dispatchouli object can be passed to be used for logging. The fatal log level will log messages even if the object is muted, but an exception will not be thrown as "log_fatal" in Log::Dispatchouli normally does.
muted
The string Log::Log4perl will use a global Log::Log4perl logger with the specified category (defaults to Mojo::Log).
Another Mojo::Log object can be passed to be used for logging.
Report any issues on the public bugtracker.
Dan Book <dbook@cpan.org>
This software is Copyright (c) 2017 by Dan Book.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
Mojo::Log, Log::Any, Log::Contextual, Log::Dispatch, Log::Dispatchouli, Log::Log4perl
To install Mojolicious::Plugin::Log::Any, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::Log::Any
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::Log::Any
For more information on module installation, please visit the detailed CPAN module installation guide.