Mojo/Log/Role/AttachLogger.pm

filter: Statementstmt 53.0% 43 / 81 Branchbran 25.0% 13 / 52 Conditioncond 38.8% 7 / 18 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 61.5% 8 / 13 Podpod 100% 1 / 1 totaltotal 42.6% 75 / 176 CC 34 SCAR 38.4
CC
34
Cov
42%
attach_logger
91.9
__ANON__
56.0
__ANON__
30.0
CRAP 46.4
↑ Summary Mojolicious/Plugin/Log/Any.pm »
1 package Mojo::Log::Role::AttachLogger;
2  
3 3 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Import::Into ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Module::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $VERSION = 'v2.0.0';
10  
11 3 our @CARP_NOT = 'Mojolicious::Plugin::Log::Any';
12  
13 3 requires 'on';
14  
15 4 ▶sub attach_logger {
Subroutine attach_logger: called CC=16 SCAR=45.2
Pod documented
16 4 my ($self, $logger, $opt) = @_;
17 4 ▶ Carp::croak 'No logger passed' unless defined $logger;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $logger 0 4 4
18 4 my ($category, $prepend, $separator);
19 4 ▶ if (ref $opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt) { } 3 1 4
20 3 ($category, $prepend, $separator) = @$opt{qw[ category prepend_level message_separator ]};
21 } else {
22 1 $category = $opt;
23 }
24 4 ▶ $category //= 'Mojo::Log';
Condition100%2/2
$category //= "Mojo::Log"
l!l
31
MC/DC100%1/1
$category //= "Mojo::Log"
$category
Truth table100%2/2
A
A= $category
Aresult
11
00
25 4 ▶ $prepend //= 1;
Condition100%2/2
$prepend //= 1
l!l
13
MC/DC100%1/1
$prepend //= 1
$prepend
Truth table100%2/2
A
A= $prepend
Aresult
11
00
26 4 ▶ $separator //= "\n";
Condition100%2/2
$separator //= "\n"
l!l
13
MC/DC100%1/1
$separator //= "\n"
$separator
Truth table100%2/2
A
A= $separator
Aresult
11
00
27  
28 4 my $do_log;
29 4 ▶ if (Scalar::Util::blessed($logger)) {
Branch: 4 branches
BranchTrueFalseTotal
if (builtin::blessed($logger)) { } 3 1 4
elsif ($logger eq 'Log::Any') { } 1 0 1
elsif ($logger eq 'Log::Log4perl') { } 0 0 0
elsif ($logger eq 'Log::Contextual' or "$logger"->isa('Log::Contextual')) { } 0 0 0
30 3 ▶ if ($logger->isa('Log::Any::Proxy')) {
Branch: 4 branches
BranchTrueFalseTotal
if ($logger->isa('Log::Any::Proxy')) { } 0 3 3
elsif ($logger->isa('Log::Dispatch')) { } 0 3 3
elsif ($logger->isa('Log::Dispatchouli') or $logger->isa('Log::Dispatchouli::Proxy')) { } 0 3 3
elsif ($logger->isa('Mojo::Log')) { } 3 0 3
31 0 ▶ $do_log = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
32 0 my ($self, $level, @msg) = @_;
33 0 ▶ my $msg = @msg > 1 ? join($separator, @msg) : $msg[0];
Branch: 1 branch
BranchTrueFalseTotal
@msg > 1 ? : 0 0 0
34 0 ▶ $msg = "[$level] $msg" if $prepend;
Branch: 1 branch
BranchTrueFalseTotal
if $prepend 0 0 0
35 0 $logger->$level($msg);
36 0 };
37 } elsif ($logger->isa('Log::Dispatch')) {
38 0 ▶ $do_log = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
39 0 my ($self, $level, @msg) = @_;
40 0 ▶ my $msg = @msg > 1 ? join($separator, @msg) : $msg[0];
Branch: 1 branch
BranchTrueFalseTotal
@msg > 1 ? : 0 0 0
41 0 ▶ $msg = "[$level] $msg" if $prepend;
Branch: 1 branch
BranchTrueFalseTotal
if $prepend 0 0 0
42 0 ▶ $level = 'critical' if $level eq 'fatal';
Branch: 1 branch
BranchTrueFalseTotal
if $level eq "fatal" 0 0 0
43 0 ▶ $level = 'debug' if $level eq 'trace';
Branch: 1 branch
BranchTrueFalseTotal
if $level eq "trace" 0 0 0
44 0 $logger->log(level => $level, message => $msg);
45 0 };
46 ▶ } elsif ($logger->isa('Log::Dispatchouli') or $logger->isa('Log::Dispatchouli::Proxy')) {
Condition33%1/3
$logger->isa("Log::Dispatchouli") or $logger->isa("Log::Dispatchouli::Proxy")
l!l&&r!l&&!r
003
MC/DC0%0/2
$logger->isa("Log::Dispatchouli") or $logger->isa("Log::Dispatchouli::Proxy")
$logger->isa("Log::Dispatchouli") $logger->isa("Log::Dispatchouli::Proxy")
Truth table33%1/3
A or B
A= $logger->isa("Log::Dispatchouli")B= $logger->isa("Log::Dispatchouli::Proxy")
ABresult
1X1
011
000
47 0 ▶ $do_log = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
48 0 my ($self, $level, @msg) = @_;
49 0 ▶ my $msg = @msg > 1 ? join($separator, @msg) : $msg[0];
Branch: 1 branch
BranchTrueFalseTotal
@msg > 1 ? : 0 0 0
50 0 ▶ $msg = "[$level] $msg" if $prepend;
Branch: 1 branch
BranchTrueFalseTotal
if $prepend 0 0 0
51 0 ▶ return $logger->log_debug($msg) if $level eq 'trace' or $level eq 'debug';
Branch: 1 branch
BranchTrueFalseTotal
if $level eq "trace" or $level eq "debug" 0 0 0
Condition0%0/3
$level eq "trace" or $level eq "debug"
l!l&&r!l&&!r
000
MC/DC0%0/2
$level eq "trace" or $level eq "debug"
$level eq "trace" $level eq "debug"
Truth table0%0/3
A or B
A= $level eq "trace"B= $level eq "debug"
ABresult
1X1
011
000
52 # hacky but we don't want to use log_fatal because it throws an
53 # exception, we want to allow real exceptions to propagate, and we
54 # can't localize a call to set_muted
55 0 ▶ local $logger->{muted} = 0 if $level eq 'fatal' and $logger->get_muted;
Branch: 1 branch
BranchTrueFalseTotal
if $level eq "fatal" and $logger->get_muted 0 0 0
Condition0%0/3
$level eq "fatal" and $logger->get_muted
!ll&&!rl&&r
000
MC/DC0%0/2
$level eq "fatal" and $logger->get_muted
$level eq "fatal" $logger->get_muted
Truth table0%0/3
A and B
A= $level eq "fatal"B= $logger->get_muted
ABresult
0X0
100
111
56 0 $logger->log($msg);
57 0 };
58 } elsif ($logger->isa('Mojo::Log')) {
59 54 ▶ $do_log = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
60 54 my ($self, $level, @msg) = @_;
61 54 $logger->$level(@msg);
62 3 };
63 } else {
64 0 Carp::croak "Unsupported logger object class " . ref($logger);
65 }
66 } elsif ($logger eq 'Log::Any') {
67 1 require Log::Any;
68 1 $logger = Log::Any->get_logger(category => $category);
69 12 ▶ $do_log = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
70 12 my ($self, $level, @msg) = @_;
71 12 ▶ my $msg = @msg > 1 ? join($separator, @msg) : $msg[0];
Branch: 1 branch
BranchTrueFalseTotal
@msg > 1 ? : 6 6 12
72 12 ▶ $msg = "[$level] $msg" if $prepend;
Branch: 1 branch
BranchTrueFalseTotal
if $prepend 0 12 12
73 12 $logger->$level($msg);
74 1 };
75 } elsif ($logger eq 'Log::Log4perl') {
76 0 require Log::Log4perl;
77 0 $logger = Log::Log4perl->get_logger($category);
78 0 ▶ $do_log = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
79 0 my ($self, $level, @msg) = @_;
80 0 ▶ my $msg = @msg > 1 ? join($separator, @msg) : $msg[0];
Branch: 1 branch
BranchTrueFalseTotal
@msg > 1 ? : 0 0 0
81 0 ▶ $msg = "[$level] $msg" if $prepend;
Branch: 1 branch
BranchTrueFalseTotal
if $prepend 0 0 0
82 0 $logger->$level($msg);
83 0 };
84 ▶ } elsif ($logger eq 'Log::Contextual' or "$logger"->isa('Log::Contextual')) {
Condition0%0/3
$logger eq "Log::Contextual" or "$logger"->isa("Log::Contextual")
l!l&&r!l&&!r
000
MC/DC0%0/2
$logger eq "Log::Contextual" or "$logger"->isa("Log::Contextual")
$logger eq "Log::Contextual" "$logger"->isa("Log::Contextual")
Truth table0%0/3
A or B
A= $logger eq "Log::Contextual"B= "$logger"->isa("Log::Contextual")
ABresult
1X1
011
000
85 0 Module::Runtime::require_module("$logger");
86 0 Log::Contextual->VERSION('0.008001');
87 0 my %functions = map { ($_ => "slog_$_") } qw(trace debug info warn error fatal);
88 0 "$logger"->import::into(ref($self), values %functions);
89 0 ▶ $do_log = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
90 0 my ($self, $level, @msg) = @_;
91 0 ▶ my $msg = @msg > 1 ? join($separator, @msg) : $msg[0];
Branch: 1 branch
BranchTrueFalseTotal
@msg > 1 ? : 0 0 0
92 0 ▶ $msg = "[$level] $msg" if $prepend;
Branch: 1 branch
BranchTrueFalseTotal
if $prepend 0 0 0
93 0 $self->can($functions{$level})->($msg);
94 0 };
95 } else {
96 0 Carp::croak "Unsupported logger class $logger";
97 }
98
99 4 $self->on(message => $do_log);
100
101 4 return $self;
102 }
103  
104 3 1;
105  
106 =head1 NAME
107  
108 Mojo::Log::Role::AttachLogger - Use other loggers for Mojo::Log
109  
110 =head1 SYNOPSIS
111  
112 use Mojo::Log;
113 my $log = Mojo::Log->with_roles('+AttachLogger')->new->unsubscribe('message');
114
115 # Log::Any
116 use Log::Any::Adapter {category => 'Mojo::Log', message_separator => ' '}, 'Syslog';
117 $log->attach_logger('Log::Any', 'Some::Category');
118
119 # Log::Contextual
120 use Log::Contextual::WarnLogger;
121 use Log::Contextual -logger => Log::Contextual::WarnLogger->new({env_prefix => 'MYAPP'});
122 $log->attach_logger('Log::Contextual');
123
124 # Log::Dispatch
125 use Log::Dispatch;
126 my $logger = Log::Dispatch->new(outputs => ['File::Locked',
127 min_level => 'warning',
128 filename => '/path/to/file.log',
129 mode => 'append',
130 newline => 1,
131 callbacks => sub { my %p = @_; '[' . localtime() . '] ' . $p{message} },
132 ]);
133 $log->attach_logger($logger);
134
135 # Log::Dispatchouli
136 use Log::Dispatchouli;
137 my $logger = Log::Dispatchouli->new({ident => 'MyApp', facility => 'daemon', to_file => 1});
138 $log->attach_logger($logger);
139
140 # Log::Log4perl
141 use Log::Log4perl;
142 Log::Log4perl->init('/path/to/log.conf');
143 $log->attach_logger('Log::Log4perl', 'Some::Category');
144
145 =head1 DESCRIPTION
146  
147 L<Mojo::Log::Role::AttachLogger> is a L<Role::Tiny> role for L<Mojo::Log> that
148 redirects log messages to an external logging framework. L</"attach_logger">
149 currently recognizes the strings C<Log::Any>, C<Log::Contextual>,
150 C<Log::Log4perl>, and objects of the classes C<Log::Any::Proxy>,
151 C<Log::Dispatch>, C<Log::Dispatchouli>, and C<Mojo::Log>.
152  
153 The default L<Mojo::Log/"message"> event handler is not suppressed by
154 L</"attach_logger">, so if you want to suppress the default behavior, you
155 should unsubscribe from the message event first. Unsubscribing from the message
156 event will also remove any loggers attached by L</"attach_logger">.
157  
158 Since L<Mojolicious> 8.06, the L<Mojo::Log/"message"> event will not be sent
159 for messages below the log level set in the L<Mojo::Log> object, so the
160 attached logger will only receive log messages exceeding the configured level.
161  
162 Since L<Mojolicious> 9.20, the C<trace> log level is supported though it may be
163 mapped to C<debug> on some loggers.
164  
165 L<Mojolicious::Plugin::Log::Any> can be used to attach a logger to the
166 L<Mojolicious> application logger and suppress the default message event
167 handler.
168  
169 =head1 METHODS
170  
171 L<Mojo::Log::Role::AttachLogger> composes the following methods.
172  
173 =head2 attach_logger
174  
175 $log = $log->attach_logger($logger, $options);
176  
177 Subscribes to L<Mojo::Log/"message"> and passes log messages to the given
178 logging framework or object. The second argument is optionally a category
179 (default C<Mojo::Log>) or hashref of options. The log level will be prepended
180 to the message in square brackets (except when passing to another L<Mojo::Log>
181 object, or L</"prepend_level"> is false).
182  
183 The following loggers are recognized:
184  
185 =over
186  
187 =item Log::Any
188  
189 The string C<Log::Any> will use a global L<Log::Any> logger with the specified
190 category (defaults to C<Mojo::Log>).
191  
192 =item Log::Any::Proxy
193  
194 A L<Log::Any::Proxy> object can be passed directly and will be used for logging
195 in the standard manner, using the object's existing category.
196  
197 =item Log::Contextual
198  
199 The string C<Log::Contextual> will use the global L<Log::Contextual> logger.
200 Package loggers are not supported. Note that L<Log::Contextual/"with_logger">
201 may be difficult to use with L<Mojolicious> logging due to the asynchronous
202 nature of the dispatch cycle.
203  
204 =item Log::Dispatch
205  
206 A L<Log::Dispatch> object can be passed to be used for logging. The C<fatal>
207 log level will be mapped to C<critical>, and the C<trace> and C<debug> log
208 levels will both be mapped to C<debug>.
209  
210 =item Log::Dispatchouli
211  
212 A L<Log::Dispatchouli> object can be passed to be used for logging. The
213 C<fatal> log level will log messages even if the object is C<muted>, but an
214 exception will not be thrown as L<Log::Dispatchouli/"log_fatal"> normally does.
215 The C<trace> and C<debug> log levels will be logged with
216 L<Log::Dispatchouli/"log_debug">.
217  
218 =item Log::Log4perl
219  
220 The string C<Log::Log4perl> will use a global L<Log::Log4perl> logger with the
221 specified category (defaults to C<Mojo::Log>).
222  
223 =item Mojo::Log
224  
225 Another L<Mojo::Log> object can be passed to be used for logging.
226  
227 =back
228  
229 The following options are supported:
230  
231 =over
232  
233 =item category
234  
235 Category name (defaults to Mojo::Log).
236  
237 =item prepend_level
238  
239 Prepend the log level to messages in the form C<[$level]> (default for
240 non-L<Mojo::Log> loggers). Set false to disable.
241  
242 =item message_separator
243  
244 String to separate multiple messages. Defaults to newline.
245  
246 =back
247  
248 =head1 BUGS
249  
250 Report any issues on the public bugtracker.
251  
252 =head1 AUTHOR
253  
254 Dan Book <dbook@cpan.org>
255  
256 =head1 COPYRIGHT AND LICENSE
257  
258 This software is Copyright (c) 2017 by Dan Book.
259  
260 This is free software, licensed under:
261  
262 The Artistic License 2.0 (GPL Compatible)
263  
264 =head1 SEE ALSO
265  
266 L<Mojo::Log>, L<Log::Any>, L<Log::Contextual>, L<Log::Dispatch>,
267 L<Log::Dispatchouli>, L<Log::Log4perl>
↑ Summary Mojolicious/Plugin/Log/Any.pm »