| 1 | package BusyBird::Log; | ||||||||||
| 2 | 19 | ▶ | use v5.8.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 19 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 19 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 19 | ▶ | use Exporter 5.57 qw(import); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 19 | our @EXPORT_OK = qw(bblog); | |||||||||
| 8 | 19 | our $Logger = \&default_logger; | |||||||||
| 9 | |||||||||||
| 10 | 0 | ▶ | sub default_logger { | ||||||||
|
Subroutine
default_logger: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 11 | 0 | my ($level, $msg) = @_; | |||||||||
| 12 | 0 | my ($caller_package) = caller(1); | |||||||||
| 13 | 0 | my $output = "$caller_package: $level: $msg"; | |||||||||
| 14 | 0 | ▶ | $output .= "\n" if $output !~ /[\n\r]$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 0 | print STDERR $output; | |||||||||
| 16 | } | ||||||||||
| 17 | |||||||||||
| 18 | 62 | ▶ | sub bblog { | ||||||||
|
Subroutine
bblog: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 19 | 62 | my ($level, $msg) = @_; | |||||||||
| 20 | 62 | ▶ | $Logger->($level, $msg) if defined $Logger; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | 19 | 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<BusyBird::Log> manages the logger singleton used in L<BusyBird>. | ||||||||||
| 54 | |||||||||||
| 55 | This module is used by some of the L<BusyBird> 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<bblog()> is called. | ||||||||||
| 77 | The subroutine is supposed to do the logging. | ||||||||||
| 78 | |||||||||||
| 79 | Setting this to C<undef> disables logging at all. | ||||||||||
| 80 | |||||||||||
| 81 | =head1 AUTHOR | ||||||||||
| 82 | |||||||||||
| 83 | Toshio Ito C<< <toshioito [at] cpan.org> >> | ||||||||||
| 84 | |||||||||||
| 85 | =cut | ||||||||||
| 86 | |||||||||||