line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Log::Any::Adapter::JSON; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '1.15'; |
4
|
|
|
|
|
|
|
|
5
|
10
|
|
|
10
|
|
1863622
|
use strict; |
|
10
|
|
|
|
|
84
|
|
|
10
|
|
|
|
|
333
|
|
6
|
10
|
|
|
10
|
|
61
|
use warnings; |
|
10
|
|
|
|
|
23
|
|
|
10
|
|
|
|
|
290
|
|
7
|
10
|
|
|
10
|
|
56
|
use feature 'say'; |
|
10
|
|
|
|
|
21
|
|
|
10
|
|
|
|
|
1529
|
|
8
|
|
|
|
|
|
|
|
9
|
10
|
|
|
10
|
|
75
|
use Carp qw/ croak confess /; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
716
|
|
10
|
10
|
|
|
10
|
|
100
|
use Cpanel::JSON::XS; |
|
10
|
|
|
|
|
23
|
|
|
10
|
|
|
|
|
696
|
|
11
|
10
|
|
|
10
|
|
923
|
use Path::Tiny; |
|
10
|
|
|
|
|
11988
|
|
|
10
|
|
|
|
|
555
|
|
12
|
10
|
|
|
10
|
|
5134
|
use Time::Moment; |
|
10
|
|
|
|
|
16890
|
|
|
10
|
|
|
|
|
415
|
|
13
|
10
|
|
|
10
|
|
5626
|
use strictures 2; |
|
10
|
|
|
|
|
17764
|
|
|
10
|
|
|
|
|
462
|
|
14
|
|
|
|
|
|
|
|
15
|
10
|
|
|
10
|
|
2937
|
use Log::Any::Adapter::Util 'make_method'; |
|
10
|
|
|
|
|
9090
|
|
|
10
|
|
|
|
|
638
|
|
16
|
|
|
|
|
|
|
|
17
|
10
|
|
|
10
|
|
558
|
use parent 'Log::Any::Adapter::Base'; |
|
10
|
|
|
|
|
328
|
|
|
10
|
|
|
|
|
111
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $trace_level = Log::Any::Adapter::Util::numeric_level('trace'); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new { |
22
|
9
|
|
|
9
|
0
|
990
|
my ($class, $filename_or_handle, %args) = @_; |
23
|
|
|
|
|
|
|
|
24
|
9
|
|
|
|
|
22
|
my $handle; |
25
|
9
|
|
|
|
|
29
|
my $ref = ref($filename_or_handle); |
26
|
|
|
|
|
|
|
|
27
|
9
|
50
|
33
|
|
|
96
|
if ( $ref && $ref ne 'GLOB' ) { |
|
|
50
|
|
|
|
|
|
28
|
0
|
|
|
|
|
0
|
croak('Died: Not a filehandle'); |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
elsif ($ref) { |
31
|
9
|
|
|
|
|
19
|
$handle = $filename_or_handle; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
else { |
34
|
0
|
|
|
|
|
0
|
$handle = path($filename_or_handle)->opena; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
9
|
|
|
|
|
162
|
$handle->autoflush; |
38
|
|
|
|
|
|
|
|
39
|
9
|
|
50
|
|
|
980
|
my $encoding = delete($args{encoding}) || 'UTF-8'; |
40
|
9
|
|
|
9
|
|
300
|
binmode $handle, ":encoding($encoding)"; |
|
9
|
|
|
|
|
72
|
|
|
9
|
|
|
|
|
21
|
|
|
9
|
|
|
|
|
103
|
|
41
|
|
|
|
|
|
|
|
42
|
9
|
|
|
|
|
12862
|
$args{handle} = $handle; |
43
|
9
|
|
33
|
|
|
79
|
$args{log_level} //= $trace_level; |
44
|
|
|
|
|
|
|
|
45
|
9
|
|
|
|
|
135
|
return $class->SUPER::new(%args); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub structured { |
49
|
24
|
|
|
24
|
0
|
715
|
my $self = shift; |
50
|
24
|
|
|
|
|
82
|
my ($level, $category, $string, @items) = @_; |
51
|
|
|
|
|
|
|
|
52
|
24
|
50
|
|
|
|
100
|
return if Log::Any::Adapter::Util::numeric_level($level) > $self->{log_level}; |
53
|
|
|
|
|
|
|
|
54
|
24
|
|
|
|
|
410
|
my $log_entry = _prepare_log_entry($self, @_); |
55
|
|
|
|
|
|
|
|
56
|
15
|
|
|
|
|
87
|
select $self->{handle}; |
57
|
15
|
|
|
|
|
931
|
say $log_entry; |
58
|
15
|
|
|
|
|
127
|
select STDOUT; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub _prepare_log_entry { |
62
|
24
|
|
|
24
|
|
78
|
my ($self, $level, $category, $string, @items) = @_; |
63
|
|
|
|
|
|
|
|
64
|
24
|
100
|
|
|
|
100
|
confess 'Died: A log message is required' if ! $string; |
65
|
|
|
|
|
|
|
|
66
|
22
|
100
|
|
|
|
79
|
my $method = $self->{localtime} ? 'now' : 'now_utc'; |
67
|
|
|
|
|
|
|
|
68
|
22
|
|
|
|
|
309
|
my $time = Time::Moment->$method; |
69
|
22
|
|
|
|
|
161
|
my $float = $time->strftime('%f'); |
70
|
22
|
|
50
|
|
|
70
|
$float ||= '.'; |
71
|
22
|
|
|
|
|
81
|
$float .= 0 while length $float < 7; |
72
|
|
|
|
|
|
|
|
73
|
22
|
|
|
|
|
322
|
my %log_entry = ( |
74
|
|
|
|
|
|
|
time => join('', $time->strftime('%FT%T'), $float, $time->strftime('%Z')), |
75
|
|
|
|
|
|
|
level => $level, |
76
|
|
|
|
|
|
|
category => $category, |
77
|
|
|
|
|
|
|
); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# Process pattern and values if present, unless not wanted |
80
|
22
|
100
|
100
|
|
|
237
|
if ( ! $self->{without_formatting} && (my $num_tokens =()= $string =~ m/%s|%d/g) ) { |
81
|
8
|
|
|
|
|
24
|
my @vals = grep { ! ref } splice @items, 0, $num_tokens; |
|
16
|
|
|
|
|
37
|
|
82
|
|
|
|
|
|
|
|
83
|
8
|
100
|
|
|
|
23
|
if ( @vals < $num_tokens ) { |
84
|
2
|
50
|
|
|
|
6
|
my $inflected = $num_tokens == 1 ? 'value is' : 'values are'; |
85
|
2
|
|
|
|
|
33
|
confess sprintf('Died: %s scalar %s required for this pattern', $num_tokens, $inflected); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
6
|
|
|
|
|
45
|
$log_entry{message} = sprintf($string, @vals); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
else { |
91
|
14
|
|
|
|
|
44
|
$log_entry{message} = $string; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# Process structured data and additional messages if present. |
95
|
|
|
|
|
|
|
# The first hashref encountered has its keys promoted to top-level. |
96
|
19
|
|
|
|
|
37
|
my $seen_href; |
97
|
|
|
|
|
|
|
|
98
|
19
|
|
|
|
|
52
|
for my $item ( @items ) { |
99
|
|
|
|
|
|
|
|
100
|
18
|
100
|
|
|
|
56
|
if ( ref($item) eq 'HASH' ) { |
|
|
100
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# special handling for Log::Any's context hash |
102
|
10
|
100
|
|
|
|
32
|
if ( $item->{context} ) { |
103
|
2
|
|
|
|
|
7
|
$log_entry{context} = delete $item->{context}; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
10
|
100
|
|
|
|
32
|
if ( ! $seen_href ) { |
107
|
8
|
|
|
|
|
18
|
for my $key ( keys %{ $item } ) { |
|
8
|
|
|
|
|
30
|
|
108
|
8
|
100
|
|
|
|
46
|
if ( $key =~ /^(?:time|level|category|message)$/ ) { |
109
|
4
|
|
|
|
|
68
|
confess sprintf( |
110
|
|
|
|
|
|
|
'Died: %s is a reserved key name and may not be passed in the first hashref', |
111
|
|
|
|
|
|
|
$key, |
112
|
|
|
|
|
|
|
); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
4
|
|
|
|
|
14
|
$log_entry{$key} = $item->{$key}; |
116
|
|
|
|
|
|
|
} |
117
|
4
|
|
|
|
|
15
|
$seen_href++; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
else { |
120
|
2
|
|
|
|
|
5
|
push @{ $log_entry{hash_data} }, $item; |
|
2
|
|
|
|
|
9
|
|
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
elsif ( ref($item) eq 'ARRAY' ) { |
124
|
2
|
|
|
|
|
4
|
push @{ $log_entry{list_data} }, $item; |
|
2
|
|
|
|
|
5
|
|
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
else { |
127
|
6
|
|
|
|
|
12
|
push ( @{ $log_entry{additional_messages} }, $item); |
|
6
|
|
|
|
|
19
|
|
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
15
|
|
|
|
|
250
|
my $serializer = Cpanel::JSON::XS->new |
132
|
|
|
|
|
|
|
->utf8(0) |
133
|
|
|
|
|
|
|
->pretty(0) |
134
|
|
|
|
|
|
|
->indent(0) |
135
|
|
|
|
|
|
|
->canonical(1) |
136
|
|
|
|
|
|
|
->allow_stringify(1); |
137
|
|
|
|
|
|
|
|
138
|
15
|
|
|
|
|
318
|
return $serializer->encode( \%log_entry ); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
#-- Methods required by the base class --------------------------------# |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub init { |
144
|
9
|
|
|
9
|
0
|
110
|
my $self = shift; |
145
|
9
|
50
|
33
|
|
|
99
|
if ( $self->{log_level} && $self->{log_level} =~ /\D/ ) { |
146
|
0
|
|
|
|
|
0
|
my $numeric_level = Log::Any::Adapter::Util::numeric_level( $self->{log_level} ); |
147
|
|
|
|
|
|
|
|
148
|
0
|
0
|
|
|
|
0
|
if ( ! defined($numeric_level) ) { |
149
|
0
|
|
|
|
|
0
|
croak sprintf('Invalid log level [%s]', $self->{log_level}); |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
0
|
$self->{log_level} = $numeric_level; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
9
|
50
|
|
|
|
45
|
if ( ! defined $self->{log_level} ) { |
156
|
0
|
|
|
|
|
0
|
$self->{log_level} = $trace_level; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
for my $method ( Log::Any->detection_methods ) { |
161
|
|
|
|
|
|
|
my $base = substr($method, 3); |
162
|
|
|
|
|
|
|
my $method_level = Log::Any::Adapter::Util::numeric_level( $base ); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
make_method( $method, sub { |
165
|
24
|
|
|
24
|
|
77170
|
return !!( $method_level <= $_[0]->{log_level} ); |
166
|
|
|
|
|
|
|
}); |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
1; # return true |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
__END__ |