Interact/Logger.pm

filter: Statementstmt 70.5% 48 / 68 Branchbran 31.8% 7 / 22 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 93.3% 14 / 15 Podpod 66.6% 2 / 3 totaltotal 63.7% 72 / 113 CC 13 SCAR 22.4
CC
13
Cov
60%
log
28.7
would_log
9.8
BUILDARGS
3.1
CRAP 9.4
« Interact/Transport/SSH.pm ↑ Summary Interact/Action.pm »
1 package Net::CLI::Interact::Logger;
2 {
3 1 $Net::CLI::Interact::Logger::VERSION = '2.143070';
4 }
5  
6 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Sub::Quote;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MooX::Types::MooseLike::Base qw(HashRef Bool ArrayRef Any);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Class::Mix qw(genpkg);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Log::Dispatch::Config; # loads Log::Dispatch
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Log::Dispatch::Configurator::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub BUILDARGS {
16 2 ▶ my ($class, @args) = @_;
Subroutine BUILDARGS: called CC=3 SCAR=11.4
Pod undocumented
17  
18 # accept single hash ref or naked hash
19 2 ▶ my $params = (ref {} eq ref $args[0] ? $args[0] : {@args});
Branch: 1 branch
BranchTrueFalseTotal
ref {} eq ref $args[0] ? : 2 0 2
20  
21 # back-compat for old attr name
22 2 ▶ $params->{log_stamp} = $params->{log_stamps} if exists $params->{log_stamps};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params->{"log_stamps"} 0 2 2
23  
24 2 return $params;
25 }
26  
27 1 has log_config => (
28 is => 'rw',
29 isa => HashRef,
30 builder => 1,
31 trigger => quote_sub(q{ $_[0]->_clear_logger }),
32 );
33  
34 sub _build_log_config {
35 return {
36 2 ▶ dispatchers => ['screen'],
Subroutine _build_log_config: called CC=1 SCAR=0.0
37 screen => {
38 class => 'Log::Dispatch::Screen',
39 min_level => 'debug',
40 },
41 };
42 }
43  
44 1 has _logger => (
45 is => 'ro',
46 isa => quote_sub(q{ $_[0]->isa('Log::Dispatch::Config') }),
47 builder => 1,
48 lazy => 1,
49 clearer => 1,
50 );
51  
52 # this allows each instance of this module to have its own
53 # wrapped logger with different configuration.
54 sub _build__logger {
55 0 ▶ my $self = shift;
Subroutine _build__logger: not called CC=1 SCAR=3.3
56  
57 0 my $anon_logger = genpkg();
58 {
59 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
60 0 @{"$anon_logger\::ISA"} = 'Log::Dispatch::Config';
61 }
62  
63 0 my $config = Log::Dispatch::Configurator::Any->new($self->log_config);
64 0 $anon_logger->configure($config);
65  
66 0 return $anon_logger->instance;
67 }
68  
69 1 has 'log_stamp' => (
70 is => 'rw',
71 isa => Bool,
72 default => quote_sub('1'),
73 );
74  
75 1 has 'log_category' => (
76 is => 'rw',
77 isa => Bool,
78 default => quote_sub('1'),
79 );
80  
81 has 'log_start' => (
82 is => 'ro',
83 isa => ArrayRef,
84 2 ▶ default => sub{ [gettimeofday] },
Subroutine __ANON__: called CC=1 SCAR=0.0
85 1 );
86  
87 has 'log_flags' => (
88 is => 'rw',
89 isa => Any, # FIXME 'ArrayRef|HashRef[Str]',
90 2 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
91 1 );
92  
93 1 my %code_for = (
94 debug => 0,
95 info => 1,
96 notice => 2,
97 warning => 3,
98 error => 4,
99 critical => 5,
100 alert => 6,
101 emergency => 7,
102 );
103  
104 sub would_log {
105 2 ▶ my ($self, $category, $level) = @_;
Subroutine would_log: called CC=6 SCAR=22.9
Pod documented
106 2 ▶ return 0 if !defined $category or !defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $category and defined $level 0 2 2
Condition33%1/3
defined $category and defined $level
!ll&&!rl&&r
002
MC/DC0%0/2
defined $category and defined $level
defined $category defined $level
Truth table33%1/3
A and B
A= defined $categoryB= defined $level
ABresult
0X0
100
111
107  
108 my $flags = (ref $self->log_flags eq ref []
109 2 ▶ ? { map {$_ => 'error'} @{$self->log_flags} }
Branch: 1 branch
BranchTrueFalseTotal
ref $self->log_flags eq ref [] ? : 0 2 2
110 : $self->log_flags
111 );
112  
113 2 ▶ return 0 if !exists $code_for{$level};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $code_for{$level} 0 2 2
114 2 ▶ return 0 if !exists $flags->{$category};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $flags->{$category} 2 0 2
115 0 return ($code_for{$level} >= $code_for{ $flags->{$category} });
116 }
117  
118 sub log {
119 2 ▶ my ($self, $category, $level, @msgs) = @_;
Subroutine log: called CC=6 SCAR=33.6
Pod documented
120 2 ▶ return unless $self->would_log($category, $level);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->would_log($category, $level) 2 0 2
121 0 @msgs = grep {defined} @msgs;
122 0 ▶ return unless scalar @msgs;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @msgs 0 0 0
123  
124 0 my $prefix = '';
125 0 ▶ $prefix .= sprintf "[%11s] ", sprintf "%.6f", (tv_interval $self->log_start, [gettimeofday])
Branch: 1 branch
BranchTrueFalseTotal
if $self->log_stamp 0 0 0
126 if $self->log_stamp;
127 0 ▶ $prefix .= (substr $category, 0, 2)
Branch: 1 branch
BranchTrueFalseTotal
if $self->log_category 0 0 0
128 if $self->log_category;
129  
130 0 my $suffix = '';
131 0 ▶ $suffix = "\n" if $msgs[-1] !~ m/\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $msgs[-1] =~ /\n$/ 0 0 0
132  
133 0 $self->_logger->$level($prefix . (' ' x (2 - $code_for{$level})), (join ' ', @msgs) . $suffix);
134 }
135  
136 1 1;
137  
138 # ABSTRACT: Per-instance multi-target logging, with categories
139  
140  
141 __END__
142 =pod
143  
144 =head1 NAME
145  
146 Net::CLI::Interact::Logger - Per-instance multi-target logging, with categories
147  
148 =head1 VERSION
149  
150 version 2.143070
151  
152 =head1 SYNOPSIS
153  
154 $logger->log($category, $level, @message);
155  
156 =head1 DESCRIPTION
157  
158 This module implements a generic logging service, based on L<Log::Dispatch>
159 but with additional options and configuration. Log messages coming from your
160 application are categorized, and each category can be enabled/disabled
161 separately and have its own log level (i.e. C<emergency> .. C<debug>). High
162 resolution timestamps can be added to log messages.
163  
164 =head1 DEFAULT CONFIGURATION
165  
166 Being based on L<Log::Dispatch::Config>, this logger can have multiple
167 targets, each configured for independent level thresholds. The overall default
168 configuration is to print log messages to the screen (console), with a minimum
169 level of C<debug>. Each category (see below) has its own log level as well.
170  
171 Note that categories, as discussed below, are arbitrary so if a category is
172 not explicitly enabled or disabled, it is assumed to be B<disabled>. If you
173 wish to invent a new category for your application, simply think of the name
174 and begin to use it, with a C<$level> and C<@message> as above in the
175 SYNOPSIS.
176  
177 =head1 INTERFACE
178  
179 =head2 log( $category, $level, @message )
180  
181 The combination of category and level determine whether the the log messages
182 are emitted to any of the log destinations. Destinations are set using the
183 C<log_config> method, and categories are configured using the C<log_flags>
184 method.
185  
186 The C<@message> list will be joined by a space character, and a newline
187 appended if the last message doesn't contain one itself. Messages are
188 prepended with the first character of their C<$category>, and then indented
189 proportionally to their C<$level>.
190  
191 =head2 log_config( \%config )
192  
193 A C<Log::Dispatch::Config> configuration (hash ref), meaning multiple log
194 targets may be specified with different minimum level thresholds. There is a
195 default configuration which emits messages to your screen (console) with no
196 minimum threshold:
197  
198 {
199 dispatchers => ['screen'],
200 screen => {
201 class => 'Log::Dispatch::Screen',
202 min_level => 'debug',
203 },
204 };
205  
206 =head2 log_flags( \@categories | \%category_level_map )
207  
208 The user is expected to specify which log categories they are interested in,
209 and at what levels. If a category is used in the application for logging but
210 not specified, then it is deemed B<disabled>. Hence, even though the default
211 destination log level is C<debug>, no messages are emitted until a category is
212 enabled.
213  
214 In the array reference form, the list should contain category names, and they
215 will all be mapped to the C<error> level:
216  
217 $logger->log_flags([qw/
218 network
219 disk
220 io
221 cpu
222 /]);
223  
224 In the hash reference form, the keys should be category names and the values
225 log levels from the list below (ordered such that each level "includes" the
226 levels I<above>):
227  
228 emergency
229 alert
230 critical
231 error
232 warning
233 notice
234 info
235 debug
236  
237 For example:
238  
239 $logger->log_flags({
240 network => 'info',
241 disk => 'debug',
242 io => 'critical',
243 cpu => 'debug',
244 });
245  
246 Messages at or above the specified level will be passed on to the
247 C<Log::Dispatch> target, which may then specify an overriding threshold.
248  
249 =head2 C< Net::CLI::Interact->default_log_categories() >>
250  
251 Not a part of this class, but the only way to retrieve a list of the current
252 log categories used in the L<Net::CLI::Interact> distribution source. Does not
253 take into account any log categories added by the user.
254  
255 =head2 log_stamp( $boolean )
256  
257 Enable (the default) or disable the display of high resolution interval
258 timestamps with each log message.
259  
260 =head2 log_category( $boolean )
261  
262 Enable (the default) or disable the display of the first letters of the
263 category name with each log message.
264  
265 =head2 log_start( [$seconds, $microseconds] )
266  
267 Time of the start for generating a time interval when logging stamps. Defaults
268 to the result of C<Time::HiRes::gettimeofday> at the point the module is
269 loaded, in list context.
270  
271 =head2 would_log( $category, $level )
272  
273 Returns True if, according to the current C<log_flags>, the given C<$category>
274 is enabled at or above the threshold of C<$level>, otherwise returns False.
275 Note that the C<Log::Dispatch> targets maintain their own thresholds as well.
276  
277 =head1 AUTHOR
278  
279 Oliver Gorwits <oliver@cpan.org>
280  
281 =head1 COPYRIGHT AND LICENSE
282  
283 This software is copyright (c) 2014 by Oliver Gorwits.
284  
285 This is free software; you can redistribute it and/or modify it under
286 the same terms as the Perl 5 programming language system itself.
287  
288 =cut
289  
« Interact/Transport/SSH.pm ↑ Summary Interact/Action.pm »