blib/lib/Mojolicious/Plugin/Logf.pm

filter: Statementstmt 100% 34 / 34 Branchbran 100% 16 / 16 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 97.0% 66 / 68 CC 12 SCAR 13.1
CC
12
Cov
98%
flatten
7.0
logf
3.0
BEGIN
2.0
CRAP 3.7
↑ Summary
1 package Mojolicious::Plugin::Logf;
2 4 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use constant UNDEF => $ENV{MOJO_LOGF_UNDEF} || '__UNDEF__';
Subroutine BEGIN: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{'MOJO_LOGF_UNDEF'} || '__UNDEF__'
l!l
13
MC/DC100%1/1
$ENV{'MOJO_LOGF_UNDEF'} || '__UNDEF__'
$ENV{'MOJO_LOGF_UNDEF'}
Truth table100%2/2
A
A= $ENV{'MOJO_LOGF_UNDEF'}
Aresult
11
00
6  
7 4 our $VERSION = '0.10';
8  
9 5 ▶sub logf {
Subroutine logf: called CC=3 SCAR=11.0
Pod documented
10 5 my ($self, $c, $level, $format, @args) = @_;
11 5 my $log = $c->app->log;
12 5 ▶ $log->$level(@args ? sprintf $format, $self->flatten(@args) : $format)
Branch: 2 branches
BranchTrueFalseTotal
if $log->is_level($level) 4 1 5
@args ? : 3 1 4
13 if $log->is_level($level);
14 5 $c;
15 }
16  
17 6 ▶sub flatten {
Subroutine flatten: called CC=7 SCAR=19.5
Pod documented
18 6 my $self = shift;
19 6 ▶ my @args = map { ref $_ eq 'CODE' ? $_->() : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'CODE' ? : 1 10 11
20  
21 6 local $Data::Dumper::Indent = 0;
22 6 ▶ local $Data::Dumper::Maxdepth = $Data::Dumper::Maxdepth || 2;
Condition50%1/2
$Data::Dumper::Maxdepth || 2
l!l
06
MC/DC0%0/1
$Data::Dumper::Maxdepth || 2
$Data::Dumper::Maxdepth
Truth table50%1/2
A
A= $Data::Dumper::Maxdepth
Aresult
11
00
23 6 local $Data::Dumper::Sortkeys = 1;
24 6 local $Data::Dumper::Terse = 1;
25  
26 6 for (@args) {
27 11 ▶ $_
Branch: 3 branches
BranchTrueFalseTotal
!defined($_) ? : 3 8 11
overload::Method($_, '""') ? : 1 7 8
ref $_ ? : 6 1 7
28 = !defined($_) ? UNDEF
29 : overload::Method($_, q("")) ? "$_"
30 : ref($_) ? Data::Dumper::Dumper($_)
31 : $_;
32 }
33  
34 6 return @args;
35 }
36  
37 2 ▶sub register {
Subroutine register: called CC=2 SCAR=6.9
Pod documented
38 2 my ($self, $app, $config) = @_;
39  
40 2 ▶ $app->helper(logf => sub { @_ == 1 ? $self : logf($self, @_) });
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 1 5 6
41 2 ▶ $app->log->format(\&_rfc3339) if $config->{rfc3339};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"rfc3339"} 1 1 2
42 }
43  
44 1 ▶sub _rfc3339 {
Subroutine _rfc3339: called CC=1 SCAR=0.0
45 1 my ($s, $m, $h, $day, $month, $year) = gmtime(shift);
46 1 sprintf '[%04d-%02d-%02dT%02d:%02d:%02dZ] [%s] %s', $year + 1900, $month + 1, $day, $h,
47 $m, $s, shift(@_), join "\n", @_, '';
48 }
49  
50 4 1;
51  
52 =encoding utf8
53  
54 =head1 NAME
55  
56 Mojolicious::Plugin::Logf - Plugin for logging datastructures using sprintf
57  
58 =head1 VERSION
59  
60 0.10
61  
62 =head1 DESCRIPTION
63  
64 L<Mojolicious::Plugin::Logf> is a plugin which will log complex datastructures
65 and avoid "unitialized" warnings. This plugin use L<Mojo::Log> or whatever
66 L<Mojo/log> is set to, to do the actual logging.
67  
68 =head1 SYNOPSIS
69  
70 use Mojolicious::Lite;
71 plugin logf => {rfc3339 => 1};
72  
73 get "/" => sub {
74 my $c = shift;
75 $c->logf(info => 'request: %s', $self->req->params->to_hash);
76 $c->render(text => "cool!");
77 };
78  
79 Setting C<rfc3339> to "1" will make the log look like this:
80  
81 [2016-02-19T13:05:37Z] [info] Some log message
82  
83 =head1 COPY/PASTE CODE
84  
85 If you think it's a waste to depend on this module, you can copy paste the
86 code below to get the same functionality as the L</logf> helper:
87  
88 helper logf => sub {
89 my ($c, $level, $format) = (shift, shift, shift);
90 my $log = $c->app->log;
91 return $c unless $log->is_level($level);
92 my @args = map { ref $_ eq 'CODE' ? $_->() : $_ } @_;
93 local $Data::Dumper::Indent = 0;
94 local $Data::Dumper::Maxdepth = $Data::Dumper::Maxdepth || 2;
95 local $Data::Dumper::Sortkeys = 1;
96 local $Data::Dumper::Terse = 1;
97 for (@args) {
98 $_
99 = !defined($_) ? "__UNDEF__"
100 : overload::Method($_, q("")) ? "$_"
101 : ref($_) ? Data::Dumper::Dumper($_)
102 : $_;
103 }
104 $log->$level(sprintf $format, @args);
105 return $c;
106 };
107  
108 Note: The code above is generated and tested from the original source code,
109 but it will more difficult to get updates and bug fixes.
110  
111 =head1 HELPERS
112  
113 =head2 logf
114  
115 $self = $c->logf;
116 $c = $c->logf($level => $format, @args);
117  
118 Logs a string formatted by the usual C<printf> conventions of the C library
119 function C<sprintf>. C<$level> need to be a valid L<Mojo::Log> level.
120 C<@args> will be converted using L</flatten>.
121  
122 Calling this method without any arguments will return C<$self>
123 (an instance of this plugin), allowing you to call L</flatten>:
124  
125 @args_as_strings = $c->logf->flatten(@args);
126  
127 =head1 METHODS
128  
129 =head2 flatten
130  
131 @args_as_strings = $self->flatten(@args);
132  
133 Used to convert input C<@args> using these rules:
134  
135 =over 4
136  
137 =item * Scalar
138  
139 No rule applied.
140  
141 =item * Code ref
142  
143 A code ref will be called, and the list of return values will be flattened.
144 The code below will not calculate the request params, unless the log level
145 is "debug":
146  
147 $c->logf(debug => 'request: %s', sub {$c->req->params->to_hash});
148  
149 =item * Object with string overloading
150  
151 Will be coverted to a string using the string overloading function.
152  
153 =item * Data structure or object
154  
155 Will be serialized using L<Data::Dumper> with these settings:
156  
157 $Data::Dumper::Indent = 0;
158 $Data::Dumper::Maxdepth = $Data::Dumper::Maxdepth || 2;
159 $Data::Dumper::Sortkeys = 1;
160 $Data::Dumper::Terse = 1;
161  
162 NOTE! These settings might change, but will always do its best to
163 serialize the object into one line. C<$Data::Dumper::Maxdepth> is
164 used to avoid dumping large nested objects. Set this variable
165 if you need deeper logging. Example:
166  
167 local $Data::Dumper::Maxdepth = 1000;
168 $c->logf(info => 'Deep structure: %s', $some_object);
169  
170 =item * Undefined value
171  
172 Will be logged as "__UNDEF__". This value can be changed by setting
173 the global environment variable C<MOJO_LOGF_UNDEF> before loading this
174 plugin.
175  
176 =back
177  
178 =head2 register
179  
180 Will register the L</logf> helper in the application
181  
182 =head1 COPYRIGHT AND LICENSE
183  
184 Copyright (C) 2014, Jan Henning Thorsen
185  
186 This program is free software, you can redistribute it and/or modify it under
187 the terms of the Artistic License version 2.0.
188  
189 =head1 AUTHOR
190  
191 Jan Henning Thorsen - C<jhthorsen@cpan.org>
192  
193 =cut
↑ Summary