File Coverage

blib/lib/Log/LogMethods.pm
Criterion Covered Total %
statement 206 236 87.2
branch 40 74 54.0
condition 4 6 66.6
subroutine 43 45 95.5
pod 10 12 83.3
total 303 373 81.2


line stmt bran cond sub pod time code
1             package Log::LogMethods;
2              
3 4     4   664 use Modern::Perl;
  4         10  
  4         24  
4 4     4   2153 use Time::HiRes qw(tv_interval gettimeofday);
  4         4348  
  4         24  
5 4     4   2495 use Ref::Util qw(is_plain_hashref is_blessed_hashref);
  4         4987  
  4         332  
6 4     4   31 use Scalar::Util qw(blessed);
  4         7  
  4         180  
7 4     4   22 use B qw(svref_2object);
  4         8  
  4         191  
8 4     4   33 use Scalar::Util qw(looks_like_number);
  4         10  
  4         190  
9 4     4   25 no warnings 'redefine';
  4         17  
  4         185  
10 4     4   3823 use Log::Log4perl;
  4         152587  
  4         24  
11 4     4   248 use Log::Log4perl::Level;
  4         12  
  4         21  
12             Log::Log4perl->wrapper_register(__PACKAGE__);
13 4     4   2765 use Moo::Role;
  4         57240  
  4         27  
14              
15 4     4   1731 use Carp qw(croak);
  4         11  
  4         175  
16 4     4   1650 use namespace::clean;
  4         35122  
  4         34  
