line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooseX::Role::Loggable; |
2
|
|
|
|
|
|
|
$MooseX::Role::Loggable::VERSION = '0.113'; |
3
|
|
|
|
|
|
|
# ABSTRACT: Extensive, yet simple, logging role using Log::Dispatchouli |
4
|
|
|
|
|
|
|
|
5
|
6
|
|
|
6
|
|
39919
|
use strict; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
151
|
|
6
|
6
|
|
|
6
|
|
19
|
use warnings; |
|
6
|
|
|
|
|
7
|
|
|
6
|
|
|
|
|
114
|
|
7
|
|
|
|
|
|
|
|
8
|
6
|
|
|
6
|
|
19
|
use Carp (); |
|
6
|
|
|
|
|
7
|
|
|
6
|
|
|
|
|
64
|
|
9
|
6
|
|
|
6
|
|
2230
|
use Safe::Isa; |
|
6
|
|
|
|
|
2039
|
|
|
6
|
|
|
|
|
616
|
|
10
|
6
|
|
|
6
|
|
24
|
use Moo::Role; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
28
|
|
11
|
6
|
|
|
6
|
|
4013
|
use MooX::Types::MooseLike::Base qw; |
|
6
|
|
|
|
|
25991
|
|
|
6
|
|
|
|
|
400
|
|
12
|
6
|
|
|
6
|
|
2607
|
use Sub::Quote 'quote_sub'; |
|
6
|
|
|
|
|
13887
|
|
|
6
|
|
|
|
|
253
|
|
13
|
6
|
|
|
6
|
|
2814
|
use Log::Dispatchouli; |
|
6
|
|
|
|
|
131637
|
|
|
6
|
|
|
|
|
176
|
|
14
|
6
|
|
|
6
|
|
2516
|
use namespace::sweep; |
|
6
|
|
|
|
|
66922
|
|
|
6
|
|
|
|
|
36
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my %attr_meth_map = ( |
17
|
|
|
|
|
|
|
'logger_facility' => 'facility', |
18
|
|
|
|
|
|
|
'logger_ident' => 'ident', |
19
|
|
|
|
|
|
|
'log_to_file' => 'to_file', |
20
|
|
|
|
|
|
|
'log_to_stdout' => 'to_stdout', |
21
|
|
|
|
|
|
|
'log_to_stderr' => 'to_stderr', |
22
|
|
|
|
|
|
|
'log_fail_fatal' => 'fail_fatal', |
23
|
|
|
|
|
|
|
'log_muted' => 'muted', |
24
|
|
|
|
|
|
|
'log_quiet_fatal' => 'quiet_fatal', |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has 'debug' => ( |
28
|
|
|
|
|
|
|
'is' => 'ro', |
29
|
|
|
|
|
|
|
'isa' => Bool, |
30
|
|
|
|
|
|
|
'default' => sub {0}, |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has 'logger_facility' => ( |
34
|
|
|
|
|
|
|
'is' => 'ro', |
35
|
|
|
|
|
|
|
'isa' => Str, |
36
|
|
|
|
|
|
|
'default' => sub {'local6'}, |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
has 'logger_ident' => ( |
40
|
|
|
|
|
|
|
'is' => 'ro', |
41
|
|
|
|
|
|
|
'isa' => Str, |
42
|
|
|
|
|
|
|
'default' => sub { ref shift }, |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
has 'log_to_file' => ( |
46
|
|
|
|
|
|
|
'is' => 'ro', |
47
|
|
|
|
|
|
|
'isa' => Bool, |
48
|
|
|
|
|
|
|
'default' => sub {0}, |
49
|
|
|
|
|
|
|
); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
has 'log_to_stdout' => ( |
52
|
|
|
|
|
|
|
'is' => 'ro', |
53
|
|
|
|
|
|
|
'isa' => Bool, |
54
|
|
|
|
|
|
|
'default' => sub {0}, |
55
|
|
|
|
|
|
|
); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
has 'log_to_stderr' => ( |
58
|
|
|
|
|
|
|
'is' => 'ro', |
59
|
|
|
|
|
|
|
'isa' => Bool, |
60
|
|
|
|
|
|
|
'default' => sub {0}, |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
has 'log_file' => ( |
64
|
|
|
|
|
|
|
'is' => 'ro', |
65
|
|
|
|
|
|
|
'isa' => Str, |
66
|
|
|
|
|
|
|
'predicate' => 'has_log_file', |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
has 'log_path' => ( |
70
|
|
|
|
|
|
|
'is' => 'ro', |
71
|
|
|
|
|
|
|
'isa' => Str, |
72
|
|
|
|
|
|
|
'predicate' => 'has_log_path', |
73
|
|
|
|
|
|
|
); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
has 'log_pid' => ( |
76
|
|
|
|
|
|
|
'is' => 'ro', |
77
|
|
|
|
|
|
|
'isa' => Bool, |
78
|
|
|
|
|
|
|
'default' => sub {1}, |
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
has 'log_fail_fatal' => ( |
82
|
|
|
|
|
|
|
'is' => 'ro', |
83
|
|
|
|
|
|
|
'isa' => Bool, |
84
|
|
|
|
|
|
|
'default' => sub {1}, |
85
|
|
|
|
|
|
|
); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
has 'log_muted' => ( |
88
|
|
|
|
|
|
|
'is' => 'ro', |
89
|
|
|
|
|
|
|
'isa' => Bool, |
90
|
|
|
|
|
|
|
'default' => sub {0}, |
91
|
|
|
|
|
|
|
); |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
has 'log_quiet_fatal' => ( |
94
|
|
|
|
|
|
|
'is' => 'ro', |
95
|
|
|
|
|
|
|
'isa' => quote_sub( |
96
|
|
|
|
|
|
|
q{ |
97
|
|
|
|
|
|
|
use Safe::Isa; |
98
|
|
|
|
|
|
|
$_[0] || $_[0]->$_isa( ref [] ) |
99
|
|
|
|
|
|
|
or die "$_[0] must be a string or arrayref" |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
), |
102
|
|
|
|
|
|
|
'default' => sub {'stderr'}, |
103
|
|
|
|
|
|
|
); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
has 'logger' => ( |
106
|
|
|
|
|
|
|
'is' => 'lazy', |
107
|
|
|
|
|
|
|
'isa' => quote_sub( |
108
|
|
|
|
|
|
|
q{ |
109
|
|
|
|
|
|
|
use Safe::Isa; |
110
|
|
|
|
|
|
|
$_[0]->$_isa('Log::Dispatchouli') || |
111
|
|
|
|
|
|
|
$_[0]->$_isa('Log::Dispatchouli::Proxy') |
112
|
|
|
|
|
|
|
or die "$_[0] must be a Log::Dispatchouli object"; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
), |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
'handles' => [ |
117
|
|
|
|
|
|
|
qw/ |
118
|
|
|
|
|
|
|
log log_fatal log_debug |
119
|
|
|
|
|
|
|
set_debug clear_debug set_prefix clear_prefix set_muted clear_muted |
120
|
|
|
|
|
|
|
/ |
121
|
|
|
|
|
|
|
], |
122
|
|
|
|
|
|
|
); |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub _build_logger { |
125
|
7
|
|
|
7
|
|
4490
|
my $self = shift; |
126
|
7
|
|
|
|
|
24
|
my %optional; |
127
|
|
|
|
|
|
|
|
128
|
7
|
|
|
|
|
14
|
foreach my $option (qw) { |
129
|
14
|
|
|
|
|
22
|
my $method = "has_$option"; |
130
|
14
|
100
|
|
|
|
55
|
if ( $self->$method ) { |
131
|
1
|
|
|
|
|
4
|
$optional{$option} = $self->$option; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
7
|
|
|
|
|
129
|
my $logger = Log::Dispatchouli->new( |
136
|
|
|
|
|
|
|
{ |
137
|
|
|
|
|
|
|
'debug' => $self->debug, |
138
|
|
|
|
|
|
|
'ident' => $self->logger_ident, |
139
|
|
|
|
|
|
|
'facility' => $self->logger_facility, |
140
|
|
|
|
|
|
|
'to_file' => $self->log_to_file, |
141
|
|
|
|
|
|
|
'to_stdout' => $self->log_to_stdout, |
142
|
|
|
|
|
|
|
'to_stderr' => $self->log_to_stderr, |
143
|
|
|
|
|
|
|
'log_pid' => $self->log_pid, |
144
|
|
|
|
|
|
|
'fail_fatal' => $self->log_fail_fatal, |
145
|
|
|
|
|
|
|
'muted' => $self->log_muted, |
146
|
|
|
|
|
|
|
'quiet_fatal' => $self->log_quiet_fatal, |
147
|
|
|
|
|
|
|
%optional, |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
); |
150
|
|
|
|
|
|
|
|
151
|
7
|
|
|
|
|
52366
|
return $logger; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# if we already have a logger, use its values |
155
|
|
|
|
|
|
|
sub BUILDARGS { |
156
|
13
|
|
|
13
|
1
|
54250
|
my $class = shift; |
157
|
13
|
|
|
|
|
30
|
my %args = @_; |
158
|
13
|
|
|
|
|
64
|
my @items = qw< |
159
|
|
|
|
|
|
|
debug logger_facility logger_ident |
160
|
|
|
|
|
|
|
log_to_file log_to_stdout log_to_stderr log_file log_path |
161
|
|
|
|
|
|
|
log_pid log_fail_fatal log_muted log_quiet_fatal |
162
|
|
|
|
|
|
|
>; |
163
|
|
|
|
|
|
|
|
164
|
13
|
100
|
|
|
|
42
|
if ( exists $args{'logger'} ) { |
165
|
|
|
|
|
|
|
$args{'logger'}->$_isa('Log::Dispatchouli') |
166
|
5
|
100
|
100
|
|
|
13
|
|| $args{'logger'}->$_isa('Log::Dispatchouli::Proxy') |
167
|
|
|
|
|
|
|
or Carp::croak('logger must be a Log::Dispatchouli object'); |
168
|
|
|
|
|
|
|
|
169
|
4
|
|
|
|
|
65
|
foreach my $item (@items) { |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
# if value is overridden, don't touch it |
172
|
|
|
|
|
|
|
my $attr |
173
|
|
|
|
|
|
|
= exists $attr_meth_map{$item} |
174
|
48
|
100
|
|
|
|
63
|
? $attr_meth_map{$item} |
175
|
|
|
|
|
|
|
: $item; |
176
|
|
|
|
|
|
|
|
177
|
48
|
100
|
|
|
|
46
|
if ( exists $args{$item} ) { |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
# override logger configuration |
180
|
1
|
|
|
|
|
3
|
$args{'logger'}{$attr} = $args{$item}; |
181
|
|
|
|
|
|
|
} else { |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
# override our attributes if it's in logger |
184
|
|
|
|
|
|
|
exists $args{'logger'}{$attr} |
185
|
47
|
100
|
|
|
|
125
|
and $args{$item} = $args{'logger'}{$attr}; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
12
|
|
|
|
|
201
|
return {%args}; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
sub log_fields { |
194
|
1
|
|
|
1
|
1
|
955
|
my $self = shift; |
195
|
1
|
|
|
|
|
2
|
my $warning |
196
|
|
|
|
|
|
|
= '[MooseX::Role::Loggable] Calling ->log_fields() is deprecated, ' |
197
|
|
|
|
|
|
|
. 'it will be removed in the next version'; |
198
|
|
|
|
|
|
|
|
199
|
1
|
|
|
|
|
4
|
$self->log( { 'level' => 'warning' }, $warning ); |
200
|
1
|
|
|
|
|
1549
|
Carp::carp($warning); |
201
|
|
|
|
|
|
|
|
202
|
1
|
|
|
|
|
373
|
return ( 'logger' => $self->logger ); |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
1; |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
__END__ |