File Coverage

blib/lib/Piper/Logger.pm
Criterion Covered Total %
statement 22 22 100.0
branch 6 6 100.0
condition 2 3 66.6
subroutine 7 7 100.0
pod 2 2 100.0
total 39 40 97.5


line stmt bran cond sub pod time code
1             #####################################################################
2             ## AUTHOR: Mary Ehlers, regina.verbae@gmail.com
3             ## ABSTRACT: Logging and debugging message handler for Piper
4             #####################################################################
5              
6             package Piper::Logger;
7              
8 3     3   131513 use v5.10;
  3         8  
9 3     3   11 use strict;
  3         9  
  3         65  
10 3     3   11 use warnings;
  3         4  
  3         93  
11              
12 3     3   11 use Carp qw();
  3         3  
  3         40  
13             # use Data::Dump qw(dump); # required if needed
14              
15 3     3   514 use Moo;
  3         9277  
  3         17  
16              
17             with qw(Piper::Role::Logger);
18              
19             our $VERSION = '0.04'; # from Piper-0.04.tar.gz
20              
21             #pod =head1 CONSTRUCTOR
22             #pod
23             #pod =head2 new
24             #pod
25             #pod =head1 METHODS
26             #pod
27             #pod =head2 DEBUG($segment, $message, @items)
28             #pod
29             #pod This method is a no-op unless S<<< C<< $self->debug_level($segment) > 0 >> >>>.
30             #pod
31             #pod Prints an informational message to STDERR.
32             #pod
33             #pod Uses the method C to format the printed message according to the debug/verbose levels of C<$segment>.
34             #pod
35             #pod Labels the message by pre-pending 'Info: ' to the formatted message.
36             #pod
37             #pod =cut
38              
39             sub DEBUG {
40             my $self = shift;
41             $self->INFO(@_);
42             }
43              
44             #pod =head2 ERROR($segment, $message, @items)
45             #pod
46             #pod Prints an error to STDERR and dies via L.
47             #pod
48             #pod Uses the method C to format the printed message according to the debug/verbose levels of C<$segment>.
49             #pod
50             #pod Labels the message by pre-pending 'Error: ' to the formatted message.
51             #pod
52             #pod =cut
53              
54             sub ERROR {
55             my $self = shift;
56             Carp::croak('Error: '.$self->make_message(@_));
57             }
58              
59             #pod =head2 INFO($segment, $message, @items)
60             #pod
61             #pod This method is a no-op unless S<<< C<< $self->verbose_level($segment) > 0 >> >>> or S<<< C<< $self->debug_level($segment) > 0 >> >>>.
62             #pod
63             #pod Prints an informational message to STDERR.
64             #pod
65             #pod Uses the method C to format the printed message according to the debug/verbose levels of C<$segment>.
66             #pod
67             #pod Labels the message by pre-pending 'Info: ' to the formatted message.
68             #pod
69             #pod =cut
70              
71             sub INFO {
72             my $self = shift;
73             say STDERR 'Info: '.$self->make_message(@_);
74             }
75              
76             #pod =head2 WARN($segment, $message, @items)
77             #pod
78             #pod Prints a warning to STDERR via L.
79             #pod
80             #pod Uses the method C to format the printed message according to the debug/verbose levels of C<$segment>.
81             #pod
82             #pod Labels the message by pre-pending 'Warning: ' to the formatted message.
83             #pod
84             #pod =cut
85              
86             sub WARN {
87 16     16 1 6233 my $self = shift;
88 16         29 Carp::carp('Warning: '.$self->make_message(@_));
89             }
90              
91             #pod =head1 UTILITY METHODS
92             #pod
93             #pod =head2 make_message($segment, $message, @items)
94             #pod
95             #pod Formats and returns the message according to the debug/verbose levels of C<$segment> and the provided arguments.
96             #pod
97             #pod There are two-three parts to the message:
98             #pod
99             #pod segment_name: message
100             #pod
101             #pod The message part is simply C<$message> for all debug/verbose levels.
102             #pod
103             #pod The part is only included when the verbosity level of the segment is greater than 1. It is formatted by L.
104             #pod
105             #pod If the verbosity and debug levels are both 0, segment_name is simply the segment's C
106             #pod
107             #pod =cut
108              
109             sub make_message {
110 101     101 1 24212 my ($self, $segment, $message, @items) = @_;
111              
112 101 100       225 $message = ($self->verbose_level($segment) ? $segment->path : $segment->label)
    100          
113             . ($self->debug_level($segment) > 1 ? ' (' . $segment->id . '): ' : ': ')
114             . $message;
115              
116 101 100 66     323 if ($self->verbose_level($segment) > 1 and @items) {
117 58         942 require Data::Dump;
118              
119 58         3956 $message .= ' ' . Data::Dump::dump(@items);
120             }
121              
122 101         12725 return $message;
123             }
124              
125             #pod =head2 debug_level($segment)
126             #pod
127             #pod =head2 verbose_level($segment)
128             #pod
129             #pod These methods determine the appropriate debug and verbosity levels for the given $segment, while respecting any environment variable overrides.
130             #pod
131             #pod Available environment variable overrides:
132             #pod
133             #pod PIPER_DEBUG
134             #pod PIPER_VERBOSE
135             #pod
136             #pod =cut
137              
138             1;
139              
140             __END__