line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Log::Contextual::Router; |
2
|
|
|
|
|
|
|
$Log::Contextual::Router::VERSION = '0.008001'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Route messages to loggers |
4
|
|
|
|
|
|
|
|
5
|
20
|
|
|
20
|
|
8814
|
use Moo; |
|
20
|
|
|
|
|
188114
|
|
|
20
|
|
|
|
|
108
|
|
6
|
20
|
|
|
20
|
|
23504
|
use Scalar::Util 'blessed'; |
|
20
|
|
|
|
|
44
|
|
|
20
|
|
|
|
|
15116
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
with 'Log::Contextual::Role::Router', |
9
|
|
|
|
|
|
|
'Log::Contextual::Role::Router::SetLogger', |
10
|
|
|
|
|
|
|
'Log::Contextual::Role::Router::WithLogger', |
11
|
|
|
|
|
|
|
'Log::Contextual::Role::Router::HasLogger'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
eval { |
14
|
|
|
|
|
|
|
require Log::Log4perl; |
15
|
|
|
|
|
|
|
die if $Log::Log4perl::VERSION < 1.29; |
16
|
|
|
|
|
|
|
Log::Log4perl->wrapper_register(__PACKAGE__) |
17
|
|
|
|
|
|
|
}; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has _default_logger => ( |
20
|
|
|
|
|
|
|
is => 'ro', |
21
|
|
|
|
|
|
|
default => sub { {} }, |
22
|
|
|
|
|
|
|
init_arg => undef, |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has _package_logger => ( |
26
|
|
|
|
|
|
|
is => 'ro', |
27
|
|
|
|
|
|
|
default => sub { {} }, |
28
|
|
|
|
|
|
|
init_arg => undef, |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
has _get_logger => ( |
32
|
|
|
|
|
|
|
is => 'ro', |
33
|
|
|
|
|
|
|
default => sub { {} }, |
34
|
|
|
|
|
|
|
init_arg => undef, |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
31
|
0
|
|
sub before_import { } |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub after_import { |
40
|
31
|
|
|
31
|
0
|
104
|
my ($self, %import_info) = @_; |
41
|
31
|
|
|
|
|
70
|
my $exporter = $import_info{exporter}; |
42
|
31
|
|
|
|
|
52
|
my $target = $import_info{target}; |
43
|
31
|
|
|
|
|
63
|
my $config = $import_info{arguments}; |
44
|
|
|
|
|
|
|
|
45
|
31
|
100
|
|
|
|
119
|
if (my $l = $exporter->arg_logger($config->{logger})) { |
46
|
7
|
|
|
|
|
37
|
$self->set_logger($l); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
31
|
100
|
|
|
|
180
|
if (my $l = $exporter->arg_package_logger($config->{package_logger})) { |
50
|
3
|
|
|
|
|
11
|
$self->_set_package_logger_for($target, $l); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
31
|
100
|
|
|
|
126
|
if (my $l = $exporter->arg_default_logger($config->{default_logger})) { |
54
|
4
|
|
|
|
|
15
|
$self->_set_default_logger_for($target, $l); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub with_logger { |
59
|
4
|
|
|
4
|
0
|
9
|
my $logger = $_[1]; |
60
|
4
|
100
|
|
|
|
11
|
if (ref $logger ne 'CODE') { |
61
|
3
|
50
|
|
|
|
12
|
die 'logger was not a CodeRef or a logger object. Please try again.' |
62
|
|
|
|
|
|
|
unless blessed($logger); |
63
|
3
|
|
|
|
|
5
|
$logger = do { |
64
|
3
|
|
|
|
|
3
|
my $l = $logger; |
65
|
8
|
|
|
8
|
|
11
|
sub { $l } |
66
|
3
|
|
|
|
|
10
|
} |
67
|
|
|
|
|
|
|
} |
68
|
4
|
|
|
|
|
12
|
local $_[0]->_get_logger->{l} = $logger; |
69
|
4
|
|
|
|
|
9
|
$_[2]->(); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub set_logger { |
73
|
19
|
|
|
19
|
0
|
40
|
my $logger = $_[1]; |
74
|
19
|
100
|
|
|
|
92
|
if (ref $logger ne 'CODE') { |
75
|
13
|
50
|
|
|
|
95
|
die 'logger was not a CodeRef or a logger object. Please try again.' |
76
|
|
|
|
|
|
|
unless blessed($logger); |
77
|
13
|
|
|
|
|
38
|
$logger = do { |
78
|
13
|
|
|
|
|
26
|
my $l = $logger; |
79
|
119
|
|
|
119
|
|
169
|
sub { $l } |
80
|
13
|
|
|
|
|
60
|
} |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
warn 'set_logger (or -logger) called more than once! This is a bad idea!' |
84
|
19
|
100
|
|
|
|
202
|
if $_[0]->_get_logger->{l}; |
85
|
19
|
|
|
|
|
135
|
$_[0]->_get_logger->{l} = $logger; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
2
|
|
|
2
|
0
|
331
|
sub has_logger { !!$_[0]->_get_logger->{l} } |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub _set_default_logger_for { |
91
|
4
|
|
|
4
|
|
7
|
my $logger = $_[2]; |
92
|
4
|
50
|
|
|
|
13
|
if (ref $logger ne 'CODE') { |
93
|
4
|
50
|
|
|
|
20
|
die 'logger was not a CodeRef or a logger object. Please try again.' |
94
|
|
|
|
|
|
|
unless blessed($logger); |
95
|
4
|
|
|
|
|
7
|
$logger = do { |
96
|
4
|
|
|
|
|
6
|
my $l = $logger; |
97
|
16
|
|
|
16
|
|
21
|
sub { $l } |
98
|
4
|
|
|
|
|
14
|
} |
99
|
|
|
|
|
|
|
} |
100
|
4
|
|
|
|
|
1721
|
$_[0]->_default_logger->{$_[1]} = $logger |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub _set_package_logger_for { |
104
|
3
|
|
|
3
|
|
5
|
my $logger = $_[2]; |
105
|
3
|
50
|
|
|
|
15
|
if (ref $logger ne 'CODE') { |
106
|
3
|
50
|
|
|
|
20
|
die 'logger was not a CodeRef or a logger object. Please try again.' |
107
|
|
|
|
|
|
|
unless blessed($logger); |
108
|
3
|
|
|
|
|
6
|
$logger = do { |
109
|
3
|
|
|
|
|
5
|
my $l = $logger; |
110
|
28
|
|
|
28
|
|
54
|
sub { $l } |
111
|
3
|
|
|
|
|
21
|
} |
112
|
|
|
|
|
|
|
} |
113
|
3
|
|
|
|
|
29
|
$_[0]->_package_logger->{$_[1]} = $logger |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub get_loggers { |
117
|
209
|
|
|
209
|
0
|
482
|
my ($self, %info) = @_; |
118
|
209
|
|
|
|
|
267
|
my $package = $info{caller_package}; |
119
|
209
|
|
|
|
|
253
|
my $log_level = $info{message_level}; |
120
|
|
|
|
|
|
|
my $logger = |
121
|
|
|
|
|
|
|
( $_[0]->_package_logger->{$package} |
122
|
|
|
|
|
|
|
|| $_[0]->_get_logger->{l} |
123
|
209
|
|
100
|
|
|
1067
|
|| $_[0]->_default_logger->{$package} |
124
|
|
|
|
|
|
|
|| die |
125
|
|
|
|
|
|
|
q( no logger set! you can't try to log something without a logger! )); |
126
|
|
|
|
|
|
|
|
127
|
208
|
|
|
|
|
266
|
$info{caller_level}++; |
128
|
208
|
|
|
|
|
356
|
$logger = $logger->($package, \%info); |
129
|
|
|
|
|
|
|
|
130
|
208
|
100
|
|
|
|
432
|
return $logger if $logger ->${\"is_${log_level}"}; |
|
208
|
|
|
|
|
659
|
|
131
|
45
|
|
|
|
|
172
|
return (); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub handle_log_request { |
135
|
209
|
|
|
209
|
0
|
672
|
my ($self, %message_info) = @_; |
136
|
209
|
|
|
|
|
325
|
my $generator = $message_info{message_sub}; |
137
|
209
|
|
|
|
|
313
|
my $text = $message_info{message_text}; |
138
|
209
|
|
|
|
|
262
|
my $args = $message_info{message_args}; |
139
|
209
|
|
|
|
|
258
|
my $log_level = $message_info{message_level}; |
140
|
|
|
|
|
|
|
|
141
|
209
|
|
|
|
|
555
|
$message_info{caller_level}++; |
142
|
|
|
|
|
|
|
|
143
|
209
|
100
|
|
|
|
475
|
my @loggers = $self->get_loggers(%message_info) |
144
|
|
|
|
|
|
|
or return; |
145
|
|
|
|
|
|
|
|
146
|
163
|
100
|
|
|
|
632
|
my @log = defined $text ? ($text) : ($generator->(@$args)); |
147
|
161
|
|
|
|
|
1659
|
$_->$log_level(@log) for @loggers; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
1; |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
__END__ |