File Coverage

blib/lib/Bot/Cobalt/Logger.pm
Criterion Covered Total %
statement 32 32 100.0
branch 5 8 62.5
condition 1 3 33.3
subroutine 13 13 100.0
pod 0 4 0.0
total 51 60 85.0


line stmt bran cond sub pod time code
1             package Bot::Cobalt::Logger;
2             $Bot::Cobalt::Logger::VERSION = '0.021001';
3 6     6   14154 use strictures 2;
  6         1095  
  6         194  
4 6     6   889 use Carp;
  6         6  
  6         280  
5              
6 6     6   405 use Bot::Cobalt::Common ':types';
  6         6  
  6         32  
7 6     6   2095 use Bot::Cobalt::Logger::Output;
  6         11  
  6         154  
8              
9              
10 6     6   34 use Moo;
  6         6  
  6         18  
11              
12             has level => (
13             required => 1,
14             is => 'ro',
15             writer => 'set_level',
16             isa => Enum[qw/error warn info debug/],
17             );
18              
19             ## time_format / log_format are passed to ::Output
20             has time_format => (
21             lazy => 1,
22             is => 'rw',
23             isa => Str,
24             predicate => 'has_time_format',
25             trigger => sub {
26             my ($self, $val) = @_;
27             $self->output->time_format($val) if $self->has_output
28             },
29             );
30              
31             has log_format => (
32             lazy => 1,
33             is => 'rw',
34             isa => Str,
35             predicate => 'has_log_format',
36             trigger => sub {
37             my ($self, $val) = @_;
38             $self->output->log_format($val) if $self->has_output
39             },
40             );
41              
42              
43             has output => (
44             lazy => 1,
45             is => 'rwp',
46             predicate => 'has_output',
47             isa => InstanceOf['Bot::Cobalt::Logger::Output'],
48             builder => '_build_output',
49             );
50              
51             has _levmap => (
52             is => 'ro',
53             isa => HashRef,
54             builder => sub {
55             +{
56 3     3   7174 error => 1,
57             warn => 2,
58             info => 3,
59             debug => 4,
60             }
61             },
62             );
63              
64             sub _build_output {
65 2     2   940 my ($self) = @_;
66              
67 2         3 my %opts;
68              
69 2 50       9 $opts{log_format} = $self->log_format if $self->has_log_format;
70 2 50       9 $opts{time_format} = $self->time_format if $self->has_time_format;
71              
72 2         12 Bot::Cobalt::Logger::Output->new(
73             %opts
74             )
75             }
76              
77             sub _should_log {
78 11     11   1145 my ($self, $level) = @_;
79              
80 11   33     37 my $num_lev = $self->_levmap->{$level}
81             || confess "unknown level $level";
82              
83 11         26 my $accept = $self->_levmap->{ $self->level };
84              
85 11 100       128 $accept >= $num_lev ? 1 : 0
86             }
87              
88             sub _log_to_level {
89 6     6   11 my ($self, $level) = splice @_, 0, 2;
90              
91 6 50       12 $self->output->_write(
92             $level,
93             [ caller(1) ],
94             @_
95             ) if $self->_should_log($level);
96              
97 6         33 1
98             }
99              
100 1     1 0 199 sub debug { shift->_log_to_level( 'debug', @_ ) }
101 1     1 0 3 sub info { shift->_log_to_level( 'info', @_ ) }
102 3     3 0 30 sub warn { shift->_log_to_level( 'warn', @_ ) }
103 1     1 0 3 sub error { shift->_log_to_level( 'error', @_ ) }
104              
105             1;
106              
107             =pod
108              
109             =head1 NAME
110              
111             Bot::Cobalt::Logger - Log handler for Bot::Cobalt
112              
113             =head1 SYNOPSIS
114              
115             my $logger = Bot::Cobalt::Logger->new(
116             ## Required, one of: debug info warn error
117             level => 'info',
118            
119             ## Optional, passed to Bot::Cobalt::Logger::Output
120             time_format => "%Y/%m/%d %H:%M:%S"
121             log_format => "%time% %pkg% (%level%) %msg%"
122             );
123              
124             ## Add outputs
125             ## (See Bot::Cobalt::Logger::Output for details)
126             $logger->output->add(
127             'Output::File' =>
128             { file => $path_to_log },
129              
130             'Output::Term' =>
131             { },
132             );
133              
134             ## Log messages
135             $logger->debug("Debugging message", @more_info );
136             $logger->info("Informative message");
137             $logger->warn("Warning message");
138             $logger->error("Error message");
139              
140             =head1 DESCRIPTION
141              
142             This is the log handler for L.
143              
144             Configured outputs must be added before log messages actually go
145             anywhere (see the L). See L for
146             details.
147              
148             =head2 Log Levels
149              
150             A B is required at construction-time; messages logged to the
151             specified level or any level below it will be recorded.
152              
153             For example, a B of 'warn' will discard log messages to 'debug'
154             and 'info' and report only 'warn' and 'error' messages.
155              
156             Valid levels, from high to low:
157              
158             debug
159             info
160             warn
161             error
162              
163             These should be called as methods to log to the appropriate level:
164              
165             $logger->info("This is some information");
166              
167             If a list is provided, it will be concatenated with an empty space
168             between items:
169              
170             $logger->info("Some info", "more info");
171              
172             =head2 Methods
173              
174             =head3 level
175              
176             Returns the currently tracked log level.
177              
178             =head3 set_level
179              
180             Changes the current log level.
181              
182             =head3 time_format
183              
184             Sets a date/time formatting string to be fed to C -- see
185             L
186              
187             =head3 log_format
188              
189             Sets a formatting template string for log messages -- see
190             L
191              
192             =head3 output
193              
194             Returns the L object.
195              
196             =head1 AUTHOR
197              
198             Jon Portnoy
199              
200             =cut