| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
################################################## |
|
2
|
|
|
|
|
|
|
package Log::Log4perl::Layout::LTSV; |
|
3
|
|
|
|
|
|
|
################################################## |
|
4
|
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
103528
|
use 5.008_001; |
|
|
2
|
|
|
|
|
231
|
|
|
6
|
2
|
|
|
2
|
|
10
|
use strict; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
504
|
|
|
7
|
2
|
|
|
2
|
|
267
|
use warnings; |
|
|
2
|
|
|
|
|
7
|
|
|
|
2
|
|
|
|
|
518
|
|
|
8
|
2
|
|
|
2
|
|
2834
|
use Encode; |
|
|
2
|
|
|
|
|
20749
|
|
|
|
2
|
|
|
|
|
156
|
|
|
9
|
2
|
|
|
2
|
|
1180
|
use Log::Log4perl; |
|
|
2
|
|
|
|
|
49960
|
|
|
|
2
|
|
|
|
|
12
|
|
|
10
|
2
|
|
|
2
|
|
1600
|
use POSIX qw(strftime); |
|
|
2
|
|
|
|
|
12446
|
|
|
|
2
|
|
|
|
|
11
|
|
|
11
|
2
|
|
|
2
|
|
2619
|
use base qw(Log::Log4perl::Layout::PatternLayout); |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
1168
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
$Log::Log4perl::ALLOW_CODE_IN_CONFIG_FILE = 1; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
|
16
|
|
|
|
|
|
|
Log::Log4perl::Layout::LTSV - Log4perl for LTSV |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 VERSION |
|
19
|
|
|
|
|
|
|
Version 1.0 |
|
20
|
|
|
|
|
|
|
=cut |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $VERSION = '1.0'; |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
25
|
|
|
|
|
|
|
Log4perl implementation of LTSV. |
|
26
|
|
|
|
|
|
|
=head1 Configuration Sample |
|
27
|
|
|
|
|
|
|
use Log::Log4perl |
|
28
|
|
|
|
|
|
|
my $logger_conf = { |
|
29
|
|
|
|
|
|
|
'log4perl.logger.test' => 'DEBUG, SERVER', |
|
30
|
|
|
|
|
|
|
'log4perl.appender.SERVER' => 'Log::Log4perl::Appender::Socket', |
|
31
|
|
|
|
|
|
|
'log4perl.appender.SERVER.PeerAddr' => '10.1.2.3', |
|
32
|
|
|
|
|
|
|
'log4perl.appender.SERVER.PeerPort' => '514', |
|
33
|
|
|
|
|
|
|
'log4perl.appender.SERVER.Proto' => 'tcp', |
|
34
|
|
|
|
|
|
|
'log4perl.appender.SERVER.layout' => 'LTSV', |
|
35
|
|
|
|
|
|
|
'log4perl.appender.SERVER.layout.facility' => 'Custom facility' |
|
36
|
|
|
|
|
|
|
}; |
|
37
|
|
|
|
|
|
|
Log::Log4perl->init($logger_conf); |
|
38
|
|
|
|
|
|
|
my $LOGGER = Log::Log4perl->get_logger('test'); |
|
39
|
|
|
|
|
|
|
$LOGGER->debug('Debug log'); |
|
40
|
|
|
|
|
|
|
... |
|
41
|
|
|
|
|
|
|
=cut |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head2 new |
|
46
|
|
|
|
|
|
|
Can take most of options that Log::Log4perl::Layout::PatternLayout can. |
|
47
|
|
|
|
|
|
|
=cut |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub new { |
|
50
|
2
|
|
|
2
|
1
|
3065
|
my $class = shift; |
|
51
|
2
|
|
33
|
|
|
15
|
$class = ref($class) || $class; |
|
52
|
2
|
100
|
|
|
|
10
|
my $options = ref $_[0] eq 'HASH' ? shift : {}; |
|
53
|
2
|
|
|
|
|
19
|
my $record = { |
|
54
|
|
|
|
|
|
|
'time' => '%Z', |
|
55
|
|
|
|
|
|
|
'host' => '%H', |
|
56
|
|
|
|
|
|
|
'message' => '%m{chomp}', |
|
57
|
|
|
|
|
|
|
'level' => '%Y', |
|
58
|
|
|
|
|
|
|
'facility' => '%M', |
|
59
|
|
|
|
|
|
|
'file' => '%F', |
|
60
|
|
|
|
|
|
|
'line' => '%L', |
|
61
|
|
|
|
|
|
|
'pid' => '%P', |
|
62
|
|
|
|
|
|
|
}; |
|
63
|
2
|
|
|
|
|
5
|
while ( my ($key) = each %{ $options->{field} } ) { |
|
|
2
|
|
|
|
|
13
|
|
|
64
|
0
|
|
|
|
|
0
|
$record->{$key} = $options->{field}->{$key}->{value}; |
|
65
|
|
|
|
|
|
|
} |
|
66
|
2
|
|
|
|
|
9
|
my $conversion_pattern = _encode_ltsv($record); |
|
67
|
2
|
|
|
|
|
10
|
$options->{ConversionPattern} = { value => $conversion_pattern }; |
|
68
|
|
|
|
|
|
|
$options->{cspec} = { |
|
69
|
|
|
|
|
|
|
'Y' => { value => \&_level_converter }, |
|
70
|
|
|
|
|
|
|
'Z' => { |
|
71
|
|
|
|
|
|
|
value => sub { |
|
72
|
5
|
|
|
5
|
|
686
|
return strftime( '[%Y-%m-%dT%H:%M:%SZ]', gmtime( time() ) ); |
|
73
|
|
|
|
|
|
|
} |
|
74
|
|
|
|
|
|
|
} |
|
75
|
2
|
|
|
|
|
22
|
}; |
|
76
|
2
|
|
|
|
|
29
|
return $class->SUPER::new($options); |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub _encode_ltsv { |
|
80
|
2
|
|
|
2
|
|
4
|
my $hash = shift; |
|
81
|
2
|
|
|
|
|
4
|
my @res; |
|
82
|
2
|
|
|
|
|
11
|
while ( my ($key, $value) = each %$hash ) { |
|
83
|
16
|
|
|
|
|
29
|
$value =~ s/[\r\n\t]/ /g; |
|
84
|
16
|
50
|
|
|
|
57
|
if ( not Encode::is_utf8( $value, 1 ) ) { |
|
85
|
16
|
|
|
|
|
61
|
$value = Encode::encode( 'UTF-8', $value, Encode::FB_CROAK ); |
|
86
|
16
|
|
|
|
|
808
|
Encode::_utf8_on($value); |
|
87
|
|
|
|
|
|
|
} |
|
88
|
16
|
|
|
|
|
75
|
push( @res, join( ':', $key, $value ) ); |
|
89
|
|
|
|
|
|
|
} |
|
90
|
2
|
|
|
|
|
13
|
return join( "\t", @res ); |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub _level_converter { |
|
94
|
5
|
|
|
5
|
|
68
|
my ( $layout, $message, $category, $priority, $caller_level ) = @_; |
|
95
|
5
|
|
|
|
|
19
|
my $levels = { |
|
96
|
|
|
|
|
|
|
'FATAL' => 2, |
|
97
|
|
|
|
|
|
|
'ERROR' => 3, |
|
98
|
|
|
|
|
|
|
'WARN' => 4, |
|
99
|
|
|
|
|
|
|
'NOTICE' => 5, |
|
100
|
|
|
|
|
|
|
'INFO' => 6, |
|
101
|
|
|
|
|
|
|
'DEBUG' => 7 |
|
102
|
|
|
|
|
|
|
}; |
|
103
|
5
|
|
|
|
|
20
|
return $levels->{$priority}; |
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head2 render |
|
107
|
|
|
|
|
|
|
Wraps the Log::Log4perl::Layout::PatternLayout return value |
|
108
|
|
|
|
|
|
|
=cut |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub render { |
|
111
|
5
|
|
|
5
|
1
|
1678
|
my ( $self, $message, $category, $priority, $caller_level ) = @_; |
|
112
|
5
|
|
|
|
|
18
|
return $self->SUPER::render( $message, $category, $priority, $caller_level ) |
|
113
|
|
|
|
|
|
|
. "\n"; |
|
114
|
|
|
|
|
|
|
} |
|
115
|
|
|
|
|
|
|
1; |