File Coverage

blib/lib/Piper/Role/Logger.pm
Criterion Covered Total %
statement 19 19 100.0
branch n/a
condition 6 6 100.0
subroutine 8 8 100.0
pod 2 2 100.0
total 35 35 100.0


line stmt bran cond sub pod time code
1             #####################################################################
2             ## AUTHOR: Mary Ehlers, regina.verbae@gmail.com
3             ## ABSTRACT: Role for logging and debugging in the Piper system
4             #####################################################################
5              
6             package Piper::Role::Logger;
7              
8 5     5   15689 use v5.10;
  5         14  
9 5     5   22 use strict;
  5         18  
  5         103  
10 5     5   30 use warnings;
  5         10  
  5         142  
11              
12 5     5   19 use Carp;
  5         9  
  5         352  
13 5     5   518 use Types::Common::Numeric qw(PositiveOrZeroNum);
  5         137812  
  5         49  
14              
15 5     5   2139 use Moo::Role;
  5         5  
  5         39  
16              
17             our $VERSION = '0.04'; # from Piper-0.04.tar.gz
18              
19             #TODO: Look into making this Log::Any-compatible
20              
21             #pod =head1 DESCRIPTION
22             #pod
23             #pod The role exists to support future subclassing and testing of the logging mechanism used by L.
24             #pod
25             #pod =head1 REQUIRES
26             #pod
27             #pod This role requires the definition of the below methods, each of which will be provided the following arguments:
28             #pod
29             #pod $segment # The pipeline segment calling the method
30             #pod $message # The message sent (a string)
31             #pod @items # Items that provide context to the message
32             #pod
33             #pod =head2 DEBUG
34             #pod
35             #pod This method is only called if the debug level of the segment is greater than zero.
36             #pod
37             #pod =cut
38              
39             requires 'DEBUG';
40              
41             around DEBUG => sub {
42             my ($orig, $self, $instance) = splice @_, 0, 3;
43             return unless $self->debug_level($instance);
44             $self->$orig($instance, @_);
45             };
46              
47             #pod =head2 ERROR
48             #pod
49             #pod This method should cause a C or C. It will do so automatically if not done explicitly, though with an extremely generic and unhelpful message.
50             #pod
51             #pod =cut
52              
53             requires 'ERROR';
54              
55             after ERROR => sub {
56             croak 'ERROR encountered';
57             };
58              
59             #pod =head2 INFO
60             #pod
61             #pod This method is only called if either the verbosity or debug levels of the segment are greater than zero.
62             #pod
63             #pod =cut
64              
65             requires 'INFO';
66              
67             around INFO => sub {
68             my ($orig, $self, $instance) = splice @_, 0, 3;
69             return unless $self->debug_level($instance) or $self->verbose_level($instance);
70             $self->$orig($instance, @_);
71             };
72              
73             #pod =head2 WARN
74             #pod
75             #pod This method should issue a warning (such as C or C).
76             #pod
77             #pod =cut
78              
79             requires 'WARN';
80              
81             #pod =head1 UTILITY METHODS
82             #pod
83             #pod =head2 debug_level($segment)
84             #pod
85             #pod =head2 verbose_level($segment)
86             #pod
87             #pod These methods should be used to determine the appropriate debug and verbosity levels for the logger. They honor the following environment variable overrides (if they exist) before falling back to the appropriate levels set by the given C<$segment>:
88             #pod
89             #pod PIPER_DEBUG
90             #pod PIPER_VERBOSE
91             #pod
92             #pod =cut
93              
94             sub debug_level {
95 2012   100 2012 1 34425 return $ENV{PIPER_DEBUG} // $_[1]->debug;
96             }
97              
98             sub verbose_level {
99 628   100 628 1 21675 return $ENV{PIPER_VERBOSE} // $_[1]->verbose;
100             }
101              
102             1;
103              
104             __END__