line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package ControlFreak::Logger; |
2
|
8
|
|
|
8
|
|
46
|
use strict; |
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
331
|
|
3
|
8
|
|
|
8
|
|
46
|
use warnings; |
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
303
|
|
4
|
|
|
|
|
|
|
|
5
|
8
|
|
|
8
|
|
45
|
use Carp; |
|
8
|
|
|
|
|
18
|
|
|
8
|
|
|
|
|
641
|
|
6
|
8
|
|
|
8
|
|
10764
|
use Log::Log4perl(); |
|
8
|
|
|
|
|
364065
|
|
|
8
|
|
|
|
|
242
|
|
7
|
|
|
|
|
|
|
|
8
|
8
|
|
|
8
|
|
103
|
use Object::Tiny qw{ config_file }; |
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
81
|
|
9
|
8
|
|
|
8
|
|
1437
|
use Params::Util qw{ _STRING }; |
|
8
|
|
|
|
|
20
|
|
|
8
|
|
|
|
|
531
|
|
10
|
8
|
|
|
8
|
|
46
|
use Try::Tiny; |
|
8
|
|
|
|
|
18
|
|
|
8
|
|
|
|
|
2397
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $CURRENT_SVC_PID; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Log::Log4perl::Layout::PatternLayout::add_global_cspec( |
15
|
|
|
|
|
|
|
'S', sub { $ControlFreak::Logger::CURRENT_SVC_PID || "-" }, |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub new { |
19
|
14
|
|
|
14
|
0
|
37
|
my $class = shift; |
20
|
14
|
|
|
|
|
96
|
my $logger = $class->SUPER::new(@_); |
21
|
|
|
|
|
|
|
|
22
|
14
|
50
|
|
|
|
612
|
if (my $config_file = $logger->config_file) { |
23
|
0
|
0
|
|
|
|
0
|
unless (-e $config_file) { |
24
|
0
|
|
|
|
|
0
|
croak "cannot find '$config_file'"; |
25
|
|
|
|
|
|
|
} |
26
|
0
|
|
|
|
|
0
|
Log::Log4perl->init($config_file); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
else { |
29
|
14
|
|
|
|
|
192
|
Log::Log4perl->init( $logger->default_config ); |
30
|
|
|
|
|
|
|
} |
31
|
14
|
|
|
|
|
51666
|
return $logger; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub safe_reinit { |
35
|
0
|
|
|
0
|
0
|
0
|
my $logger = shift; |
36
|
0
|
0
|
|
|
|
0
|
unless ($logger->config_file) { |
37
|
0
|
|
|
|
|
0
|
$logger->warn("Ignored USR1, running with file-less config"); |
38
|
0
|
|
|
|
|
0
|
return; |
39
|
|
|
|
|
|
|
} |
40
|
0
|
|
|
|
|
0
|
$logger->info("Reloading log config"); |
41
|
|
|
|
|
|
|
try { |
42
|
0
|
|
|
0
|
|
0
|
Log::Log4perl->init($logger->config_file); |
43
|
0
|
|
|
|
|
0
|
$logger->info("Log config reloaded"); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
catch { |
46
|
|
|
|
|
|
|
## damn ugly |
47
|
0
|
|
|
0
|
|
0
|
warn "reloading config failed"; |
48
|
8
|
|
|
8
|
|
52
|
use Log::Log4perl::Logger; |
|
8
|
|
|
|
|
28
|
|
|
8
|
|
|
|
|
3183
|
|
49
|
0
|
|
|
|
|
0
|
Log::Log4perl::Config->_init(undef, $Log::Log4perl::Config::OLD_CONFIG); |
50
|
0
|
|
|
|
|
0
|
$logger->error("There is an error in my config. Aborting. ($_)"); |
51
|
0
|
|
|
|
|
0
|
}; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub default_config { |
55
|
0
|
|
|
0
|
0
|
0
|
return \<<'EOFC'; |
56
|
|
|
|
|
|
|
log4perl.rootLogger=INFO, ALL |
57
|
|
|
|
|
|
|
log4perl.appender.ALL=Log::Log4perl::Appender::File |
58
|
|
|
|
|
|
|
log4perl.appender.ALL.filename=sub { $ENV{CFKD_HOME} . "/cfkd.log" } |
59
|
|
|
|
|
|
|
log4perl.appender.ALL.mode=append |
60
|
|
|
|
|
|
|
log4perl.appender.ALL.layout=PatternLayout |
61
|
|
|
|
|
|
|
# %S = service pid |
62
|
|
|
|
|
|
|
log4perl.appender.ALL.layout.ConversionPattern=%S %p %L %c - %m%n |
63
|
|
|
|
|
|
|
EOFC |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub log_handle { |
67
|
243
|
|
|
243
|
0
|
742
|
my $logger = shift; |
68
|
243
|
|
|
|
|
3316
|
Log::Log4perl->get_logger(@_); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub svc_watcher { |
72
|
58
|
|
|
58
|
0
|
130
|
my $logger = shift; |
73
|
|
|
|
|
|
|
## type is 'out' our 'err' |
74
|
58
|
|
|
|
|
167
|
my ($type, $svc) = @_; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
## configurable? |
77
|
58
|
100
|
|
|
|
323
|
my $logmethod = $type eq 'err' ? 'error' : 'info'; |
78
|
|
|
|
|
|
|
my $watcher_cb = sub { |
79
|
65
|
|
|
65
|
|
531653
|
my $msg = shift; |
80
|
65
|
100
|
|
|
|
3967
|
return unless defined $msg; |
81
|
9
|
50
|
|
|
|
70
|
chomp $msg if $msg; |
82
|
9
|
|
|
|
|
174
|
return $logger->_svclog($logmethod, $type, $svc, $msg); |
83
|
58
|
|
|
|
|
423
|
}; |
84
|
58
|
|
|
|
|
247
|
return $watcher_cb; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub _svclog { |
88
|
9
|
|
|
9
|
|
33
|
my $logger = shift; |
89
|
9
|
|
|
|
|
81
|
my ($logmethod, $type, $svc, $msg) = @_; |
90
|
|
|
|
|
|
|
|
91
|
9
|
|
|
|
|
1380
|
my $svcname = $svc->name; |
92
|
|
|
|
|
|
|
## for 'S' cspec |
93
|
9
|
|
|
|
|
1205
|
local $CURRENT_SVC_PID = $svc->pid; |
94
|
9
|
|
|
|
|
172
|
my $log_handle = $logger->log_handle("service.$svcname.$type"); |
95
|
9
|
50
|
|
|
|
3698
|
chomp $msg if $msg; |
96
|
9
|
|
|
|
|
115
|
$log_handle->$logmethod($msg); |
97
|
9
|
|
|
|
|
2513
|
return; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
for my $lvl (qw/trace debug info warn error fatal/) { |
101
|
8
|
|
|
8
|
|
47
|
no strict 'refs'; |
|
8
|
|
|
|
|
18
|
|
|
8
|
|
|
|
|
2985
|
|
102
|
|
|
|
|
|
|
*{$lvl} = sub { |
103
|
233
|
|
|
233
|
|
4959
|
local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1; |
104
|
233
|
|
|
|
|
1716
|
shift->log_handle->$lvl(@_); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub proxy_log { |
109
|
0
|
|
|
0
|
0
|
|
my $logger = shift; |
110
|
0
|
|
|
|
|
|
my ($data) = @_; |
111
|
0
|
|
|
|
|
|
my ($type, $proxy, $msg) = @$data; |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
my $proxyname = $proxy->name; |
114
|
0
|
0
|
|
|
|
|
my $logmethod = $type eq 'err' ? 'error' : 'info'; |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
my $log_handle = $logger->log_handle("proxy.$proxyname.$type"); |
117
|
0
|
0
|
|
|
|
|
chomp $msg if $msg; |
118
|
0
|
|
|
|
|
|
$log_handle->$logmethod($msg); |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub proxy_svc_log { |
122
|
0
|
|
|
0
|
0
|
|
my $logger = shift; |
123
|
0
|
|
|
|
|
|
my ($data) = @_; |
124
|
0
|
|
|
|
|
|
my ($type, $svc, $msg) = @$data; |
125
|
0
|
0
|
|
|
|
|
my $logmethod = $type eq 'err' ? 'error' : 'info'; |
126
|
0
|
|
|
|
|
|
$logger->_svclog($logmethod, $type, $svc, $msg); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub set_config { |
130
|
0
|
|
|
0
|
0
|
|
my $logger = shift; |
131
|
0
|
0
|
|
|
|
|
my $configfile = _STRING(shift) or return; |
132
|
|
|
|
|
|
|
## reinit |
133
|
0
|
|
|
|
|
|
Log::Log4perl->init($configfile); |
134
|
0
|
|
|
|
|
|
return 1; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub unset { |
138
|
0
|
|
|
0
|
0
|
|
my $logger = shift; |
139
|
0
|
|
|
|
|
|
my $what = _STRING(shift); |
140
|
0
|
0
|
0
|
|
|
|
return unless $what or $what eq 'configfile'; |
141
|
0
|
|
|
|
|
|
return 1; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 NAME |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
ControlFreak::Logger - All about logging |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=cut |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 SYNOPSIS |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 AUTHOR |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Yann Kerherve |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=cut |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
"zog-zog"; |