File Coverage

blib/lib/Mojolicious/Plugin/Log/Any.pm
Criterion Covered Total %
statement 15 15 100.0
branch 1 2 50.0
condition 2 5 40.0
subroutine 2 2 100.0
pod 1 1 100.0
total 21 25 84.0


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