File Coverage

blib/lib/Siffra/Logger.pm
Criterion Covered Total %
statement 56 56 100.0
branch n/a
condition 1 2 50.0
subroutine 18 18 100.0
pod n/a
total 75 76 98.6


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