blib/lib/Mojo/Log/Che.pm

filter: Statementstmt 95.0% 77 / 81 Branchbran 75.0% 33 / 44 Conditioncond 58.3% 14 / 24 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 15 / 15 Podpod 100% 4 / 4 totaltotal 79.6% 145 / 182 CC 31 SCAR 18.5
CC
31
Cov
83%
_message
11.6
handler
8.0
_trace
7.7
CRAP 6.4
↑ Summary
1 package Mojo::Log::Che;
2 3 ▶use Mojo::Base 'Mojo::Log';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Fcntl ':flock';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 #~ use Debug::LTrace qw/Mojo::Log::_log/;;
8 #~ use Carp::Trace;
9 #~ use Encode qw(decode_utf8);
10 3 ▶use Mojo::Util 'encode';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 #~ binmode STDERR, ":utf8";
12  
13 6 ▶has paths => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
14 9 ▶has handlers => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
15 3 has trace => 4;
16 #~ has parent => undef, weak => 1;
17  
18 # Standard log levels
19 3 my %LEVEL = (debug => 1, info => 2, warn => 3, error => 4, fatal => 5);
20  
21 14 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
22 14 my $self = shift->SUPER::new(format => \&_format, @_);
23 14 $self->unsubscribe('message');
24 14 $self->on(message => \&_message);
25 14 return $self;
26 }
27  
28 1 ▶sub context {# override
Subroutine context: called CC=2 SCAR=7.1
Pod documented
29 1 my ($self, @context) = @_;
30 #~ push @{ $self->{context} ||= [] }, @context;
31 $self->{context} = \@context
32 1 ▶ if @context;
Branch: 1 branch
BranchTrueFalseTotal
if @context 1 0 1
33 1 return $self;
34 #~ return $self->new(parent => $self->parent || $self, context => \@context, level => $self->level);
35 }
36  
37 30 ▶sub handler {
Subroutine handler: called CC=8 SCAR=20.8
Pod documented
38 30 my ($self, $level) = @_;
39
40 30 my $handler = $self->handlers->{$level};
41 30 ▶ return $handler
Branch: 1 branch
BranchTrueFalseTotal
if $handler 1 29 30
42 if $handler;
43
44 29 my $path = shift->path;
45 29 my $path_level = $self->paths->{$level};
46 29 ▶ my $is_dir = -d -w $path
Branch: 1 branch
BranchTrueFalseTotal
if $path 18 11 29
47 if $path;
48
49 29 my $file;
50 29 ▶ if ($is_dir) {# DIR
Branch: 2 branches
BranchTrueFalseTotal
if ($is_dir) { } 6 23 29
elsif ($path_level) { } 3 20 23
51 # relative path for level
52 6 ▶ chop($path)
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[/$]u 0 6 6
53 if $path =~ /\/$/;
54
55 6 ▶ $file = sprintf "%s/%s", $path, $path_level ||"$level.log";
Condition100%2/2
$path_level || "$level.log"
l!l
24
MC/DC100%1/1
$path_level || "$level.log"
$path_level
Truth table100%2/2
A
A= $path_level
Aresult
11
00
56 }
57 elsif ($path_level) {# absolute FILE for level
58 3 $file = $path_level;
59 }
60 else {
61 #~ warn "Cant create log handler for level=[$level] and path=[$path] (also check filesystem permissions)";
62 20 return; # Parent way to handle
63 }
64
65 9 ▶ $handler = Mojo::File->new($file)->open('>>')#:encoding(UTF-8)
Branch: 1 branch
BranchTrueFalseTotal
$handler = "Mojo::File"->new($file)->open(">>") or croak("Cant create log handler for [$file]") 9 0 9
66 or croak "Cant create log handler for [$file]";
67
68 9 $self->handlers->{$level} = $handler;
69
70 9 return $handler;
71 };
72  
73 30 ▶sub append {
Subroutine append: called CC=4 SCAR=14.5
Pod documented
74 30 my ($self, $msg, $handle) = @_;
75  
76 30 ▶ return unless $handle ||= $self->handle;
Branch: 1 branch
BranchTrueFalseTotal
unless $handle ||= $self->handle 0 30 30
Condition66%2/3
$handle ||= $self->handle
l!l&&r!l&&!r
10200
MC/DC0%0/2
$handle ||= $self->handle
$handle $self->handle
Truth table66%2/3
A ||= B
A= $handleB= $self->handle
ABresult
1X1
011
000
77 30 flock $handle, LOCK_EX;
78 30 ▶ $handle->print(encode('UTF-8', $msg))#
Branch: 1 branch
BranchTrueFalseTotal
$handle->print(encode("UTF-8", $msg)) or croak("Can't write to log: $!") 30 0 30
79 or croak "Can't write to log: $!";
80 30 flock $handle, LOCK_UN;
81 }
82  
83 3 my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
84 #~ my @wday = qw(Sn Mn Ts Wn Th Fr St);
85 30 ▶sub _format {
Subroutine _format: called CC=5 SCAR=16.1
86 30 my ($time, $level) = (shift, shift);
87 30 ▶ $level = '['.($LEVEL{$level} ? ($level =~ /^(\w)/)[0] : $level) . '] ' #"[$level] "
Branch: 2 branches
BranchTrueFalseTotal
if $level //= "" 20 10 30
$LEVEL{$level} ? : 17 3 20
Condition50%1/2
$level //= ""
l!l
300
MC/DC0%0/1
$level //= ""
$level
Truth table50%1/2
A
A= $level
Aresult
11
00
88 if $level //= '';
89
90 30 my ($sec,$min,$hour,$mday,$mon,$year,$wday) = localtime($time);
91 30 ▶ $time = sprintf "%s %s %s:%s:%s", $mday, map(length == 1 ? "0$_" : $_, $mon[$mon], $hour, $min, $sec);#$wday[$wday],
Branch: 1 branch
BranchTrueFalseTotal
length $_ == 1 ? : 30 90 120
92
93 30 return "$time $level" . join "\n", @_, '';
94 }
95  
96 27 ▶sub _trace {
Subroutine _trace: called CC=4 SCAR=20.4
97 27 ▶ my $start = shift // 1;
Condition50%1/2
shift() // 1
l!l
270
MC/DC0%0/1
shift() // 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
98 27 my @call = caller($start);
99 return \@call
100 27 ▶ if @call;
Branch: 1 branch
BranchTrueFalseTotal
if @call 27 0 27
101 #~ my @frames;
102 0 $start = 1;
103 #~ while (my @trace = caller($start++)) { push @call, \@trace }
104 #~ return pop @call;
105 0 ▶ while (@call = caller($start++)) { 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (@call = caller $start++) 0 0 0
106 #~ return $frames[4];
107 0 return \@call;
108 }
109  
110  
111  
112 30 ▶sub _message {
Subroutine _message: called CC=11 SCAR=24.5
113 30 my ($self, $level) = (shift, shift);
114
115 #~ warn @{$self->{context}} if $self->{context};
116  
117 30 ▶ return unless !$LEVEL{$level} || $self->is_level($level);
Branch: 1 branch
BranchTrueFalseTotal
unless not $LEVEL{$level} or $self->is_level($level) 0 30 30
Condition66%2/3
not $LEVEL{$level} or $self->is_level($level)
l!l&&r!l&&!r
6240
MC/DC0%0/2
not $LEVEL{$level} or $self->is_level($level)
not $LEVEL{$level} $self->is_level($level)
Truth table66%2/3
not(A) or B
A= not $LEVEL{$level}B= $self->is_level($level)
ABresult
1X1
011
000
118  
119 30 my $max = $self->max_history_size;
120 30 my $history = $self->history;
121 30 my $time = time;
122 30 ▶ my $trace = _trace($self->trace)
Branch: 1 branch
BranchTrueFalseTotal
if $self->trace 27 3 30
123 if $self->trace;
124
125 1 splice(@_,0, scalar @{$self->{context}},)
126 1 and $_[0]= join(' ', @{$self->{context}}, $_[0])
127 30 ▶ if $self->{context};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"context"} 1 29 30
Condition50%3/6
$self->{'context'} && (splice(@_, 0, scalar @{$self->{'context'};}) && ($_[0] = join(' ', @{$self->{'context'};}, $_[0])))
!ll&&!rl&&r
2910
splice @_, 0, scalar @{$self->{"context"};} and $_[0] = join(" ", @{$self->{"context"};}, $_[0])
!ll&&!rl&&r
010
MC/DC0%0/3
$self->{'context'} && (splice(@_, 0, scalar @{$self->{'context'};}) && ($_[0] = join(' ', @{$self->{'context'};}, $_[0])))
$self->{'context'} splice @_, 0, scalar @{$self->{"context"};} $_[0] = join(" ", @{$self->{"context"};}, $_[0])
Truth table0%0/4
A && B and C
A= $self->{'context'}B= splice @_, 0, scalar @{$self->{"context"};}C= $_[0] = join(" ", @{$self->{"context"};}, $_[0])
ABCresult
0XX0
10X0
1100
1111
128  
129 30 ▶ unshift @_, "$$〉". join(":", @$trace[$$trace[0] eq 'main' ? (1,2) : (0,2)]). ' ' . shift
Branch: 2 branches
BranchTrueFalseTotal
if $trace and @$trace 27 3 30
$trace->[0] eq 'main' ? : 27 0 27
Condition66%2/3
$trace and @$trace
!ll&&!rl&&r
3027
MC/DC50%1/2
$trace and @$trace
$trace @$trace
Truth table66%2/3
A and B
A= $traceB= @$trace
ABresult
0X0
100
111
130 if $trace && @$trace;
131 30 push @$history, my $msg = [$time, $level, @_];
132 30 ▶ shift @$history while @$history > $max;
Branch: 1 branch
BranchTrueFalseTotal
@$history > $max and do { shift @$history } 1 30 31
133
134 30 ▶ if (my $handle = $self->handler($level)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $handle = $self->handler($level)) 10 20 30
135 10 return $self->append($self->format->($time, '', @_), $handle);
136 }
137  
138 # as parent
139 20 return $self->append($self->format->(@$msg));
140
141 }
142  
143 6 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=12.0
144 6 my $self = shift;
145  
146 6 my ($package, $method) = our $AUTOLOAD =~ /^(.+)::(.+)$/;
147 6 ▶ Carp::croak "Undefined log level(subroutine) &${package}::$method called"
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) and $self->isa(__PACKAGE__) 0 6 6
Condition33%1/3
builtin::blessed($self) and $self->isa(__PACKAGE__)
!ll&&!rl&&r
006
MC/DC0%0/2
builtin::blessed($self) and $self->isa(__PACKAGE__)
builtin::blessed($self) $self->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= builtin::blessed($self)B= $self->isa(__PACKAGE__)
ABresult
0X0
100
111
148 unless Scalar::Util::blessed $self && $self->isa(__PACKAGE__);
149  
150 6 return $self->_log(@_, $method);
151
152 }
153  
154 3 our $VERSION = '0.902';# as to Mojolicious version/10+<child minor>
155  
156 =encoding utf8
157  
158 Доброго всем
159  
160 =head1 Mojo::Log::Che
161  
162 I<¡ ¡ ¡ ALL GLORY TO GLORIA ! ! !>
163  
164 =head1 VERSION
165  
166 0.902 (up to Mojolicious version/10+C<child minor>)
167  
168 =head1 NAME
169  
170 Mojo::Log::Che - Little child of great parent Mojo::Log.
171  
172 =head1 SYNOPSIS
173  
174 Parent Mojo::Log behavior just works
175  
176 use Mojo::Log::Che;
177 my $log = Mojo::Log::Log->new(path => '/var/log/mojo.log', level => 'warn');
178 $log->debug(...);
179  
180 =head2 EXTENDED THINGS
181  
182 # Set "path" to folder + have default "paths" for levels (be sure that mkdir /var/log/mojo)
183 my $log = Mojo::Log::Log->new(path => '/var/log/mojo');
184 $log->warn(...);# log to /var/log/mojo/warn.log
185 $log->error(...); # log to /var/log/mojo/error.log
186 $log->foo(...);# log to /var/log/mojo/foo.log
187
188 # set "path" to folder + set custom relative "paths" (be sure that mkdir /var/log/mojo)
189 my $log = Mojo::Log::Log->new(path => '/var/log/mojo', paths=>{debug=>'dbg.log', foo=>'myfoo.log'});
190 $log->debug(...); # log to /var/log/mojo/dbg.log
191 $log->warn(...);# log to /var/log/mojo/warn.log
192 $log->foo(...);# log to /var/log/mojo/myfoo.log
193
194 # set "path" to file + have default "paths" for levels
195 # this is standard Mojo::Log behavior + custom level/method also
196 my $log = Mojo::Log::Log->new(path => '/var/log/mojo.log');
197 $log->debug(...); # log to /var/log/mojo.log
198 $log->warn(...);# log to /var/log/mojo.log
199 $log->foo(...);# log to /var/log/mojo.log
200
201 # set "path" to file + set custom absolute "paths"
202 my $log = Mojo::Log::Log->new(path => '/var/log/mojo.log', paths => {error=>'/var/log/mojo.error.log'});
203 $log->debug(...); # log to /var/log/mojo.log
204 $log->foo(...);# log to /var/log/mojo.log
205 $log->error(...); # log to /var/log/mojo.error.log
206
207 # Log to STDERR + set custom absolute "paths"
208 $log->path(undef); # none path
209 $log->level('info');
210 $log->paths->{'error'} = '/var/log/error.log'; # absolute file only for error level
211 $log->error(...); # log to /var/log/error.log
212 $log->info(...); # log to STDERR
213 $log->debug(...); # no log
214 $log->foo(...); # anyway log to STDERR
215
216  
217 =head1 DESCRIPTION
218  
219 This B<Mojo::Log::Che> is a extended logger module for L<Mojo> projects.
220  
221 =head1 EVENTS
222  
223 B<Mojo::Log::Che> inherits all events from L<Mojo::Log> and override following ones.
224  
225 =head2 message
226  
227 See also parent L<Mojo::Log/"message">. Extends parent module logics for switching handlers.
228  
229 =head1 ATTRIBUTES
230  
231 B<Mojo::Log::Che> inherits all attributes from L<Mojo::Log and implements the following new ones.
232  
233 =head2 handlers
234  
235 Hashref of created file handlers for standard and custom levels. For standard parent L<Mojo::Log> logic none handlers but L<Mojo::Log/"handle"> will be in the scene.
236  
237 $log->handlers->{'foo'} = IO::Handle->new();
238  
239 =head2 path
240  
241 See parent L<Mojo::Log/"path">. Can set to folder and file path.
242  
243 =head2 paths
244  
245 Hashref map level names to absolute or relative to L</"path">
246  
247 $log->path('/var/log'); # folder relative
248 $log->paths->{'error'} = 'err.log';
249 $log->error(...);# /var/log/err.log
250 $log->info(...); # log to filename as level name /var/log/info.log
251
252 $log->path(undef); # none
253 $log->paths->{'error'} = '/var/log/error.log'; # absolute path only error level
254 $log->error(...); # log to /var/log/error.log
255 $log->info(...); # log to STDERR
256  
257 =head2 trace
258  
259 An trace level, defaults to C<4>, C<0> value will disable trace log. This value pass to C<caller()>.
260  
261 =head1 METHODS
262  
263 B<Mojo::Log::Che> inherits all methods from L<Mojo::Log> and implements the
264 following new ones.
265  
266 =head2 handler($level)
267  
268 Return undef when L</"path"> undefined or L</"path"> is file or has not defined L</"paths"> for $level. In this case L<Mojo::Log/"handle"> will return default handler.
269  
270 Return file handler overwise.
271  
272 =head1 AUTOLOAD
273  
274 Autoloads nonstandard/custom levels excepts already defined keywords of this and parent modules L<Mojo::Log>, L<Mojo::EventEmitter>, L<Mojo::Base>:
275  
276 qw(message _message format _format handle handler handlers
277 history level max_history_size path paths append debug error fatal info
278 is_level new warn catch emit has_subscribers on once subscribers unsubscribe
279 has attr tap _monkey_patch import)
280  
281 and maybe anymore!
282  
283  
284 $log->foo('bar here');
285  
286 That custom levels log always without reducing log output outside of level.
287  
288 =head1 SEE ALSO
289  
290 L<Mojo::Log>, L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
291  
292 =head1 AUTHOR
293  
294 Михаил Че (Mikhail Che), C<< <mche[-at-]cpan.org> >>
295  
296 =head1 BUGS / CONTRIBUTING
297  
298 Please report any bugs or feature requests at L<https://github.com/mche/Mojo-Log-Che/issues>. Pull requests also welcome.
299  
300 =head1 COPYRIGHT
301  
302 Copyright 2017 Mikhail Che.
303  
304 This library is free software; you can redistribute it and/or modify
305 it under the same terms as Perl itself.
306  
307 =cut
↑ Summary