File Coverage

blib/lib/Dancer/Logger/Abstract.pm
Criterion Covered Total %
statement 76 80 95.0
branch 34 38 89.4
condition 4 10 40.0
subroutine 29 31 93.5
pod 6 6 100.0
total 149 165 90.3


line stmt bran cond sub pod time code
1             package Dancer::Logger::Abstract;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             #ABSTRACT: Abstract logging engine for Dancer
4             $Dancer::Logger::Abstract::VERSION = '1.3520';
5 99     99   1359 use strict;
  99         283  
  99         2936  
6 99     99   584 use warnings;
  99         315  
  99         2723  
7 99     99   549 use Carp;
  99         293  
  99         5507  
8 99     99   683 use base 'Dancer::Engine';
  99         304  
  99         12612  
9              
10 99     99   3179 use Dancer::SharedData;
  99         323  
  99         2831  
11 99     99   658 use Dancer::Timer;
  99         264  
  99         3216  
12 99     99   717 use Dancer::Config 'setting';
  99         234  
  99         5499  
13 99     99   48430 use POSIX qw/strftime/;
  99         615075  
  99         805  
14              
15             # This is the only method to implement by logger engines.
16             # It receives the following arguments:
17             # $msg_level, $msg_content, it gets called only if the configuration allows
18             # a message of the given level to be logged.
19 16     16   3395 sub _log { confess "_log not implemented" }
20              
21             my $levels = {
22              
23             # levels < 0 are for core only
24             core => -10,
25              
26             # levels > 0 are for end-users only
27             debug => 1,
28             info => 2,
29             warn => 3,
30             warning => 3,
31             error => 4,
32             };
33              
34             my $log_formats = {
35             simple => '[%P] %L @%D> %i%m in %f l. %l',
36             };
37              
38             sub _log_format {
39 34     34   99 my $config = setting('logger_format');
40              
41 34 100       109 if ( !$config ) {
42 27         86 return $log_formats->{simple};
43             }
44              
45             exists $log_formats->{$config}
46 7 100       31 ? return $log_formats->{$config}
47             : return $config;
48             }
49              
50             sub _should {
51 3045     3045   5276 my ($self, $msg_level) = @_;
52 3045   100     6416 my $conf_level = setting('log') || 'debug';
53              
54 3045 100       7140 if (!exists $levels->{$conf_level}) {
55 1         3 setting('log' => 'debug');
56 1         2 $conf_level = 'debug';
57             }
58              
59 3045         9886 return $levels->{$conf_level} <= $levels->{$msg_level};
60             }
61              
62             sub format_message {
63 31     31 1 6111 my ($self, $level, $message) = @_;
64 31         89 chomp $message;
65              
66 31 100       113 $message = Encode::encode(setting('charset'), $message)
67             if setting('charset');
68              
69 31 100       215 $level = 'warn' if $level eq 'warning';
70 31         123 $level = sprintf('%5s', $level);
71              
72 31         156 my $r = Dancer::SharedData->request;
73 31         143 my @stack = caller(3);
74              
75             my $block_handler = sub {
76 2     2   7 my ( $block, $type ) = @_;
77 2 100       10 if ( $type eq 't' ) {
    50          
78 1         201 return "[" . strftime( $block, localtime ) . "]";
79             }
80             elsif ( $type eq 'h' ) {
81 1 50       4 return '-' unless defined $r;
82 1   50     8 return scalar $r->header($block) || '-';
83             }
84             else {
85 0         0 Carp::carp("{$block}$type not supported");
86 0         0 return "-";
87             }
88 31         180 };
89              
90             my $chars_mapping = {
91             h => sub {
92             defined $r
93 1 50 0 1   9 ? $r->env->{'HTTP_X_REAL_IP'} || $r->env->{'REMOTE_ADDR'} || '-'
94             : '-';
95             },
96 1   50 1   12 t => sub { Encode::decode(setting('charset') || 'utf8',
97             POSIX::strftime( "%d/%b/%Y %H:%M:%S", localtime )) },
98 1     1   46 T => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime ) },
99 0   0 0   0 u => sub { Encode::decode(setting('charset') || 'utf8',
100             POSIX::strftime( "%d/%b/%Y %H:%M:%S", gmtime )) },
101 0     0   0 U => sub { POSIX::strftime( "%Y-%m-%d %H:%M:%S", gmtime ) },
102 26     26   151 P => sub { $$ },
103 27     27   111 L => sub { $level },
104             D => sub {
105 26     26   134 my $time = Dancer::SharedData->timer->tick;
106 26         191 return $time;
107             },
108 29     29   141 m => sub { $message },
109 26 100   26   197 f => sub { $stack[1] || '-' },
110 26 100   26   138 l => sub { $stack[2] || '-' },
111             i => sub {
112 26 100   26   126 defined $r ? "[hit #" . $r->id . "]" : "";
113             },
114 31         611 };
115              
116             my $char_mapping = sub {
117 190     190   336 my $char = shift;
118              
119 190         296 my $cb = $chars_mapping->{$char};
120 190 100       369 unless ($cb) {
121 1         244 Carp::carp "\%$char not supported.";
122 1         87 return "-";
123             }
124 189         339 $cb->($char);
125 31         120 };
126              
127 31         133 my $fmt = $self->_log_format();
128              
129 31         291 $fmt =~ s^
130             (?:
131             \%\{(.+?)\}([a-z])|
132             \%([a-zA-Z])
133             )
134 192 100       1010 ^ $1 ? $block_handler->($1, $2) : $char_mapping->($3) ^egx;
135              
136 31         711 return $fmt."\n";
137             }
138              
139 2965 100   2965 1 10120 sub core { $_[0]->_should('core') and $_[0]->_log('core', $_[1]) }
140 14 100   14 1 2844 sub debug { $_[0]->_should('debug') and $_[0]->_log('debug', $_[1]) }
141 7 100   7 1 2826 sub info { $_[0]->_should('info') and $_[0]->_log('info', $_[1]) }
142 12 100   12 1 3027 sub warning { $_[0]->_should('warning') and $_[0]->_log('warning', $_[1]) }
143 47 50   47 1 3274 sub error { $_[0]->_should('error') and $_[0]->_log('error', $_[1]) }
144              
145             1;
146              
147             __END__