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', 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::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::Dispatchouli
  use Log::Dispatchouli;
  my $logger = Log::Dispatchouli->new({ident => 'MyApp', facility => 'daemon', to_file => 1});
  # Log::Log4perl
  use Log::Log4perl;
  $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.

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.

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, $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:


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.


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.


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.

The following options are supported:


Category name (defaults to Mojo::Log).


Prepend the log level to messages in the form [$level] (default for non-Mojo::Log loggers). Set false to disable.


String to separate multiple messages. Defaults to newline.


Report any issues on the public bugtracker.


Dan Book <>


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