Mojolicious/Plugin/Log/Any.pm

filter: Statementstmt 100% 17 / 17 Branchbran 50.0% 1 / 2 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 2 / 2 Podpod 100% 1 / 1 totaltotal 82.1% 23 / 28 CC 4 SCAR 12.4
CC
4
Cov
87%
register
4.1
BEGIN
1.0
CRAP 3.5
« Mojo/Log/Role/AttachLogger.pm ↑ Summary
1 package Mojolicious::Plugin::Log::Any;
2  
3 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = 'v2.0.0';
6  
7 2 ▶sub register {
Subroutine register: called CC=4 SCAR=14.0
Pod documented
8 2 my ($self, $app, $conf) = @_;
9
10 2 ▶ my $logger = delete $conf->{logger} // 'Log::Any';
Condition50%1/2
delete $conf->{"logger"} // "Log::Any"
l!l
20
MC/DC0%0/1
delete $conf->{"logger"} // "Log::Any"
delete $conf->{"logger"}
Truth table50%1/2
A
A= delete $conf->{"logger"}
Aresult
11
00
11 2 my %opt = %$conf;
12 2 ▶ $opt{category} //= ref $app;
Condition50%1/2
$opt{"category"} //= ref $app
l!l
02
MC/DC0%0/1
$opt{"category"} //= ref $app
$opt{"category"}
Truth table50%1/2
A
A= $opt{"category"}
Aresult
11
00
13  
14 2 my $lowest_level;
15 2 { local $@;
16 2 ▶ $lowest_level = eval { require Mojolicious; Mojolicious->VERSION('9.20'); 1 } ? 'trace' : 'debug';
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Mojolicious; 'Mojolicious'->VERSION('9.20'); 1 } } ? : 2 0 2
17 }
18 2 $app->log->with_roles('Mojo::Log::Role::AttachLogger')->level($lowest_level)
19 ->unsubscribe('message')->attach_logger($logger, \%opt);
20 }
21  
22 1 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<Mojolicious::Plugin::Log::Any> is a L<Mojolicious> plugin that redirects the
75 application logger to pass its log messages to an external logging framework
76 using L<Mojo::Log::Role::AttachLogger/"attach_logger">. By default, L<Log::Any>
77 is used, but a different framework or object may be specified. For L<Log::Any>
78 or L<Log::Log4perl>, log messages are dispatched with a category of the
79 application class name, which is C<Mojolicious::Lite> for lite applications.
80  
81 The default behavior of the L<Mojo::Log> 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<debug> or
84 C<trace> (the lowest level) and removing the default L<Mojo::Log/"message">
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<Mojo::Log::Role::AttachLogger>
88 directly.
89  
90 =head1 METHODS
91  
92 L<Mojolicious::Plugin::Log::Any> inherits all methods from
93 L<Mojolicious::Plugin> 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<Mojolicious> 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<Mojo::Log::Role::AttachLogger/"attach_logger">. Defaults to C<Log::Any>.
108  
109 =item category
110  
111 Passed through to L<Mojo::Log::Role::AttachLogger/"attach_logger">. Defaults to
112 the application name.
113  
114 =item prepend_level
115  
116 Passed through to L<Mojo::Log::Role::AttachLogger/"attach_logger">.
117  
118 =item message_separator
119  
120 Passed through to L<Mojo::Log::Role::AttachLogger/"attach_logger">.
121  
122 =back
123  
124 =head1 BUGS
125  
126 Report any issues on the public bugtracker.
127  
128 =head1 AUTHOR
129  
130 Dan Book <dbook@cpan.org>
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<Mojo::Log>, L<Mojo::Log::Role::AttachLogger>
« Mojo/Log/Role/AttachLogger.pm ↑ Summary