17              
18             our $VERSION='1.008';
19             our $SKIP_TRIGGER=0;
20              
21             # used as a place holder for extended format data
22             our $CURRENT_CB;
23             BEGIN {
24              
25             # disable logging
26             #local $SIG{__WARN__}=sub { };
27              
28             # always should be before off
29 4     4   2243 Log::Log4perl::Logger::create_custom_level(qw( ALWAYS OFF));
30             }
31              
32 34     34 0 97 sub LOOK_BACK_DEPTH { 3; }
33              
34             our %LEVEL_MAP=(
35             OFF=>$OFF,
36             ALWAYS=>$ALWAYS,
37             FATAL=>$FATAL,
38             ERROR=>$ERROR,
39             WARN=>$WARN,
40             INFO=>$INFO,
41             DEBUG=>$DEBUG,
42             TRACE=>$TRACE,
43             );
44              
45             =pod
46              
47             =head1 NAME
48              
49             Log::LogMethods - Writes your logging code for you!
50              
51             =head1 SYNOPSIS
52              
53             package test_moo;
54              
55             use Moo;
56             BEGIN { with qw(Log::LogMethods) }
57             sub test_always : BENCHMARK_ALWAYS { ... }
58              
59             my $logger=Log::Log4perl->get_logger(__PACKAGE__);
60             my $class=new test_moo(logger=>$logger);
61              
62             =cut
63              
64             =head1 Log4Perl Sugested PatternLayout
65              
66             To get everything you were expecting from classes that extend this one, use the following PatternLayout:
67              
68             %H %P %d %p %f %k %S [%h] %s %b %j %B%n
69              
70             The above format will produce logs like this:
71              
72             d00nappu0019 108201 2017/03/13 18:36:45 INFO t/Log-LogMethods.t 292 test_header::res_info [HEADER TEST] Starting 1
73             d00nappu0019 108201 2017/03/13 18:36:45 ERROR t/Log-LogMethods.t 292 test_header::res_info [HEADER TEST] error message 1
74             d00nappu0019 108201 2017/03/13 18:36:45 INFO t/Log-LogMethods.t 292 test_header::res_info [HEADER TEST] Finished 1 elapsed 0.000362
75              
76             =head2 Log4Perl Custom PatternLayouts
77              
78             Since log4perl can get pertty confused with what the (package::method and line) number should be from Log::LogMethods, the following Custom PatternLayout have been added:
79              
80             +------------------------------------------------------+
81             | Layout | Replaces | Description |
82             +--------+----------+----------------------------------+
83             | %f | %F | File the alert came from |
84             | %s | %m | actual Message |
85             | %k | %L | Line Number ( if any ) |
86             | %S | | fully qualified package::method |
87             | %v | %C | package |
88             +--------+----------+----------------------------------+
89              
90             Special case PatternLayouts
91              
92             +--------+----------------------------------------+
93             | %h | Log Header value ( if any ) |
94             | %b | Benchmark recursion_level |
95             | %B | Benchmaked time in microseconds |
96             | %j | set to "elapsed" for benchmark methods |
97             +--------+----------------------------------------+
98              
99             =cut
100              
101             our %FORMAT_MAP=(
102             qw(
103             f filename
104             s msg
105             k line
106             h header
107             S sub
108             v package
109              
110             b recursion_level
111             B elapsed
112             j kw_elapsed
113             )
114             );
115              
116             while(my ($format,$key)=each %FORMAT_MAP) {
117             Log::Log4perl::Layout::PatternLayout::add_global_cspec($format,sub {
118             my ($layout, $msg, $category, $priority, $caller_level)=@_;
119              
120             my $hash=$CURRENT_CB;
121              
122             # make us a real drop in replacement!
123             unless(is_plain_hashref $hash) {
124             $hash=__PACKAGE__->strack_trace_to_level( $caller_level);
125             while($hash->{package} eq 'Log::Log4perl::Logger') {
126             ++$caller_level;
127             $hash=__PACKAGE__->strack_trace_to_level( $caller_level);
128             if($hash->{sub}=~ /^Log::Log4perl::Logger/s) {
129             $hash->{sub}=__PACKAGE__->strack_trace_to_level(1+ $caller_level)->{sub};
130             }
131             }
132             $hash->{msg}=$msg;
133             }
134             exists $hash->{$key} ? $hash->{$key} : '';
135             }
136             );
137             }
138              
139             =head1 DESCRIPTION
140              
141             This library provides a common logging interfcaes that expects: Log::Log4perl::Logger or something that extends those features.
142              
143             =head1 Get and set log levels
144              
145             If you want to manualy get/set log levels
146              
147             use Log::Log4perl::Level;
148              
149             if($self->level!=$WARN) { $self->level($WARN) }
150              
151             =cut
152              
153             sub level {
154 29     29 1 12806 my ($self,$level)=@_;
155            
156 29 100       710 if(defined($self->logger)) {
157 26 100       249 if(looks_like_number($level)) {
158 10         160 $self->logger->level($level);
159             }
160 26         10668 return $self->logger->level;
161             } else {
162 3         42 return;
163             }
164             }
165              
166             =head1 OO Methods provided
167              
168             This class adds the following arguments and accessors to any class that loads using 'with';
169              
170             logger: DOES(Log::Log4perl::Logger)
171              
172             When the object DOES Log::Log4perl::Logger, the correct Log::Log4perl->get_logger(__PACKAGE__) call will be done. If you wish to modify the logger method, use an around declaration. This will keep the trigger $self->_trigger_logger($logger|undef) in tact.
173              
174             Example:
175              
176             around logger=>sub {
177             my ($code,$self,$logger)=@_;
178              
179             if(defined($logger)) {
180              
181             # Do something here
182             return $org->($self,$logger);
183             } else {
184             return $org->($self);
185             }
186             };
187              
188              
189             If you wish to just disable the trigger globally, you just disable it using the following flag.
190              
191             $Log::LogMethods::SKIP_TRIGGER=1;
192              
193             =over 4
194              
195             =cut
196              
197             has logger=>(
198             is=>'rw',
199             isa=>sub {
200             my ($logger)=@_;
201             croak 'argument: logger must DOES(Log::Log4perl::Logger)' unless defined($logger);
202             croak 'argument: logger must DOES(Log::Log4perl::Logger)' unless $logger->DOES('Log::Log4perl::Logger')
203             },
204             trigger=>1,
205             );
206              
207             sub _trigger_logger {
208 2     2   30 my ($self,$logger)=@_;
209              
210 2 50       5 unless(defined($logger)) {
211 0 0       0 return undef unless exists $self->{logger};
212 0         0 return $self->{logger};
213             }
214 2 50       8 return $logger if $SKIP_TRIGGER;
215              
216 2 50       8 if($logger->DOES('Log::Log4perl::Logger')) {
217 2         9 my $class=blessed $self;
218 2 50       5 $class=$self unless defined($class);
219              
220             # create our logging class, if we wern't given the one for us
221 2         7 my $cat=$logger->category;
222 2         14 $class=~ s/::/./g;
223 2 100       5 if($cat ne $class) {
224 1         7 $self->log_debug("Logger->category eq '$cat', Creating our own: Log::Log4perl->get_logger('$class')");
225 1         5 my $our_logger=Log::Log4perl->get_logger($class);
226 1         355 $self->{logger}=$our_logger;
227 1         7 return $our_logger;
228             }
229             }
230              
231 1         5 return $logger;
232             };
233              
234             =item * $self->log_error("Some error");
235              
236             This is a lazy man's wrapper function for
237              
238             my $log=$self->logger;
239             $log->log_error("Some error") if $log;
240              
241             =cut
242              
243             sub log_error {
244 6     6 1 4828 my ( $self, @args ) = @_;
245              
246 6         15 $self->log_to_log4perl('ERROR',$self->LOOK_BACK_DEPTH,@args);
247              
248             }
249              
250             =item * $log->log_die("Log this and die");
251              
252             Logs the given message then dies.
253              
254             =cut
255              
256             sub log_die {
257 1     1 1 653 my ( $self, @args ) = @_;
258              
259 1         29 my $log = $self->logger;
260 1         9 my @list = ('DIE');
261 1 50       9 push @list, $self->log_header if $self->can('log_header');
262 1 50       5 return die join(' ',map { defined($_) ? $_ : 'undef' } @list,@args)."\n" if $self->log_to_log4perl('ERROR',$self->LOOK_BACK_DEPTH,@args);
  3 50       17  
263              
264 0         0 my $string=$self->format_log(@list,@args);
265              
266 0 0       0 return die $string unless $log;
267              
268 0         0 $self->log_to_log4perl('FATAL',$self->LOOK_BACK_DEPTH,@args);
269 0         0 die $string;
270             }
271              
272             sub format_log {
273 0     0 0 0 my ($self,@args)=@_;
274              
275 0 0       0 return join(' ',@args)."\n" unless $self->logger;
276 0         0 return $self->logger->format_log(@args);
277              
278             }
279              
280             =item * $self->log_always("Some msg");
281              
282             This is a lazy man's wrapper function for
283              
284             my $log=$self->logger;
285             $log->log_always("Some msg") if $log;
286              
287             =cut
288              
289             sub log_always {
290 6     6 1 5176 my ( $self, @args ) = @_;
291 6         23 $self->log_to_log4perl('ALWAYS',$self->LOOK_BACK_DEPTH,@args);
292             }
293              
294             =item * my $string=$self->log_header;
295              
296             This is a stub function that allows a quick addin for logging, the string returned will be inserted after the log_level in the log file if this function is created.
297              
298             =cut
299              
300             =item * $self->log_warn("Some msg");
301              
302             This is a lazy man's wrapper function for:
303              
304             my $log=$self->logger;
305             $log->log_warn("Some msg") if $log;
306              
307             =cut
308              
309             sub log_warn {
310 6     6 1 4866 my ( $self, @args ) = @_;
311              
312 6         17 $self->log_to_log4perl('WARN',$self->LOOK_BACK_DEPTH,@args);
313             }
314              
315             =item * $self->log_info("Some msg");
316              
317             This is a lazy man's wrapper function for:
318              
319             my $log=$self->logger;
320             $log->log_info("Some msg") if $log;
321              
322             =cut
323              
324             sub log_info {
325 8     8 1 4902 my ( $self, @args ) = @_;
326 8         25 $self->log_to_log4perl('INFO',$self->LOOK_BACK_DEPTH,@args);
327             }
328              
329             =item * $self->log_debug("Some msg");
330              
331             This is a lazy man's wrapper function for:
332              
333             my $log=$self->logger;
334             $log->log_debug("Some msg") if $log;
335              
336             =cut
337              
338             sub log_debug {
339 7     7 1 4903 my ( $self, @args ) = @_;
340 7         22 $self->log_to_log4perl('DEBUG',$self->LOOK_BACK_DEPTH,@args);
341             }
342              
343             =back
344              
345             =head2 ATTRIBUTES
346              
347             Logging attributes can be set for a given function. All logging wrappers autmatically log failed Data::Result objects as log_level ERROR.
348              
349             =head3 BASIC WRAPPERS
350              
351             These attributes provide the baseic Starting and Ending log entries for a given function.
352              
353             =over 4
354              
355             =cut
356              
357             =item * sub some_method : RESULT_ALWAYS { ... }
358              
359             Will always produce a start and end log entry
360              
361             =item * sub some_method : RESULT_ERROR { ... }
362              
363             Will always produce a starting and ending log entry at log level ERROR.
364              
365             =item * sub some_method : RESULT_WARN { ... }
366              
367             Will always produce a starting and ending log entry at log level WARN.
368              
369             =item * sub some_method : RESULT_INFO { ... }
370              
371             Will always produce a starting and ending log entry at log level INFO.
372              
373             =item * sub some_method : RESULT_DEBUG { ... }
374              
375             Will always produce a starting and ending log entry at log level DEBUG.
376              
377             =cut
378              
379             =back
380              
381             =head3 BENCHMARKING
382              
383             Functions can be declared with a given benchmark method.
384              
385             =over 4
386              
387             =item * BENCHMARK_INFO
388              
389             Declares Start and End log entries for the given function, along with a benchmark timestamp. Benchmark time differences are in microseconds.
390              
391             =cut
392              
393              
394             =item * sub method : BENCHMARK_ALWAYS { ... }
395              
396             Always benchmark this method.
397              
398             =item * sub method : BENCHMARK_ERROR { ... }
399              
400             Only benchmark this function if log level is >= ERROR
401              
402             =item * sub method : BENCHMARK_WARN { ... }
403              
404             Only benchmark this function if log level is >= WARN
405              
406             =item * sub method : BENCHMARK_INFO { ... }
407              
408             Only benchmark this function if log level is >= INFO
409              
410             =item * sub method : BENCHMARK_DEBUG { ... }
411              
412             Only benchmark this function if log level is >= DEBUG
413              
414             =back
415              
416             =head1 INTERNAL METHODS
417              
418             This section documents internal methods.
419              
420             =over 4
421              
422             =item * $self->MODIFY_CODE_ATTRIBUTES($code,$att)
423              
424             Method that generates the wrapper funcitons.
425              
426             Attrivutes:
427              
428             code: glob to overwrite
429             att: The Attribute being overwritten
430              
431             =cut
432              
433             sub MODIFY_CODE_ATTRIBUTES {
434              
435 60     60   17148 my ($self,$code,$attr)=@_;
436 60         156 my $trace=$self->strack_trace_to_level(2);
437            
438              
439 60         333 my $name=svref_2object($code)->GV->NAME;
440 60         203 $trace->{sub}="${self}::$name";
441 60         196 my ($type,$level)=split /_/,$attr;
442 60 50 33     416 return $attr unless exists $LEVEL_MAP{$level} and $type=~ m/^(?:BENCHMARK|RESULT)$/s;
443 60 50       346 croak "Cannot add $attr to ${self}::$name" if __PACKAGE__->can($name);
444              
445 60         130 my $lc=lc($type);
446 60         110 my $method="_attribute_${lc}_common";
447 60         169 my $ref=$self->$method($trace,$level,$code);
448              
449             #return $self->SUPER::MODIFY_CODE_ATTRIBUTES($self,$ref,$attr) if $self->can('SUPER::MODIFY_CODE_ATTRIBUTES');
450 60         212 return ();
451             }
452              
453              
454             =item * $self->_attribute_result_common( $stack,$level,$code );
455              
456             Compile time code, generates basic Startin Finsihed log messages for a given "LEVEL" and also creates ERROR Log entries if the object returned DOES('Data::Result') and is in an error state.
457              
458             Arguments:
459              
460             stack: stack hashref
461             level: level(WARN|ALWAYS|INFO|ERROR|TRACE|DEBUG)
462             code: code ref to replcae
463              
464             =cut
465              
466             sub _attribute_result_common {
467 20     20   46 my ($self,$stack,$level,$code)=@_;
468              
469 20         36 my $method=$stack->{sub};
470             my $ref=sub {
471 4     4   7200 use strict;
  4         10  
  4         129  
472 4     4   23 use warnings;
  4         9  
  4         975  
473 0     0   0 my ($self)=@_;
474              
475 0         0 my $log=$self->logger;
476 0         0 my $constant="LOG_$level";
477              
478 0         0 $self->log_to_log4perl($level,$stack,'Starting');
479              
480 0         0 my $result;
481 0 0       0 if(wantarray) {
482 0         0 $result=[$code->(@_)];
483 0 0       0 if($#{$result}==0) {
  0         0  
484 0         0 $self->data_result_auto_log_error($stack,$result->[0]);
485             }
486             } else {
487 0         0 $result=$code->(@_);
488 0         0 $self->data_result_auto_log_error($stack,$result);
489             }
490              
491 0         0 $self->log_to_log4perl($level,$stack,'Finished');
492              
493 0 0       0 return wantarray ? @{$result} : $result;
  0         0  
494 20         105 };
495 4     4   31 no strict;
  4         9  
  4         147  
496 4     4   29 no warnings 'redefine';
  4         9  
  4         662  
497 20         46 my $eval="*$method=\$ref";
498 20         1660 eval $eval;
499 20 50       109 croak $@ if $@;
500 20         47 return $ref;
501             }
502              
503             =item * $self->_attribute_benchmark_common( $stack,$level,$code);
504              
505             Compile time code, generates Benchmarking log for a given function: Startin Finsihed log messages for a given "LEVEL" and also creates ERROR Log entries if the object returned DOES('Data::Result') and is in an error state.
506              
507             Arguments:
508              
509             stack: stack hashref
510             level: level(WARN|ALWAYS|INFO|ERROR|TRACE|DEBUG)
511             code: code ref to replcae
512              
513             =cut
514              
515             sub _attribute_benchmark_common {
516 40     40   86 my ($self,$stack,$level,$code)=@_;
517              
518 40         77 my $method=$stack->{sub};
519 40         51 my $id=0;
520             my $ref=sub {
521 4     4   44 use strict;
  4         8  
  4         131  
522 4     4   25 use warnings;
  4         8  
  4         1219  
523 30     30   29195 my ($self)=@_;
524              
525 30         51 ++$id;
526 30         519 my $log=$self->logger;
527              
528 30         213 my $constant="LOG_$level";
529 30         103 my $t0 = [gettimeofday];
530 30         45 my $stack={%{$stack}};
  30         162  
531 30         62 $stack->{recursion_level}=$id;
532              
533 30         96 $self->log_to_log4perl($level,$stack,'Starting');
534              
535 30         45 my $result;
536 30 50       62 if(wantarray) {
537 0         0 $result=[$code->(@_)];
538 0 0       0 if($#{$result}==0) {
  0         0  
539 0         0 $self->data_result_auto_log_error($stack,$result->[0]);
540             }
541             } else {
542 30         95 $result=$code->(@_);
543 30         4706 $self->data_result_auto_log_error($stack,$result);
544             }
545              
546 30         351 my $elapsed = tv_interval ( $t0, [gettimeofday]);
547 30         406 $stack->{elapsed}=$elapsed;
548 30         99 $stack->{kw_elapsed}='elapsed';
549 30         82 $self->log_to_log4perl($level,$stack,'Finished');
550              
551 30         46 --$id;
552              
553 30 50       132 return wantarray ? @{$result} : $result;
  0         0  
554 40         239 };
555 4     4   51 no strict;
  4         9  
  4         142  
556 4     4   23 no warnings 'redefine';
  4         7  
  4         3519  
557 40         93 my $eval="*$method=\$ref";
558 40         3318 eval $eval;
559 40 50       222 croak $@ if $@;
560 40         102 return $ref;
561             }
562              
563             =item * $self->log_to_log4perl($level,$stack,@args)
564              
565             Low level Automatic logger selection.
566              
567             Arguments:
568              
569             level: Log level (ALWAYS|ERROR|WARN|INFO|DEBUG)
570             stack: number or hashref $trace
571             args: argument list for logging
572              
573             =cut
574              
575             =item * $self->data_result_auto_log_error($stack,$result);
576              
577             Creates a required log entry for a false Data::Result object
578              
579             Arguments:
580              
581             stack: level or $trace
582             result: Object, if DOES('Data::Result') and !$result->is_true a log entry is created
583              
584             =cut
585              
586             sub data_result_auto_log_error {
587 30     30 1 66 my ($self,$stack,$result)=@_;
588 30 50       86 if(is_blessed_hashref($result)) {
589 30 50       102 if($result->DOES('Data::Result')) {
590 30 100       517 $self->log_to_log4perl('ERROR',$stack,$result) unless $result->is_true;
591             }
592             }
593             }
594              
595             =item * my $strace=$self->strack_trace_to_level($number)
596              
597             Given the number, trturns the currect $trace
598              
599             trace
600              
601             sub: Name of the function
602             filename: source file
603             package: Package name
604             line: Line number
605              
606             =cut
607              
608             sub strack_trace_to_level {
609 152     152 1 312 my ($self, $level) = @_;
610              
611 152         254 my $hash = {};
612 152         964 @{$hash}{qw(package filename line sub)} = caller($level);
  152         524  
613              
614             # Look up the stack until we find something that explains who and what called us
615 152   100     877 LOOK_BACK_LOOP: while ( defined( $hash->{sub} ) and $hash->{sub} =~ /eval/ ) {
616              
617 2         8 my $copy = {%$hash};
618 2         6 @{$hash}{qw(package filename line sub)} = caller( ++$level );
  2         5  
619              
620             # give up when we have a dead package name
621 2 50       8 unless ( defined( $hash->{package} ) ) {
622              
623 2         5 $hash = $copy;
624 2         5 $hash->{eval} = 1;
625              
626 2         5 last LOOK_BACK_LOOP;
627             }
628             }
629              
630             # if we don't know where we were called from, we can assume main.
631 25         76 @{$hash}{qw(sub filename package line)} = ( 'main::', $0, 'main', 'undef' )
632 152 100       341 unless defined( $hash->{package} );
633              
634 152         278 $hash->{level}=$level;
635              
636 152         283 return $hash;
637             }
638              
639             =item * if($self->log_to_log4perl($level,$trace,@args)) { ... }
640              
641             Low Level check and log to log4perl logger object
642              
643             Arguments:
644              
645             level: Log Level (ALWAYS|ERROR|WARN|INFO|DEBUG)
646             trace: level number or $trace
647             args: list of strings to log
648              
649             =cut
650              
651             sub log_to_log4perl {
652 121     121 1 480 my ($self,$level,$trace,@args)=@_;
653              
654 121         2320 my $log=$self->logger;
655 121 50       819 return 0 unless defined($log);
656            
657 121         209 my $header=' ';
658 121 100       539 $header=' '.$self->log_header.' ' if $self->can('log_header');
659 121         262 foreach my $value (@args) {
660 121 50       276 $value='undef' unless defined($value);
661             }
662              
663 121 100       277 if(is_plain_hashref($trace)) {
664              
665             # this will be modified, so make a copy!
666 87         112 $trace={%{$trace}};
  87         442  
667             } else {
668 34         74 $trace=$self->strack_trace_to_level($trace);
669 34         78 $trace->{line}=$self->strack_trace_to_level($trace->{level} -1)->{line};
670             }
671            
672 121 100       441 $trace->{header}=$self->log_header if $self->can('log_header');
673 121         371 $trace->{msg}=join ' ',@args;
674              
675 121         524 my $id;
676 121 50       279 if(exists $LEVEL_MAP{$level}) {
677 121         208 $id=$LEVEL_MAP{$level};
678             } else {
679 0         0 $id=$LEVEL_MAP{OFF};
680             }
681 121         187 $CURRENT_CB=$trace;
682 121         448 $log->log($id,$trace->{msg});
683 121         18261 $CURRENT_CB=undef;
684 121         466 return 1;
685             }
686              
687             =back
688              
689             =head1 Method Generation
690              
691             This section documents the code generation methods
692              
693             =over 4
694              
695             =item * $self->_create_is_check($name,$level)
696              
697             Generates the "is_xxx" method based on $name and $level.
698              
699             Argumetns:
700              
701             name: Human readable word such as: DEBUG
702             level: Levels come from Log::Log4perl::Level
703              
704             =cut
705              
706             sub _create_is_check {
707 32     32   63 my ($self,$name,$level)=@_;
708              
709 32 100       110 my $method="is_".lc($name eq 'WARN' ? 'warning' : $name);
710             my $code=sub {
711 8     8   4067 my ($self)=@_;
712              
713 8         21 my $level=$self->level;
714 8 50       322 return 0 unless looks_like_number($level);
715 8         26 return $level == $Log::LogMethods::LEVEL_MAP{$name};
716 32         122 };
717              
718 4     4   32 no strict;
  4         9  
  4         142  
719 4     4   23 no warnings 'redefine';
  4         8  
  4         1100  
720 32         1986 eval "*$method=\$code";
721             }
722              
723             =item * $self->_create_logging_methods($name,$level)
724              
725             Generates the logging methods based on $name and $level.
726              
727             Argumetns:
728              
729             name: Human readable word such as: DEBUG
730             level: Levels come from Log::Log4perl::Level
731              
732             =cut
733              
734             sub _create_logging_methods {
735 32     32   75 my ($self,$name,$level)=@_;
736 32 100       98 my $method=lc($name eq 'WARN' ? 'warning' : $name);
737             my $code=sub {
738 12     12   13052 my ($self,@args)=@_;
739              
740 12         41 my $trace=$self->strack_trace_to_level(2);
741 12         33 $trace->{line}=$self->strack_trace_to_level($trace->{level} -1)->{line};
742              
743 12         42 return $self->log_to_log4perl($name,$trace,@args);
744 32         161 };
745 32         1874 eval "*$method=\$code";
746             }
747              
748              
749             while(my ($name,$level)=each %Log::LogMethods::LEVEL_MAP) {
750             __PACKAGE__->_create_is_check($name,$level);
751             __PACKAGE__->_create_logging_methods($name,$level);
752             }
753              
754             =back
755              
756             =head2 log level checks
757              
758             The logging and is_xxx methods are auto generated based on the key/value pairs in %Log::LogMethods::LEVEL_MAP.
759              
760             =over 4
761              
762             =item * if($self->is_always) { ... }
763              
764             =item * if($self->is_error) { ... }
765              
766             =item * if($self->is_warning) { ... }
767              
768             =item * if($self->is_info) { ... }
769              
770             =item * if($self->is_debug) { ... }
771              
772             =item * if($self->is_default_debug) { ... }
773              
774             =item * if($self->is_trace) { ... }
775              
776             =back
777              
778             =head2 Logging methods
779              
780             The following methods are autogenerated based on the key/value pairs in %Log::LogMethods::LEVEL_MAP.
781              
782             =over 4
783              
784             =item * $self->always("Some log entry")
785              
786             =item * $self->error("Some log entry")
787              
788             =item * $self->warning("Some log entry")
789              
790             =item * $self->info("Some log entry")
791              
792             =item * $self->debug("Some log entry")
793              
794             =item * $self->default_debug("Some log entry")
795              
796             =item * $self->trace("Some log entry")
797              
798             =back
799              
800             =head1 AUTHOR
801              
802             Mike Shipper <AKALINUX@CPAN.ORG>
803              
804             =cut
805              
806             1;