File Coverage

blib/lib/Mojolicious/Plugin/Log/Any.pm
Criterion Covered Total %
statement 8 8 100.0
branch n/a
condition 2 5 40.0
subroutine 2 2 100.0
pod 1 1 100.0
total 13 16 81.2


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Log::Any;
2              
3 1     1   923 use Mojo::Base 'Mojolicious::Plugin';
  1         2  
  1         8  
4              
5             our $VERSION = 'v1.0.2';
6              
7             sub register {
8 2     2 1 9243 my ($self, $app, $conf) = @_;
9            
10 2   50     12 my $logger = delete $conf->{logger} // 'Log::Any';
11 2         6 my %opt = %$conf;
12 2   33     16 $opt{category} //= ref $app;
13            
14 2         14 $app->log->with_roles('Mojo::Log::Role::AttachLogger')->level('debug')
15             ->unsubscribe('message')->attach_logger($logger, \%opt);
16             }
17              
18             1;
19              
20             =head1 NAME
21              
22             Mojolicious::Plugin::Log::Any - Use other loggers in a Mojolicious application
23              
24             =head1 SYNOPSIS
25              
26             package MyApp;
27             use Mojo::Base 'Mojolicious';
28            
29             sub startup {
30             my $self = shift;
31            
32             # Log::Any (default)
33             use Log::Any::Adapter {category => 'MyApp'}, 'Syslog';
34             $self->plugin('Log::Any');
35            
36             # Log::Contextual
37             use Log::Contextual::WarnLogger;
38             use Log::Contextual -logger => Log::Contextual::WarnLogger->new({env_prefix => 'MYAPP'});
39             $self->plugin('Log::Any' => {logger => 'Log::Contextual});
40            
41             # Log::Dispatch
42             use Log::Dispatch;
43             my $logger = Log::Dispatch->new(outputs => ['File::Locked',
44             min_level => 'warning',
45             filename => '/path/to/file.log',
46             mode => 'append',
47             newline => 1,
48             callbacks => sub { my %p = @_; '[' . localtime() . '] ' . $p{message} },
49             ]);
50             $self->plugin('Log::Any' => {logger => $logger});
51            
52             # Log::Dispatchouli
53             use Log::Dispatchouli;
54             my $logger = Log::Dispatchouli->new({ident => 'MyApp', facility => 'daemon', to_file => 1});
55             $self->plugin('Log::Any' => {logger => $logger});
56            
57             # Log::Log4perl
58             use Log::Log4perl;
59             Log::Log4perl->init($self->home->child('log.conf')->to_string);
60             $self->plugin('Log::Any' => {logger => 'Log::Log4perl'});
61             }
62            
63             # or in a Mojolicious::Lite app
64             use Mojolicious::Lite;
65             use Log::Any::Adapter {category => 'Mojolicious::Lite'}, File => app->home->child('myapp.log'), log_level => 'info';
66             plugin 'Log::Any';
67              
68             =head1 DESCRIPTION
69              
70             L is a L plugin that redirects the
71             application logger to pass its log messages to an external logging framework
72             using L. By default, L
73             is used, but a different framework or object may be specified. For L
74             or L, log messages are dispatched with a category of the
75             application class name, which is C for lite applications.
76              
77             The default behavior of the L object to filter messages by level,
78             keep history, prepend a timestamp, and write log messages to a file or STDERR
79             will be suppressed, by setting the application log level to C (the
80             lowest level) and removing the default L handler. It is
81             expected that the logging framework output handler will be configured to handle
82             these details as necessary. If you want to customize how the logging framework
83             is attached, use L directly.
84              
85             =head1 METHODS
86              
87             L inherits all methods from
88             L and implements the following new ones.
89              
90             =head2 register
91              
92             $plugin->register(Mojolicious->new);
93             $plugin->register(Mojolicious->new, {logger => $logger});
94              
95             Register logger in L application. Takes the following options:
96              
97             =over
98              
99             =item logger
100              
101             Logging framework or object to pass log messages to, of a type recognized by
102             L. Defaults to C.
103              
104             =item category
105              
106             Passed through to L. Defaults to
107             the application name.
108              
109             =item prepend_level
110              
111             Passed through to L.
112              
113             =back
114              
115             =head1 BUGS
116              
117             Report any issues on the public bugtracker.
118              
119             =head1 AUTHOR
120              
121             Dan Book
122              
123             =head1 COPYRIGHT AND LICENSE
124              
125             This software is Copyright (c) 2017 by Dan Book.
126              
127             This is free software, licensed under:
128              
129             The Artistic License 2.0 (GPL Compatible)
130              
131             =head1 SEE ALSO
132              
133             L, L