File Coverage

blib/lib/BusyBird/Log.pm
Criterion Covered Total %
statement 14 19 73.6
branch 2 4 50.0
condition n/a
subroutine 5 6 83.3
pod 1 2 50.0
total 22 31 70.9


line stmt bran cond sub pod time code
1             package BusyBird::Log;
2 19     19   50430 use v5.8.0;
  19         52  
  19         823  
3 19     19   85 use strict;
  19         28  
  19         649  
4 19     19   89 use warnings;
  19         28  
  19         811  
5 19     19   84 use Exporter 5.57 qw(import);
  19         277  
  19         4571  
6              
7             our @EXPORT_OK = qw(bblog);
8             our $Logger = \&default_logger;
9              
10             sub default_logger {
11 0     0 0 0 my ($level, $msg) = @_;
12 0         0 my ($caller_package) = caller(1);
13 0         0 my $output = "$caller_package: $level: $msg";
14 0 0       0 $output .= "\n" if $output !~ /[\n\r]$/;
15 0         0 print STDERR $output;
16             }
17              
18             sub bblog {
19 52     52 1 1689 my ($level, $msg) = @_;
20 52 100       219 $Logger->($level, $msg) if defined $Logger;
21             }
22              
23             1;
24              
25             =pod
26              
27             =head1 NAME
28              
29             BusyBird::Log - simple logging infrastructure for BusyBird
30              
31             =head1 SYNOPSIS
32              
33              
34             use BusyBird::Log qw(bblog);
35            
36             bblog('error', 'Something bad happens');
37            
38             {
39             my @logs = ();
40            
41             ## Temporarily change the Logger
42             local $BusyBird::Log::Logger = sub {
43             my ($level, $msg) = @_;
44             push(@logs, [$level, $msg]);
45             };
46              
47             bblog('info', 'This goes to @logs array.');
48             }
49              
50              
51             =head1 DESCRIPTION
52              
53             L manages the logger singleton used in L.
54              
55             This module is used by some of the L component modules to log warning/error messages.
56              
57              
58             =head1 EXPORTABLE FUNCTIONS
59              
60             The following functions are exported only by request.
61              
62             =head2 bblog($level, $msg)
63              
64             Logs the given message.
65              
66             C<$level> is a string of log level such as 'info', 'warn', 'error', 'critical' etc.
67             C<$msg> is the log message body.
68              
69             By default, it prints the log to STDERR.
70              
71              
72             =head1 PACKAGE VARIABLES
73              
74             =head2 $BusyBird::Log::Logger = CODEREF($level, $msg)
75              
76             A subroutine reference that is called when C is called.
77             The subroutine is supposed to do the logging.
78              
79             Setting this to C disables logging at all.
80              
81             =head1 AUTHOR
82              
83             Toshio Ito C<< >>
84              
85             =cut
86