line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer2::Logger::Log4perl; |
2
|
|
|
|
|
|
|
# ABSTRACT: Dancer2 logger interface for Log4perl. |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
1178
|
use Log::Log4perl qw( :easy ); |
|
1
|
|
|
|
|
36827
|
|
|
1
|
|
|
|
|
4
|
|
5
|
1
|
|
|
1
|
|
1029
|
use Moo; |
|
1
|
|
|
|
|
8899
|
|
|
1
|
|
|
|
|
4
|
|
6
|
1
|
|
|
1
|
|
1570
|
use Dancer2::Core::Types; |
|
1
|
|
|
|
|
115862
|
|
|
1
|
|
|
|
|
7
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
with 'Dancer2::Core::Role::Logger'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has config_file => ( |
11
|
|
|
|
|
|
|
is => 'ro', |
12
|
|
|
|
|
|
|
isa => ReadableFilePath, |
13
|
|
|
|
|
|
|
); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has config_watch_interval => ( |
16
|
|
|
|
|
|
|
is => 'ro', |
17
|
|
|
|
|
|
|
isa => sub { |
18
|
|
|
|
|
|
|
my $config_watch_interval = shift; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Can either be 'HUP' or a number |
21
|
|
|
|
|
|
|
Str->( $config_watch_interval ); |
22
|
|
|
|
|
|
|
if( $config_watch_interval eq 'HUP' ) { |
23
|
|
|
|
|
|
|
return; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
else { |
26
|
|
|
|
|
|
|
Num->( $config_watch_interval ); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
}, |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub _initialize_log4perl { |
32
|
0
|
|
|
0
|
|
|
my $self = shift; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# If config_file is defined, then use that |
35
|
0
|
0
|
|
|
|
|
if( defined $self->config_file ) { |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# ...optionally with the watch interval |
38
|
0
|
0
|
|
|
|
|
if( defined $self->config_watch_interval ) { |
39
|
0
|
|
|
|
|
|
Log::Log4perl->init_and_watch( $self->config_file, $self->config_watch_interval ); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
else { |
42
|
0
|
|
|
|
|
|
Log::Log4perl->init( $self->config_file ); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# Otherwise we'll easy init with the appropriate log level |
47
|
|
|
|
|
|
|
else { |
48
|
0
|
|
|
|
|
|
my $log_level_mapping = { |
49
|
|
|
|
|
|
|
'error' => $ERROR, |
50
|
|
|
|
|
|
|
'warn' => $WARN, |
51
|
|
|
|
|
|
|
'warning' => $WARN, |
52
|
|
|
|
|
|
|
'info' => $INFO, |
53
|
|
|
|
|
|
|
'debug' => $DEBUG, |
54
|
|
|
|
|
|
|
'trace' => $TRACE, |
55
|
|
|
|
|
|
|
'core' => $TRACE, |
56
|
|
|
|
|
|
|
}; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
Log::Log4perl->easy_init( $log_level_mapping->{ $self->log_level } ); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub log { |
63
|
|
|
|
|
|
|
my ( $self, $level, $message ) = @_; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Need to initialize Log4perl if it isn't yet |
66
|
|
|
|
|
|
|
if( !Log::Log4perl->initialized() ) { |
67
|
|
|
|
|
|
|
$self->_initialize_log4perl(); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# Need to convert Dancer2 log levels to Log4perl levels |
71
|
|
|
|
|
|
|
$level = 'warn' if $level eq 'warning'; |
72
|
|
|
|
|
|
|
$level = 'trace' if $level eq 'core'; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# TODO: Couldn't get $Log::Log4perl::caller_depth to work |
75
|
|
|
|
|
|
|
Log::Log4perl->get_logger( scalar caller(4) )->$level( $message ); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
__END__ |