File Coverage

blib/lib/Log/Dispatch/Perl.pm
Criterion Covered Total %
statement 34 42 80.9
branch 3 16 18.7
condition 10 32 31.2
subroutine 5 5 100.0
pod 0 2 0.0
total 52 97 53.6


line stmt bran cond sub pod time code
1             package Log::Dispatch::Perl; # git description: v0.04-5-g9e5eec5
2             # ABSTRACT: Use core Perl functions for logging
3              
4 1     1   774 use base 'Log::Dispatch::Output';
  1         2  
  1         419  
5              
6             our $VERSION = '0.05';
7              
8             # be as strict and verbose as possible
9 1     1   240878 use strict;
  1         2  
  1         21  
10 1     1   5 use warnings;
  1         2  
  1         733  
11              
12             # initialize level name / number conversion hashes
13             my %LEVEL2NUM;
14             my %NUM2LEVEL;
15             do {
16             my @level2num= (
17             debug => 0,
18             info => 1,
19             notice => 2,
20             warning => 3,
21             error => 4,
22             err => 4, # MUST be after "error"
23             critical => 5,
24             crit => 5, # MUST be after "critical"
25             alert => 6,
26             emergency => 7,
27             emerg => 7, # MUST be after "emergency"
28             );
29             %LEVEL2NUM= @level2num;
30             %NUM2LEVEL= reverse @level2num; # order fixes double assignments
31             };
32              
33             # hide ourselves from Carp
34             my $havecarp= defined $Carp::VERSION;
35             unless ( $] < 5.008 ) {
36             $Carp::Internal{$_}= 1 foreach ( 'Log::Dispatch', 'Log::Dispatch::Output' );
37             }
38              
39             # action to actual code hash
40             my %ACTION2CODE;
41             %ACTION2CODE= (
42             '' => sub { undef },
43              
44             carp => $havecarp
45             ? \&Carp::carp
46             : sub {
47             $havecarp ||= require Carp;
48             $ACTION2CODE{carp}= \&Carp::carp;
49             goto &Carp::carp;
50             },
51              
52             cluck => $] < 5.008
53             ? sub {
54             $havecarp ||= require Carp;
55             ( my $m= Carp::longmess() )
56             =~ s#\s+Log::Dispatch::[^\n]+\n##sg;
57             return CORE::warn $_[0] . $m;
58             }
59             : sub {
60             $havecarp ||= require Carp;
61             return CORE::warn $_[0] . Carp::longmess();
62             },
63              
64             confess => $] < 5.008
65             ? sub {
66             $havecarp ||= require Carp;
67             ( my $m = Carp::longmess() )
68             =~ s#\s+Log::Dispatch::[^\n]+\n##sg;
69             return CORE::die $_[0] . $m;
70             }
71             : sub {
72             $havecarp ||= require Carp;
73             return CORE::die $_[0] . Carp::longmess();
74             },
75              
76             croak => $havecarp
77             ? \&Carp::croak
78             : sub {
79             $havecarp ||= require Carp;
80             $ACTION2CODE{croak}= \&Carp::croak;
81             goto &Carp::croak;
82             },
83              
84             die => sub { CORE::die @_ },
85              
86             warn => sub { CORE::warn @_ },
87             );
88              
89             # satisfy require
90             1;
91              
92             #-------------------------------------------------------------------------------
93             #
94             # Class methods
95             #
96             #-------------------------------------------------------------------------------
97             # new
98             #
99             # Required by Log::Dispatch::Output. Creates a new Log::Dispatch::Perl
100             # object
101             #
102             # IN: 1 class
103             # 2..N parameters as a hash
104             # OUT: 1 instantiated object
105              
106             sub new {
107 1     1 0 1195 my ( $class, %param )= @_;
108              
109             # do the basic initializations
110 1   33     7 my $self= bless {}, ref $class || $class;
111 1         7 $self->_basic_init( %param );
112              
113             # we have specific actions specified
114 1         118 my @action;
115 1 50       4 if ( my $actions= $param{action} ) {
116              
117             # check all actions specified
118 0         0 foreach my $level ( keys %{$actions} ) {
  0         0  
119 0         0 my $action= $actions->{$level};
120 0 0       0 $level= $NUM2LEVEL{$level} if exists $NUM2LEVEL{$level};
121              
122             # sanity check, store if ok
123 0         0 my $warn;
124             warn qq{"$level" is an unknown logging level, ignored\n"}, $warn++
125 0 0 0     0 if !exists $LEVEL2NUM{ $level || '' };
126             warn qq{"$action" is an unknown Perl action, ignored\n"}, $warn++
127 0 0       0 if !exists $ACTION2CODE{$action};
128 0 0       0 $action[$LEVEL2NUM{$level}]= $ACTION2CODE{$action}
129             if !$warn;
130             }
131             }
132              
133             # set the actions that have not yet been specified
134 1   33     5 $action[0] ||= $ACTION2CODE{''};
135 1   33     5 $action[1] ||= $ACTION2CODE{''};
136 1   33     4 $action[2] ||= $ACTION2CODE{warn};
137 1   33     6 $action[3] ||= $ACTION2CODE{warn};
138 1   33     4 $action[4] ||= $ACTION2CODE{die};
139 1   33     7 $action[5] ||= $ACTION2CODE{die};
140 1   33     4 $action[6] ||= $ACTION2CODE{confess};
141 1   33     4 $action[7] ||= $ACTION2CODE{confess};
142              
143             # save this setting
144 1         2 $self->{action}= \@action;
145              
146 1         3 return $self;
147             } #new
148              
149             #-------------------------------------------------------------------------------
150             #
151             # Instance methods
152             #
153             #-------------------------------------------------------------------------------
154             # log_message
155             #
156             # Required by Log::Dispatch. Log a single message.
157             #
158             # IN: 1 instantiated Log::Dispatch::Perl object
159             # 2..N hash with parameters as required by Log::Dispatch
160              
161             sub log_message {
162 8     8 0 5909 my ( $self, %param )= @_;
163              
164             # huh?
165 8         17 my $level= $param{level};
166 8 0 33     19 return if !exists $LEVEL2NUM{$level} and !exists $NUM2LEVEL{$level};
167              
168             # obtain level number
169 8         11 my $num= $LEVEL2NUM{$level};
170 8 50       13 $num= $level if !defined $num; # //=
171              
172             # set message
173 8         10 my $message= $param{message};
174 8 50       25 $message .= "\n" if substr( $message, -1, 1 ) ne "\n";
175 8         16 @_= ($message);
176              
177             # log it the right way
178 8         9 goto &{$self->{action}->[$num]};
  8         26  
179             } #log_message
180              
181             #-------------------------------------------------------------------------------
182              
183             __END__
184              
185             =pod
186              
187             =encoding UTF-8
188              
189             =head1 NAME
190              
191             Log::Dispatch::Perl - Use core Perl functions for logging
192              
193             =head1 VERSION
194              
195             version 0.05
196              
197             =head1 SYNOPSIS
198              
199             use Log::Dispatch::Perl ();
200              
201             my $dispatcher = Log::Dispatch->new;
202             $dispatcher->add( Log::Dispatch::Perl->new(
203             name => 'foo',
204             min_level => 'info',
205             action => { debug => '',
206             info => '',
207             notice => 'warn',
208             warning => 'warn',
209             error => 'die',
210             critical => 'die',
211             alert => 'croak',
212             emergency => 'croak',
213             },
214             ) );
215              
216             $dispatcher->warning( "This is a warning" );
217              
218             =head1 DESCRIPTION
219              
220             The "Log::Dispatch::Perl" module offers a logging alternative using standard
221             Perl core functions. It allows you to fall back to the common Perl
222             alternatives for logging, such as "warn" and "cluck". It also adds the
223             possibility for a logging action to halt the current environment, such as
224             with "die" and "croak".
225              
226             =head1 POSSIBLE ACTIONS
227              
228             The following actions are currently supported (in alphabetical order):
229              
230             =head2 (absent or empty string or undef)
231              
232             Indicates no action should be executed. Default for log levels "debug" and
233             "info".
234              
235             =head2 carp
236              
237             Indicates a "carp" action should be executed. See L<Carp/"carp">. Halts
238             execution.
239              
240             =head2 cluck
241              
242             Indicates a "cluck" action should be executed. See L<Carp/"cluck">. Does
243             B<not> halt execution.
244              
245             =head2 confess
246              
247             Indicates a "confess" action should be executed. See L<Carp/"confess">. Halts
248             execution.
249              
250             =head2 croak
251              
252             Indicates a "croak" action should be executed. See L<Carp/"croak">. Halts
253             execution.
254              
255             =head2 die
256              
257             Indicates a "die" action should be executed. See L<perlfunc/"die">. Halts
258             execution.
259              
260             =head2 warn
261              
262             Indicates a "warn" action should be executed. See L<perlfunc/"warn">. Does
263             B<not> halt execution.
264              
265             =head1 SUPPORT
266              
267             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Log-Dispatch-Perl>
268             (or L<bug-Log-Dispatch-Perl@rt.cpan.org|mailto:bug-Log-Dispatch-Perl@rt.cpan.org>).
269              
270             I am also usually active on irc, as 'ether' at C<irc.perl.org> and C<irc.freenode.org>.
271              
272             =head1 AUTHOR
273              
274             Elizabeth Mattijsen (liz@dijkmat.nl)
275              
276             =head1 CONTRIBUTOR
277              
278             =for stopwords Karen Etheridge
279              
280             Karen Etheridge <ether@cpan.org>
281              
282             =head1 COPYRIGHT AND LICENCE
283              
284             This software is copyright (c) 2020 by Elizabeth Mattijsen.
285              
286             This is free software; you can redistribute it and/or modify it under
287             the same terms as the Perl 5 programming language system itself.
288              
289             =cut