Dispatchouli.pm

filter: Statementstmt 82.0% 169 / 206 Branchbran 55.6% 59 / 106 Conditioncond 57.1% 16 / 28 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 73.5% 50 / 68 Podpod 85.0% 34 / 40 totaltotal 71.7% 333 / 464 CC 72 SCAR 22.3
CC
72
Cov
72%
new
30.0
__ANON__
30.0
log_debug
12.2
CRAP 9.3
↑ Summary Fmt.pm »
1 7 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Log::Dispatchouli 3.101;
4 # ABSTRACT: a simple wrapper around Log::Dispatch
5  
6 # Not dangerous. Accepted without change.
7 7 ▶use experimental 'postderef', 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Log::Dispatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Log::Fmt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Params::Util qw(_ARRAY0 _HASH0 _CODELIKE);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use String::Flogger;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Try::Tiny 0.04;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 7 require Log::Dispatchouli::Proxy;
19  
20 7 our @CARP_NOT = qw(Log::Dispatchouli::Proxy);
21  
22 #pod =head1 SYNOPSIS
23 #pod
24 #pod my $logger = Log::Dispatchouli->new({
25 #pod ident => 'stuff-purger',
26 #pod facility => 'daemon',
27 #pod to_stdout => $opt->{print},
28 #pod debug => $opt->{verbose}
29 #pod });
30 #pod
31 #pod $logger->log([ "There are %s items left to purge...", $stuff_left ]);
32 #pod
33 #pod $logger->log_debug("this is extra often-ignored debugging log");
34 #pod
35 #pod $logger->log_fatal("Now we will die!!");
36 #pod
37 #pod =head1 DESCRIPTION
38 #pod
39 #pod Log::Dispatchouli is a thin layer above L<Log::Dispatch> and meant to make it
40 #pod dead simple to add logging to a program without having to think much about
41 #pod categories, facilities, levels, or things like that. It is meant to make
42 #pod logging just configurable enough that you can find the logs you want and just
43 #pod easy enough that you will actually log things.
44 #pod
45 #pod Log::Dispatchouli can log to syslog (if you specify a facility), standard error
46 #pod or standard output, to a file, or to an array in memory. That last one is
47 #pod mostly useful for testing.
48 #pod
49 #pod In addition to providing as simple a way to get a handle for logging
50 #pod operations, Log::Dispatchouli uses L<String::Flogger> to process the things to
51 #pod be logged, meaning you can easily log data structures. Basically: strings are
52 #pod logged as is, arrayrefs are taken as (sprintf format, args), and subroutines
53 #pod are called only if needed. For more information read the L<String::Flogger>
54 #pod docs.
55 #pod
56 #pod =head1 LOGGER PREFIX
57 #pod
58 #pod Log messages may be prepended with information to set context. This can be set
59 #pod at a logger level or per log item. The simplest example is:
60 #pod
61 #pod my $logger = Log::Dispatchouli->new( ... );
62 #pod
63 #pod $logger->set_prefix("Batch 123: ");
64 #pod
65 #pod $logger->log("begun processing");
66 #pod
67 #pod # ...
68 #pod
69 #pod $logger->log("finished processing");
70 #pod
71 #pod The above will log something like:
72 #pod
73 #pod Batch 123: begun processing
74 #pod Batch 123: finished processing
75 #pod
76 #pod To pass a prefix per-message:
77 #pod
78 #pod $logger->log({ prefix => 'Sub-Item 234: ' }, 'error!')
79 #pod
80 #pod # Logs: Batch 123: Sub-Item 234: error!
81 #pod
82 #pod If the prefix is a string, it is prepended to each line of the message. If it
83 #pod is a coderef, it is called and passed the message to be logged. The return
84 #pod value is logged instead.
85 #pod
86 #pod L<Proxy loggers|/METHODS FOR PROXY LOGGERS> also have their own prefix
87 #pod settings, which accumulate. So:
88 #pod
89 #pod my $proxy = $logger->proxy({ proxy_prefix => 'Subsystem 12: ' });
90 #pod
91 #pod $proxy->set_prefix('Page 9: ');
92 #pod
93 #pod $proxy->log({ prefix => 'Paragraph 6: ' }, 'Done.');
94 #pod
95 #pod ...will log...
96 #pod
97 #pod Batch 123: Subsystem 12: Page 9: Paragraph 6: Done.
98 #pod
99 #pod =method new
100 #pod
101 #pod my $logger = Log::Dispatchouli->new(\%arg);
102 #pod
103 #pod This returns a new logger, a Log::Dispatchouli object.
104 #pod
105 #pod Valid arguments are:
106 #pod
107 #pod ident - the name of the thing logging (mandatory)
108 #pod to_self - log to the logger object for testing; default: false
109 #pod to_stdout - log to STDOUT; default: false
110 #pod to_stderr - log to STDERR; default: false
111 #pod facility - to which syslog facility to send logs; default: none
112 #pod
113 #pod to_file - DEPRECATED: this option will be removed in 2025
114 #pod log to PROGRAM_NAME.YYYYMMDD in the log path; default: false
115 #pod log_file - a leaf name for the file to log to with to_file
116 #pod log_path - path in which to log to file; defaults to DISPATCHOULI_PATH
117 #pod environment variable or, failing that, to your system's tmpdir
118 #pod
119 #pod file_format - this optional coderef is passed the message to be logged
120 #pod and returns the text to write out
121 #pod
122 #pod log_pid - if 1, prefix all log entries with the pid; default: true
123 #pod can also be a comma-delimited list of log targets where pid is
124 #pod logged, like "stderr,syslog"; mostly useful for logging pid in
125 #pod syslog, but not on standard I/O
126 #pod fail_fatal - a boolean; if true, failure to log is fatal; default: true
127 #pod muted - a boolean; if true, only fatals are logged; default: false
128 #pod debug - a boolean; if true, log_debug method is not a no-op
129 #pod defaults to the truth of the DISPATCHOULI_DEBUG env var
130 #pod quiet_fatal - 'stderr' or 'stdout' or an arrayref of zero, one, or both
131 #pod fatal log messages will not be logged to these
132 #pod (default: stderr)
133 #pod config_id - a name for this logger's config; rarely needed!
134 #pod syslog_socket - a value for Sys::Syslog's "socket" arg; default: "native"
135 #pod
136 #pod The log path is either F</tmp> or the value of the F<DISPATCHOULI_PATH> env var.
137 #pod
138 #pod If the F<DISPATCHOULI_NOSYSLOG> env var is true, we don't log to syslog.
139 #pod
140 #pod =cut
141  
142 40 ▶sub new ($class, $arg = undef) {
Subroutine new: called CC=24 SCAR=34.0
Pod documented
143 40 ▶ $arg ||= {};
Condition100%2/2
$arg ||= {}
l!l
391
MC/DC100%1/1
$arg ||= {}
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
144  
145 my $ident = $arg->{ident}
146 40 ▶ or Carp::croak "no ident specified when using $class";
Branch: 1 branch
BranchTrueFalseTotal
my $ident = $arg->{"ident"} or Carp::croak("no ident specified when using $class") 39 1 40
147  
148 39 ▶ my $config_id = defined $arg->{config_id} ? $arg->{config_id} : $ident;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg->{'config_id'} ? : 0 39 39
149  
150 39 my %quiet_fatal;
151 39 for ('quiet_fatal') {
152 39 ▶ %quiet_fatal = map {; $_ => 1 } grep { defined }
Branch: 2 branches
BranchTrueFalseTotal
exists $arg->{$_} ? : 0 39 39
&_ARRAY0($arg->{$_}) ? : 0 0 0
153 exists $arg->{$_}
154 0 ? _ARRAY0($arg->{$_}) ? @{ $arg->{$_} } : $arg->{$_}
155 : ('stderr');
156 };
157  
158 39 my $log = Log::Dispatch->new;
159 my $self = bless {
160 dispatcher => $log,
161 39 ▶ log_pid => (exists $arg->{log_pid} ? $arg->{log_pid} : 1),
Branch: 1 branch
BranchTrueFalseTotal
exists $arg->{'log_pid'} ? : 38 1 39
162 } => $class;
163  
164 39 ▶ if ($arg->{to_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"to_file"}) 2 37 39
165 2 Carp::carp("to_file argument for Log::Dispatchouli is deprecated and will be removed in late 2025");
166  
167 2 require Log::Dispatch::File;
168 my $log_file = File::Spec->catfile(
169 ($arg->{log_path} || $self->env_value('PATH') || File::Spec->tmpdir),
170 2 ▶ $arg->{log_file} || do {
Condition44%4/9
$arg->{'log_path'} || $self->env_value('PATH') || 'File::Spec'->tmpdir
l!l&&r!l&&!r
200
$arg->{"log_path"} or $self->env_value("PATH")
l!l&&r!l&&!r
200
$arg->{'log_file'} || do { my(@time) = localtime; sprintf '%s.%04u%02u%02u', $ident, $time[5] + 1900, $time[4] + 1, $time[3] }
l!l&&r!l&&!r
110
MC/DC0%0/3
$arg->{'log_path'} || $self->env_value('PATH') || 'File::Spec'->tmpdir
$arg->{"log_path"} $self->env_value("PATH") 'File::Spec'->tmpdir
MC/DC0%0/2
$arg->{'log_file'} || do { my(@time) = localtime; sprintf '%s.%04u%02u%02u', $ident, $time[5] + 1900, $time[4] + 1, $time[3] }
$arg->{'log_file'} do { my(@time) = localtime; sprintf '%s.%04u%02u%02u', $ident, $time[5] + 1900, $time[4] + 1, $time[3] }
Truth table25%1/4
A or B || C
A= $arg->{"log_path"}B= $self->env_value("PATH")C= 'File::Spec'->tmpdir
ABCresult
1XX1
01X1
0011
0000
Truth table66%2/3
A || B
A= $arg->{'log_file'}B= do { my(@time) = localtime; sprintf '%s.%04u%02u%02u', $ident, $time[5] + 1900, $time[4] + 1, $time[3] }
ABresult
1X1
011
000
171 1 my @time = localtime;
172 1 sprintf('%s.%04u%02u%02u',
173 $ident,
174 $time[5] + 1900,
175 $time[4] + 1,
176 $time[3])
177 }
178 );
179  
180 $log->add(
181 Log::Dispatch::File->new(
182 name => 'logfile',
183 min_level => 'debug',
184 filename => $log_file,
185 mode => 'append',
186 2 callbacks => do {
187 2 my $log_pid = $self->log_pid_for('file');
188  
189 2 ▶ if (my $format = $arg->{file_format}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $format = $arg->{'file_format'}) { } 1 1 2
190 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
191 1 my $message = {@_}->{message};
192 1 ▶ $message = "[$$] $message" if $log_pid;
Branch: 1 branch
BranchTrueFalseTotal
if $log_pid 0 1 1
193 1 $format->($message)
194 1 };
195 } else {
196 # The time format returned here is subject to change. -- rjbs,
197 # 2008-11-21
198 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
199 1 my $message = {@_}->{message};
200 1 ▶ $message = "[$$] $message" if $log_pid;
Branch: 1 branch
BranchTrueFalseTotal
if $log_pid 1 0 1
201 1 (localtime) . " $message\n";
202 1 };
203 }
204 },
205 )
206 );
207 }
208  
209 39 ▶ if ($arg->{facility} and not $self->env_value('NOSYSLOG')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"facility"} and not $self->env_value("NOSYSLOG")) 0 39 39
Condition33%1/3
$arg->{"facility"} and not $self->env_value("NOSYSLOG")
!ll&&!rl&&r
3900
MC/DC0%0/2
$arg->{"facility"} and not $self->env_value("NOSYSLOG")
$arg->{"facility"} not $self->env_value("NOSYSLOG")
Truth table33%1/3
A and not(B)
A= $arg->{"facility"}B= not $self->env_value("NOSYSLOG")
ABresult
0X0
100
111
210 $self->setup_syslog_output(
211 facility => $arg->{facility},
212 socket => $arg->{syslog_socket},
213 0 ident => $ident,
214 );
215 }
216  
217 39 ▶ if ($arg->{to_self}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"to_self"}) 35 4 39
218 35 $self->{events} = [];
219 35 require Log::Dispatch::Array;
220 $log->add(
221 Log::Dispatch::Array->new(
222 name => 'self',
223 min_level => 'debug',
224 array => $self->{events},
225 ($self->log_pid_for('self')
226 2 ▶ ? (callbacks => sub { "[$$] ". {@_}->{message} })
Subroutine __ANON__: called CC=1 SCAR=0.0
227 35 ▶ : ())
Branch: 1 branch
BranchTrueFalseTotal
$self->log_pid_for('self') ? : 1 34 35
228 ),
229 );
230 }
231  
232 39 $self->{prefix} = $arg->{prefix};
233 39 $self->{ident} = $ident;
234 39 $self->{config_id} = $config_id;
235  
236 39 DEST: for my $dest (qw(err out)) {
237 78 ▶ next DEST unless $arg->{"to_std$dest"};
Branch: 1 branch
BranchTrueFalseTotal
unless $$arg{"to_std$dest"} 78 0 78
238 0 my $method = "enable_std$dest";
239  
240 0 $self->$method;
241 }
242  
243 $self->{debug} = exists $arg->{debug}
244 39 ▶ ? ($arg->{debug} ? 1 : 0)
Branch: 3 branches
BranchTrueFalseTotal
exists $arg->{'debug'} ? : 1 38 39
$arg->{'debug'} ? : 1 0 1
$self->env_value('DEBUG') ? : 4 34 38
245 : ($self->env_value('DEBUG') ? 1 : 0);
246 39 $self->{muted} = $arg->{muted};
247  
248 39 $self->{quiet_fatal} = \%quiet_fatal;
249 39 ▶ $self->{fail_fatal} = exists $arg->{fail_fatal} ? $arg->{fail_fatal} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $arg->{'fail_fatal'} ? : 0 39 39
250  
251 39 return $self;
252 }
253  
254 37 ▶sub log_pid_for ($self, $output) {
Subroutine log_pid_for: called CC=4 SCAR=17.9
Pod undocumented
255 37 my $log_pid = $self->{log_pid};
256 37 ▶ return undef unless $log_pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $log_pid 35 2 37
257  
258 2 ▶ return 1 if $log_pid eq 1;
Branch: 1 branch
BranchTrueFalseTotal
if $log_pid eq 1 2 0 2
259  
260 0 $self->{log_pid_for} = { map {; $_ => 1 } split /,/, $log_pid };
261  
262 0 ▶ return $self->{log_pid_for}{$output} ? 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'log_pid_for'}{$output} ? : 0 0 0
263 }
264  
265 #pod =method enable_stdout
266 #pod
267 #pod =method enable_stderr
268 #pod
269 #pod These methods turn on logging to STDOUT or STDERR, respectively. If that
270 #pod logging has already been enabled, these methods do nothing.
271 #pod
272 #pod There is not, yet, a I<disable> version of these methods.
273 #pod
274 #pod =cut
275  
276 7 for my $dest (qw(out err)) {
277 14 my $name = "std$dest";
278 0 ▶ my $code = sub ($self) {
Subroutine __ANON__: not called CC=5 SCAR=34.0
279 0 ▶ return if $self->dispatcher->output($name);
Branch: 1 branch
BranchTrueFalseTotal
if $self->dispatcher->output($name) 0 0 0
280  
281 my $callback = $self->log_pid_for($name)
282 0 ▶ ? sub { "[$$] " . ({@_}->{message}) . "\n" }
Subroutine __ANON__: not called CC=1 SCAR=6.9
283 0 ▶ : sub { ({@_}->{message}) . "\n" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$self->log_pid_for($name) ? : 0 0 0
284  
285 $self->dispatcher->add(
286 $self->stdio_dispatcher_class->new(
287 name => "std$dest",
288 min_level => 'debug',
289 stderr => ($dest eq 'err' ? 1 : 0),
290 callbacks => $callback,
291 0 ▶ ($self->{quiet_fatal}{"std$dest"} ? (max_level => 'info') : ()),
Branch: 2 branches
BranchTrueFalseTotal
$dest eq 'err' ? : 0 0 0
$self->{'quiet_fatal'}{"std$dest"} ? : 0 0 0
292 ),
293 );
294 14 };
295  
296 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
297 14 *{"enable_std$dest"} = $code;
298 }
299  
300 0 ▶sub setup_syslog_output ($self, %arg) {
Subroutine setup_syslog_output: not called CC=3 SCAR=24.8
Pod undocumented
301 0 require Log::Dispatch::Syslog;
302 $self->{dispatcher}->add(
303 Log::Dispatch::Syslog->new(
304 name => 'syslog',
305 ident => $arg{ident},
306 facility => $arg{facility},
307 logopt => ($self->log_pid_for('syslog') ? 'pid' : ''),
308 min_level => 'debug',
309 socket => $arg{socket} || 'native',
310 0 ▶ callbacks => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
311 0 ( my $m = {@_}->{message} ) =~ s/\n/<LF>/g;
312 0 $m
313 },
314 0 ▶ ),
Branch: 1 branch
BranchTrueFalseTotal
$self->log_pid_for('syslog') ? : 0 0 0
Condition0%0/2
$arg{'socket'} || 'native'
l!l
00
MC/DC0%0/1
$arg{'socket'} || 'native'
$arg{'socket'}
Truth table0%0/2
A
A= $arg{'socket'}
Aresult
11
00
315 );
316 }
317  
318 #pod =method log
319 #pod
320 #pod $logger->log(@messages);
321 #pod
322 #pod $logger->log(\%arg, @messages);
323 #pod
324 #pod This method uses L<String::Flogger> on the input, then I<unconditionally> logs
325 #pod the result. Each message is flogged individually, then joined with spaces.
326 #pod
327 #pod If the first argument is a hashref, it will be used as extra arguments to
328 #pod logging. It may include a C<prefix> entry to preprocess the message by
329 #pod prepending a string (if the prefix is a string) or calling a subroutine to
330 #pod generate a new message (if the prefix is a coderef).
331 #pod
332 #pod =cut
333  
334 49 ▶sub _flog_messages ($self, $arg, $rest) {
Subroutine _flog_messages: called CC=5 SCAR=16.1
335 49 my $flogger = $self->string_flogger;
336 49 my @flogged = map {; $flogger->flog($_) } @$rest;
337 49 ▶ my $message = @flogged > 1 ? join(q{ }, @flogged) : $flogged[0];
Branch: 1 branch
BranchTrueFalseTotal
@flogged > 1 ? : 2 47 49
338  
339 my @prefix = _ARRAY0($arg->{prefix})
340 19 ? @{ $arg->{prefix} }
341 49 ▶ : $arg->{prefix};
Branch: 1 branch
BranchTrueFalseTotal
&_ARRAY0($arg->{'prefix'}) ? : 19 30 49
342  
343 49 for (reverse grep { defined } $self->get_prefix, @prefix) {
344 76 ▶ if (_CODELIKE( $_ )) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_CODELIKE($_)) { } 1 75 76
345 1 $message = $_->($message);
346 } else {
347 75 $message =~ s/^/$_/gm;
348 }
349 }
350  
351 49 return $message;
352 }
353  
354 49 ▶sub flog_messages ($self, @rest) {
Subroutine flog_messages: called CC=2 SCAR=6.9
Pod documented
355 49 ▶ my $arg = _HASH0($rest[0]) ? shift(@rest) : {};
Branch: 1 branch
BranchTrueFalseTotal
&_HASH0($rest[0]) ? : 45 4 49
356 49 return $self->_flog_messages($arg, \@rest);
357 }
358  
359 42 ▶sub log ($self, @rest) {
Subroutine log: called CC=5 SCAR=16.8
Pod documented
360 42 ▶ my $arg = _HASH0($rest[0]) ? shift(@rest) : {};
Branch: 1 branch
BranchTrueFalseTotal
&_HASH0($rest[0]) ? : 20 22 42
361  
362 42 my $message;
363  
364 42 ▶ if ($arg->{fatal} or ! $self->get_muted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->{"fatal"} or not $self->get_muted) 40 2 42
Condition100%3/3
$arg->{"fatal"} or not $self->get_muted
l!l&&r!l&&!r
3372
MC/DC100%2/2
$arg->{"fatal"} or not $self->get_muted
$arg->{"fatal"} not $self->get_muted
Truth table100%3/3
A or not(B)
A= $arg->{"fatal"}B= not $self->get_muted
ABresult
1X1
011
000
365 40 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
366 40 $message = $self->flog_messages($arg, @rest);
367  
368 $self->dispatcher->log(
369 40 ▶ level => $arg->{level} || 'info',
Condition100%2/2
$arg->{'level'} || 'info'
l!l
634
MC/DC100%1/1
$arg->{'level'} || 'info'
$arg->{'level'}
Truth table100%2/2
A
A= $arg->{'level'}
Aresult
11
00
370 message => $message,
371 );
372 0 ▶ } catch {
Subroutine __ANON__: not called CC=3 SCAR=24.8
373 0 ▶ $message = '(no message could be logged)' unless defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 0 0 0
374 0 ▶ die $_ if $self->{fail_fatal};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fail_fatal"} 0 0 0
375 40 };
376 }
377  
378 42 ▶ Carp::croak $message if $arg->{fatal};
Branch: 1 branch
BranchTrueFalseTotal
if $arg->{"fatal"} 3 39 42
379  
380 39 return;
381 }
382  
383 #pod =method log_fatal
384 #pod
385 #pod This behaves like the C<log> method, but will throw the logged string as an
386 #pod exception after logging.
387 #pod
388 #pod This method can also be called as C<fatal>, to match other popular logging
389 #pod interfaces. B<If you want to override this method, you must override
390 #pod C<log_fatal> and not C<fatal>>.
391 #pod
392 #pod =cut
393  
394 3 ▶sub log_fatal ($self, @rest) {
Subroutine log_fatal: called CC=4 SCAR=14.9
Pod documented
395 3 ▶ my $arg = _HASH0($rest[0]) ? shift(@rest) : {}; # for future expansion
Branch: 1 branch
BranchTrueFalseTotal
&_HASH0($rest[0]) ? : 0 3 3
396  
397 3 ▶ local $arg->{level} = defined $arg->{level} ? $arg->{level} : 'error';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg->{'level'} ? : 0 3 3
398 3 ▶ local $arg->{fatal} = defined $arg->{fatal} ? $arg->{fatal} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg->{'fatal'} ? : 0 3 3
399  
400 3 $self->log($arg, @rest);
401 }
402  
403 #pod =method log_debug
404 #pod
405 #pod This behaves like the C<log> method, but will only log (at the debug level) if
406 #pod the logger object has its debug property set to true.
407 #pod
408 #pod This method can also be called as C<debug>, to match other popular logging
409 #pod interfaces. B<If you want to override this method, you must override
410 #pod C<log_debug> and not C<debug>>.
411 #pod
412 #pod =cut
413  
414 5 ▶sub log_debug ($self, @rest) {
Subroutine log_debug: called CC=4 SCAR=25.0
Pod documented
415 5 ▶ return unless $self->is_debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_debug 5 0 5
416  
417 0 ▶ my $arg = _HASH0($rest[0]) ? shift(@rest) : {}; # for future expansion
Branch: 1 branch
BranchTrueFalseTotal
&_HASH0($rest[0]) ? : 0 0 0
418  
419 0 ▶ local $arg->{level} = defined $arg->{level} ? $arg->{level} : 'debug';
Branch: 1 branch
BranchTrueFalseTotal
defined $arg->{'level'} ? : 0 0 0
420  
421 0 $self->log($arg, @rest);
422 }
423  
424 #pod =method log_event
425 #pod
426 #pod This method is like C<log>, but is used for structured logging instead of free
427 #pod form text. It's invoked like this:
428 #pod
429 #pod $logger->log($event_type => $data_ref);
430 #pod
431 #pod C<$event_type> should be a simple string, probably a valid identifier, that
432 #pod identifies the kind of event being logged. It is suggested, but not required,
433 #pod that all events of the same type have the same kind of structured data in them.
434 #pod
435 #pod C<$data_ref> is a set of key/value pairs of data to log in this event. It can
436 #pod be an arrayref (in which case the ordering of pairs is preserved) or a hashref
437 #pod (in which case they are sorted by key).
438 #pod
439 #pod The logged string will be in logfmt format, meaning a series of key=value
440 #pod pairs separated by spaces and following these rules:
441 #pod
442 #pod =for :list
443 #pod * an "identifier" is a string of printable ASCII characters between C<!> and
444 #pod C<~>, excluding C<\> and C<=>
445 #pod * keys must be valid identifiers
446 #pod * if a key is empty, C<~> is used instead
447 #pod * if a key contains characters not permitted in an identifier, they are
448 #pod replaced by C<?>
449 #pod * values must I<either> be valid identifiers, or be quoted
450 #pod * quoted value start and end with C<">
451 #pod * in a quoted value, C<"> becomes C<\">, C<\> becomes C<\\>, newline and
452 #pod carriage return become C<\n> and C<\r> respectively, and other control
453 #pod characters are replaced with C<\u{....}> where the contents of the braces are
454 #pod the hex value of the control character
455 #pod
456 #pod When values are undef, they are represented as C<~>.
457 #pod
458 #pod When values are array references, the index/values are mapped over, so that:
459 #pod
460 #pod key => [ 'a', 'b' ]
461 #pod
462 #pod becomes
463 #pod
464 #pod key.0=a key.1=b
465 #pod
466 #pod When values are hash references, the key/values are mapped, with keys sorted,
467 #pod so that:
468 #pod
469 #pod key => { b => 2, a => 1 }
470 #pod
471 #pod becomes
472 #pod
473 #pod key.a=1 key.b=2
474 #pod
475 #pod This expansion is performed recursively. If a value itself recurses,
476 #pod appearances of a reference after the first time will be replaced with a string
477 #pod like C<&foo.bar>, pointing to the first occurrence. I<This is not meant to be
478 #pod a robust serialization mechanism.> It's just here to help you be a little
479 #pod lazy. Don't push the limits.
480 #pod
481 #pod If the value in C<$data_ref> is a code reference, it will be called and its
482 #pod result logged. If its result is also a code reference, you get whatever
483 #pod garbage that code reference stringifies to.
484 #pod
485 #pod If the value in C<$data_ref> is a reference reference, then the referenced
486 #pod scalar will be passed to String::Flogger, and the resulting string will be used
487 #pod as the value to log. That string will be quoted as described above, if needed.
488 #pod
489 #pod =cut
490  
491 3 ▶sub _compute_proxy_ctx_kvstr_aref ($) {
Subroutine _compute_proxy_ctx_kvstr_aref: called CC=1 SCAR=0.0
492 3 return [];
493 }
494  
495 7 my $LOG_FMT_PACKAGE;
496 0 ▶sub _log_fmt_package { $LOG_FMT_PACKAGE }
Subroutine _log_fmt_package: not called CC=1 SCAR=6.9
497  
498 7 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=12.7
499 7 $LOG_FMT_PACKAGE = 'Log::Fmt';
500 7 my $ok = eval { require Log::Fmt::XS; };
501 7 ▶ if ($ok && ! $ENV{LOG_FMT_NO_XS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok and not $ENV{"LOG_FMT_NO_XS"}) 0 7 7
Condition33%1/3
$ok and not $ENV{"LOG_FMT_NO_XS"}
!ll&&!rl&&r
700
MC/DC0%0/2
$ok and not $ENV{"LOG_FMT_NO_XS"}
$ok not $ENV{"LOG_FMT_NO_XS"}
Truth table33%1/3
A and not(B)
A= $okB= not $ENV{"LOG_FMT_NO_XS"}
ABresult
0X0
100
111
502 0 $LOG_FMT_PACKAGE = 'Log::Fmt::XS';
503 }
504 }
505  
506 15 ▶sub fmt_event ($self, $type, $data) {
Subroutine fmt_event: called CC=2 SCAR=6.9
Pod documented
507 my $kv_aref = $self->_log_fmt_package->_pairs_to_kvstr_aref([
508 event => $type,
509 15 ▶ (_ARRAY0($data) ? @$data : $data->%{ sort keys %$data })
Branch: 1 branch
BranchTrueFalseTotal
&_ARRAY0($data) ? : 8 7 15
510 ]);
511  
512 15 return join q{ }, @$kv_aref;
513 }
514  
515 15 ▶sub log_event ($self, $type, $data) {
Subroutine log_event: called CC=2 SCAR=7.0
Pod documented
516 15 ▶ return if $self->get_muted;
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_muted 0 15 15
517  
518 15 my $message = $self->fmt_event($type, $data);
519  
520 15 $self->dispatcher->log(
521 level => 'info',
522 message => $message,
523 );
524  
525 15 return;
526 }
527  
528 #pod =method log_debug_event
529 #pod
530 #pod This method is just like C<log_event>, but will log nothing unless the logger
531 #pod has its C<debug> property set to true.
532 #pod
533 #pod =cut
534  
535 3 ▶sub log_debug_event ($self, $type, $data) {
Subroutine log_debug_event: called CC=2 SCAR=9.2
Pod documented
536 3 ▶ return unless $self->get_debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_debug 3 0 3
537  
538 0 $self->log_event($type, $data);
539 }
540  
541 #pod =method set_debug
542 #pod
543 #pod $logger->set_debug($bool);
544 #pod
545 #pod This sets the logger's debug property, which affects the behavior of
546 #pod C<log_debug>.
547 #pod
548 #pod =cut
549  
550 0 ▶sub set_debug ($self, $bool) { $self->{debug} = $bool ? 1 : 0 }
Subroutine set_debug: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$bool ? : 0 0 0
551  
552 #pod =method get_debug
553 #pod
554 #pod This gets the logger's debug property, which affects the behavior of
555 #pod C<log_debug>.
556 #pod
557 #pod =cut
558  
559 16 ▶sub get_debug ($self) { return $self->{debug} }
Subroutine get_debug: called CC=1 SCAR=0.0
Pod documented
560  
561 #pod =method clear_debug
562 #pod
563 #pod This method does nothing, and is only useful for L<Log::Dispatchouli::Proxy>
564 #pod objects. See L<Methods for Proxy Loggers|/METHODS FOR PROXY LOGGERS>, below.
565 #pod
566 #pod =cut
567  
568 sub clear_debug ($) { }
569  
570 1 ▶sub mute ($self) { $self->{muted} = 1 }
Subroutine mute: called CC=1 SCAR=0.0
Pod undocumented
571 3 ▶sub unmute ($self) { $self->{muted} = 0 }
Subroutine unmute: called CC=1 SCAR=0.0
Pod undocumented
572  
573 #pod =method set_muted
574 #pod
575 #pod $logger->set_muted($bool);
576 #pod
577 #pod This sets the logger's muted property, which affects the behavior of
578 #pod C<log>.
579 #pod
580 #pod =cut
581  
582 0 ▶sub set_muted ($self, $bool) {
Subroutine set_muted: not called CC=2 SCAR=17.9
Pod documented
583 0 ▶ return ($self->{muted} = $bool ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$bool ? : 0 0 0
584 }
585  
586 #pod =method get_muted
587 #pod
588 #pod This gets the logger's muted property, which affects the behavior of
589 #pod C<log>.
590 #pod
591 #pod =cut
592  
593 61 ▶sub get_muted ($self) { return $self->{muted} }
Subroutine get_muted: called CC=1 SCAR=0.0
Pod documented
594  
595 #pod =method clear_muted
596 #pod
597 #pod This method does nothing, and is only useful for L<Log::Dispatchouli::Proxy>
598 #pod objects. See L<Methods for Proxy Loggers|/METHODS FOR PROXY LOGGERS>, below.
599 #pod
600 #pod =cut
601  
602 sub clear_muted ($) { }
603  
604 #pod =method get_prefix
605 #pod
606 #pod my $prefix = $logger->get_prefix;
607 #pod
608 #pod This method returns the currently-set prefix for the logger, which may be a
609 #pod string or code reference or undef. See L<Logger Prefix|/LOGGER PREFIX>.
610 #pod
611 #pod =method set_prefix
612 #pod
613 #pod $logger->set_prefix( $new_prefix );
614 #pod
615 #pod This method changes the prefix. See L<Logger Prefix|/LOGGER PREFIX>.
616 #pod
617 #pod =method clear_prefix
618 #pod
619 #pod This method clears any set logger prefix. (It can also be called as
620 #pod C<unset_prefix>, but this is deprecated. See L<Logger Prefix|/LOGGER PREFIX>.
621 #pod
622 #pod =cut
623  
624 49 ▶sub get_prefix ($self) { return $self->{prefix} }
Subroutine get_prefix: called CC=1 SCAR=0.0
Pod documented
625 5 ▶sub set_prefix ($self, $prefix) { $self->{prefix} = $prefix }
Subroutine set_prefix: called CC=1 SCAR=0.0
Pod documented
626 3 ▶sub clear_prefix ($self) { $self->unset_prefix }
Subroutine clear_prefix: called CC=1 SCAR=0.0
Pod documented
627 3 ▶sub unset_prefix ($self) { undef $self->{prefix} }
Subroutine unset_prefix: called CC=1 SCAR=0.0
Pod undocumented
628  
629 #pod =method ident
630 #pod
631 #pod This method returns the logger's ident.
632 #pod
633 #pod =cut
634  
635 7 ▶sub ident ($self) { $self->{ident} }
Subroutine ident: called CC=1 SCAR=0.0
Pod documented
636  
637 #pod =method config_id
638 #pod
639 #pod This method returns the logger's configuration id, which defaults to its ident.
640 #pod This can be used to make two loggers equivalent in Log::Dispatchouli::Global so
641 #pod that trying to reinitialize with a new logger with the same C<config_id> as the
642 #pod current logger will not throw an exception, and will simply do no thing.
643 #pod
644 #pod =cut
645  
646 0 ▶sub config_id ($self) { $self->{config_id} }
Subroutine config_id: not called CC=1 SCAR=6.9
Pod documented
647  
648 #pod =head1 METHODS FOR SUBCLASSING
649 #pod
650 #pod =head2 string_flogger
651 #pod
652 #pod This method returns the thing on which F<flog> will be called to format log
653 #pod messages. By default, it just returns C<String::Flogger>
654 #pod
655 #pod =cut
656  
657 49 ▶sub string_flogger ($) { 'String::Flogger' }
Subroutine string_flogger: called CC=1 SCAR=0.0
Pod documented
658  
659 #pod =head2 env_prefix
660 #pod
661 #pod This method should return a string used as a prefix to find environment
662 #pod variables that affect the logger's behavior. For example, if this method
663 #pod returns C<XYZZY> then when checking the environment for a default value for the
664 #pod C<debug> parameter, Log::Dispatchouli will first check C<XYZZY_DEBUG>, then
665 #pod C<DISPATCHOULI_DEBUG>.
666 #pod
667 #pod By default, this method returns C<()>, which means no extra environment
668 #pod variable is checked.
669 #pod
670 #pod =cut
671  
672 35 ▶sub env_prefix ($) { return; }
Subroutine env_prefix: called CC=1 SCAR=0.0
Pod documented
673  
674 #pod =head2 env_value
675 #pod
676 #pod my $value = $logger->env_value('DEBUG');
677 #pod
678 #pod This method returns the value for the environment variable suffix given. For
679 #pod example, the example given, calling with C<DEBUG> will check
680 #pod C<DISPATCHOULI_DEBUG>.
681 #pod
682 #pod =cut
683  
684 38 ▶sub env_value ($self, $suffix) {
Subroutine env_value: called CC=3 SCAR=11.0
Pod documented
685 38 my @path = grep { defined } ($self->env_prefix, 'DISPATCHOULI');
686  
687 38 for my $prefix (@path) {
688 39 my $name = join q{_}, $prefix, $suffix;
689 39 ▶ return $ENV{ $name } if defined $ENV{ $name };
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{$name} 5 34 39
690 }
691  
692 33 return;
693 }
694  
695 #pod =method flog_messages
696 #pod
697 #pod my $str = $logger->flog_messages($m1, $m2, ...);
698 #pod
699 #pod This returns the string that would have been logged if the given arguments had
700 #pod been passed to C<< $logger->log(...) >>, without regard for log level,
701 #pod debugging, or the like.
702 #pod
703 #pod Unlike using the logger's string flogger, this will include any relevant prefix
704 #pod strings.
705 #pod
706 #pod =method fmt_event
707 #pod
708 #pod my $str = $logger->fmt_event($event_type => $data_ref);
709 #pod
710 #pod This method is equivalent to C<flog_messages>, but for an event. It returns
711 #pod the string format of the event, including all relevant prefixes.
712 #pod
713 #pod =head1 METHODS FOR TESTING
714 #pod
715 #pod =head2 new_tester
716 #pod
717 #pod my $logger = Log::Dispatchouli->new_tester( \%arg );
718 #pod
719 #pod This returns a new logger that logs only C<to_self>. It's useful in testing.
720 #pod If no C<ident> arg is provided, one will be generated. C<log_pid> is off by
721 #pod default, but can be overridden.
722 #pod
723 #pod C<\%arg> is optional.
724 #pod
725 #pod =cut
726  
727 29 ▶sub new_tester ($class, $arg = undef) {
Subroutine new_tester: called CC=3 SCAR=11.0
Pod documented
728 29 ▶ $arg ||= {};
Condition100%2/2
$arg ||= {}
l!l
236
MC/DC100%1/1
$arg ||= {}
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
729  
730 29 return $class->new({
731 ident => "$$:$0",
732 log_pid => 0,
733 %$arg,
734 to_stderr => 0,
735 to_stdout => 0,
736 to_file => 0,
737 to_self => 1,
738 facility => undef,
739 });
740 }
741  
742 #pod =head2 events
743 #pod
744 #pod This method returns the arrayref of events logged to an array in memory (in the
745 #pod logger). If the logger is not logging C<to_self> this raises an exception.
746 #pod
747 #pod =cut
748  
749 47 ▶sub events ($self) {
Subroutine events: called CC=2 SCAR=7.2
Pod documented
750 Carp::confess "->events called on a logger not logging to self"
751 47 ▶ unless $self->{events};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"events"} 0 47 47
752  
753 47 return $self->{events};
754 }
755  
756 #pod =head2 clear_events
757 #pod
758 #pod This method empties the current sequence of events logged into an array in
759 #pod memory. If the logger is not logging C<to_self> this raises an exception.
760 #pod
761 #pod =cut
762  
763 33 ▶sub clear_events ($self) {
Subroutine clear_events: called CC=2 SCAR=7.1
Pod documented
764 Carp::confess "->events called on a logger not logging to self"
765 33 ▶ unless $self->{events};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"events"} 0 33 33
766  
767 33 $self->{events}->@* = ();
768 33 return;
769 }
770  
771 #pod =head1 METHODS FOR PROXY LOGGERS
772 #pod
773 #pod =head2 proxy
774 #pod
775 #pod my $proxy_logger = $logger->proxy( \%arg );
776 #pod
777 #pod This method returns a new proxy logger -- an instance of
778 #pod L<Log::Dispatchouli::Proxy> -- which will log through the given logger, but
779 #pod which may have some settings localized.
780 #pod
781 #pod C<%arg> is optional. It may contain the following entries:
782 #pod
783 #pod =for :list
784 #pod = proxy_prefix
785 #pod This is a prefix that will be applied to anything the proxy logger logs, and
786 #pod cannot be changed.
787 #pod = proxy_ctx
788 #pod This is data to be inserted in front of event data logged through the proxy.
789 #pod It will appear I<after> the C<event> key but before the logged event data. It
790 #pod can be in the same format as the C<$data_ref> argument to C<log_event>.
791 #pod = debug
792 #pod This can be set to true or false to change the proxy's "am I in debug mode?"
793 #pod setting. It can be changed or cleared later on the proxy.
794 #pod
795 #pod =cut
796  
797 11 ▶sub proxy_class ($) {
Subroutine proxy_class: called CC=1 SCAR=0.0
Pod undocumented
798 11 return 'Log::Dispatchouli::Proxy';
799 }
800  
801 11 ▶sub proxy ($self, $arg = undef) {
Subroutine proxy: called CC=7 SCAR=20.5
Pod documented
802 11 ▶ $arg ||= {};
Condition50%1/2
$arg ||= {}
l!l
110
MC/DC0%0/1
$arg ||= {}
$arg
Truth table50%1/2
A
A= $arg
Aresult
11
00
803  
804 my $proxy = $self->proxy_class->_new({
805 parent => $self,
806 logger => $self,
807 proxy_prefix => $arg->{proxy_prefix},
808 11 ▶ (exists $arg->{debug} ? (debug => ($arg->{debug} ? 1 : 0)) : ()),
Branch: 2 branches
BranchTrueFalseTotal
exists $arg->{'debug'} ? : 0 11 11
$arg->{'debug'} ? : 0 0 0
809 });
810  
811 11 ▶ if (my $ctx = $arg->{proxy_ctx}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = $arg->{"proxy_ctx"}) 8 3 11
812 $proxy->{proxy_ctx} = _ARRAY0($ctx)
813 ? [ @$ctx ]
814 8 ▶ : [ $ctx->%{ sort keys %$ctx } ];
Branch: 1 branch
BranchTrueFalseTotal
&_ARRAY0($ctx) ? : 1 7 8
815 }
816  
817 11 return $proxy;
818 }
819  
820 #pod =head2 parent
821 #pod
822 #pod =head2 logger
823 #pod
824 #pod These methods return the logger itself. (They're more useful when called on
825 #pod proxy loggers.)
826 #pod
827 #pod =cut
828  
829 1 ▶sub parent ($self) { $self }
Subroutine parent: called CC=1 SCAR=0.0
Pod documented
830 1 ▶sub logger ($self) { $self }
Subroutine logger: called CC=1 SCAR=0.0
Pod documented
831  
832 #pod =method dispatcher
833 #pod
834 #pod This returns the underlying Log::Dispatch object. This is not the method
835 #pod you're looking for. Move along.
836 #pod
837 #pod =cut
838  
839 62 ▶sub dispatcher ($self) { $self->{dispatcher} }
Subroutine dispatcher: called CC=1 SCAR=0.0
Pod documented
840  
841 #pod =method stdio_dispatcher_class
842 #pod
843 #pod This method is an experimental feature to allow you to pick an alternate
844 #pod dispatch class for stderr and stdio. By default, Log::Dispatch::Screen is
845 #pod used. B<This feature may go away at any time.>
846 #pod
847 #pod =cut
848  
849 0 ▶sub stdio_dispatcher_class ($self) {
Subroutine stdio_dispatcher_class: not called CC=1 SCAR=6.9
Pod documented
850 0 require Log::Dispatch::Screen;
851 0 return 'Log::Dispatch::Screen';
852 }
853  
854 #pod =head1 METHODS FOR API COMPATIBILITY
855 #pod
856 #pod To provide compatibility with some other loggers, most specifically
857 #pod L<Log::Contextual>, the following methods are provided. You should not use
858 #pod these methods without a good reason, and you should never subclass them.
859 #pod Instead, subclass the methods they call.
860 #pod
861 #pod =begin :list
862 #pod
863 #pod = is_debug
864 #pod
865 #pod This method calls C<get_debug>.
866 #pod
867 #pod = is_info
868 #pod
869 #pod = is_fatal
870 #pod
871 #pod These methods return true.
872 #pod
873 #pod = info
874 #pod
875 #pod = fatal
876 #pod
877 #pod = debug
878 #pod
879 #pod These methods redispatch to C<log>, C<log_fatal>, and C<log_debug>
880 #pod respectively.
881 #pod
882 #pod =end :list
883 #pod
884 #pod =cut
885  
886 11 ▶sub is_debug ($self) { $self->get_debug }
Subroutine is_debug: called CC=1 SCAR=0.0
Pod documented
887 0 ▶sub is_info ($) { 1 }
Subroutine is_info: not called CC=1 SCAR=6.9
Pod documented
888 0 ▶sub is_fatal ($) { 1 }
Subroutine is_fatal: not called CC=1 SCAR=6.9
Pod documented
889  
890 0 ▶sub info ($self, @rest) { $self->log(@rest); }
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
891 0 ▶sub fatal ($self, @rest) { $self->log_fatal(@rest); }
Subroutine fatal: not called CC=1 SCAR=6.9
Pod documented
892 0 ▶sub debug ($self, @rest) { $self->log_debug(@rest); }
Subroutine debug: not called CC=1 SCAR=6.9
Pod documented
893  
894 use overload
895 0 ▶ '&{}' => sub { my ($self) = @_; sub { $self->log(@_) } },
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
896 7 fallback => 1,
897 7 ;
898  
899 #pod =head1 SEE ALSO
900 #pod
901 #pod =for :list
902 #pod * L<Log::Dispatch>
903 #pod * L<String::Flogger>
904 #pod
905 #pod =cut
906  
907 7 1;
908  
909 __END__
↑ Summary Fmt.pm »