File Coverage

blib/lib/Siffra/Logger.pm
Criterion Covered Total %
statement 61 72 84.7
branch 1 2 50.0
condition 2 11 18.1
subroutine 18 18 100.0
pod n/a
total 82 103 79.6


line stmt bran cond sub pod time code
1             package Siffra::Logger;
2              
3 1     1   130215 use 5.014;
  1         3  
4 1     1   4 use strict;
  1         1  
  1         18  
5 1     1   4 use warnings;
  1         2  
  1         21  
6 1     1   4 use Carp;
  1         1  
  1         44  
7 1     1   4 use utf8;
  1         2  
  1         6  
8 1     1   489 use Data::Dumper;
  1         4901  
  1         52  
9 1     1   358 use DDP;
  1         32065  
  1         11  
10 1     1   48 use Scalar::Util qw(blessed);
  1         3  
  1         64  
11             $Carp::Verbose = 1;
12              
13             $| = 1; #autoflush
14              
15             use constant {
16             FALSE => 0,
17             TRUE => 1,
18 1   50     75 DEBUG => $ENV{ DEBUG } // 0,
19 1     1   6 };
  1         2  
20              
21             BEGIN
22             {
23 1     1   8 use Exporter ();
  1         2  
  1         19  
24 1     1   4 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  1         1  
  1         79  
25 1     1   3 $VERSION = '0.06';
26 1         14 @ISA = qw(Exporter);
27              
28             #Give a hoot don't pollute, do not export more than needed by default
29 1         3 @EXPORT = qw();
30 1         1 @EXPORT_OK = qw();
31 1         38 %EXPORT_TAGS = ();
32             } ## end BEGIN
33              
34             =head1 Log-Levels
35             trace
36             debug
37             info (inform)
38             notice
39             warning (warn)
40             error (err)
41             critical (crit, fatal)
42             alert
43             emergency
44             =cut
45              
46             BEGIN
47             {
48 1     1   1802 binmode( STDOUT, ":encoding(UTF-8)" );
  1     1   6  
  1         2  
  1         13  
49 1         1032 binmode( STDERR, ":encoding(UTF-8)" );
50              
51 1     1   390 use Log::Any::Adapter;
  1         7037  
  1         4  
52 1     1   391 use Log::Dispatch;
  1         174716  
  1         33  
53 1     1   7 use File::Basename;
  1         2  
  1         98  
54 1     1   7 use POSIX qw/strftime/;
  1         2  
  1         8  
55              
56 1         46 $ENV{ LC_ALL } = $ENV{ LANG } = 'pt_BR.UTF-8';
57              
58 1         66 my ( $filename, $baseDirectory, $suffix ) = fileparse( $0, qr/\.[^.]*/ );
59 1         5 my $logDirectory = $baseDirectory . 'logs/';
60 1         3 my $logFilename = $filename . '.log';
61 1 50 33     16 croak( "Unable to create $logDirectory" ) unless ( -e $logDirectory or mkdir $logDirectory );
62              
63             my $dispatcher = Log::Dispatch->new(
64             outputs => [
65             [
66             'Screen',
67             name => 'screen',
68             min_level => DEBUG ? 'debug' : 'info',
69             max_level => 'warning',
70             newline => 1,
71             utf8 => 0,
72             stderr => 0,
73             use_color => 1,
74             ],
75             [
76             'Screen',
77             name => 'screen-error',
78             min_level => 'error',
79             newline => 1,
80             utf8 => 0,
81             stderr => 1,
82             use_color => 1,
83             ],
84             [
85             'File',
86             name => 'file-01',
87             filename => $logDirectory . $logFilename,
88             min_level => DEBUG ? 'debug' : 'info',
89             newline => 1,
90             mode => 'append',
91             binmode => ':encoding(UTF-8)',
92             ],
93             [
94             'Email::Siffra',
95             name => 'Email',
96             subject => 'Subject',
97             to => 'luiz@siffra.com.br',
98             from => 'avell@siffra.local',
99             min_level => 'error',
100             buffered => 1,
101             smtp => 'mail',
102             port => 2525,
103             utf8 => 1,
104             ]
105             ],
106             callbacks => [
107             sub {
108 0         0 my %msg = @_;
109 0         0 my $i = 0;
110 0         0 my @array_caller;
111 0         0 my ( $package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash );
112              
113             do
114 0   0     0 {
      0        
115 0         0 ( $package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask, $hinthash ) = caller( $i++ );
116              
117 0         0 my $auxiliar = {
118             package => $package,
119             filename => $filename,
120             line => $line,
121             subroutine => $subroutine,
122             };
123              
124 0         0 push( @array_caller, $auxiliar );
125             } until ( !defined $line or $line == 0 or $subroutine =~ /(eval)/ );
126              
127 0         0 $msg{ message } =~ s/\n|\r//g;
128 0         0 my $mensage = sprintf( "%s [ %9.9s ] [ pid: %d ] - %s - [ %s ]", strftime( "%F %H:%M:%S", localtime ), uc( $msg{ level } ), $$, $msg{ message }, $array_caller[ -2 ]->{ subroutine } );
129              
130 0         0 return $mensage;
131             }
132 1         17 ]
133             );
134              
135 1         140246 Log::Any::Adapter->set( 'Dispatch', dispatcher => $dispatcher );
136             } ## end BEGIN
137              
138             #################### main pod documentation begin ###################
139             ## Below is the stub of documentation for your module.
140             ## You better edit it!
141              
142             =pod
143              
144             =encoding UTF-8
145              
146             =head1 NAME
147              
148             Siffra::Logger - Siffra config for C<Log::Any>
149              
150             =head1 SYNOPSIS
151              
152             In a CPAN or other module:
153              
154             package Foo;
155             use Log::Any qw($log);
156             use Siffra::Logger;
157              
158             # log a string
159             $log->error("an error occurred");
160              
161             # log a string and some data
162             $log->info("program started", {progname => $0, pid => $$, perl_version => $]});
163              
164             # log a string and data using a format string
165             $log->debugf("arguments are: %s", \@_);
166              
167             # log an error and throw an exception
168             die $log->fatal("a fatal error occurred");
169              
170             In your application:
171              
172             use Foo;
173             use Log::Any qw($log);
174             use Siffra::Logger;
175              
176             # log a string
177             $log->error("an error occurred");
178              
179             # log a string and some data
180             $log->info("program started", {progname => $0, pid => $$, perl_version => $]});
181              
182             # log a string and data using a format string
183             $log->debugf("arguments are: %s", \@_);
184              
185             # log an error and throw an exception
186             die $log->fatal("a fatal error occurred");
187              
188             =head2 OUTPUTS
189              
190             =over 12
191              
192             =item C<Directory Creation>
193              
194             my ( $filename, $baseDirectory, $suffix ) = fileparse( $0, qr/\.[^.]*/ );
195             my $logDirectory = $baseDirectory . 'logs/';
196             my $logFilename = $filename . '.log';
197             croak( "Unable to create $logDirectory" ) unless ( -e $logDirectory or mkdir $logDirectory );
198              
199             =item C<Outputs>
200              
201             [
202             'Screen',
203             name => 'screen',
204             min_level => 'debug',
205             max_level => 'warning',
206             newline => 1,
207             utf8 => 0,
208             stderr => 0,
209             use_color => 1,
210             ],
211             [
212             'Screen',
213             name => 'screen-error',
214             min_level => 'error',
215             newline => 1,
216             utf8 => 0,
217             stderr => 1,
218             use_color => 1,
219             ],
220             [
221             'File',
222             name => 'file-01',
223             filename => $logDirectory . $logFilename,
224             min_level => 'debug',
225             newline => 1,
226             mode => 'write',
227             binmode => ':encoding(UTF-8)',
228             ]
229              
230             =back
231              
232             =head1 DESCRIPTION
233              
234             C<Siffra::logger> provides a standart outputs to C<Log::Any>
235              
236             =head1 AUTHOR
237              
238             Luiz Benevenuto
239             CPAN ID: LUIZBENE
240             Siffra TI
241             luiz@siffra.com.br
242             https://siffra.com.br
243              
244             =head1 COPYRIGHT
245              
246             This program is free software; you can redistribute
247             it and/or modify it under the same terms as Perl itself.
248              
249             The full text of the license can be found in the
250             LICENSE file included with this module.
251              
252             =head1 SEE ALSO
253              
254             perl(1).
255              
256             =cut
257              
258             #################### main pod documentation end ###################
259              
260             1;
261              
262             # The preceding line will help the module return a true value
263