line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Log::Structured; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Log::Structured::VERSION = '0.001003'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: Log events in a structured manner |
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
102004
|
use Moo; |
|
2
|
|
|
|
|
52924
|
|
|
2
|
|
|
|
|
14
|
|
9
|
2
|
|
|
2
|
|
6900
|
use Sub::Quote; |
|
2
|
|
|
|
|
10504
|
|
|
2
|
|
|
|
|
155
|
|
10
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
8745
|
use Time::HiRes qw(gettimeofday tv_interval); |
|
2
|
|
|
|
|
5104
|
|
|
2
|
|
|
|
|
13
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has log_event_listeners => ( |
14
|
|
|
|
|
|
|
is => 'ro', |
15
|
|
|
|
|
|
|
isa => quote_sub(q< |
16
|
|
|
|
|
|
|
die "log_event_listeners must be an arrayref!" |
17
|
|
|
|
|
|
|
unless ref $_[0] && ref $_[0] eq 'ARRAY'; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
for (@{$_[0]}) { |
20
|
|
|
|
|
|
|
die "each log_event_listener must be a coderef!" |
21
|
|
|
|
|
|
|
unless ref $_ && ref $_ eq 'CODE'; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
>), |
24
|
|
|
|
|
|
|
default => quote_sub q{ [] }, |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has $_ => ( is => 'rw' ) for qw( |
28
|
|
|
|
|
|
|
caller_clan category priority start_time last_event |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
has caller_depth => ( |
32
|
|
|
|
|
|
|
is => 'rw', |
33
|
|
|
|
|
|
|
predicate => 'has_caller_depth', |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
has "log_$_" => ( is => 'rw' ) for qw( |
37
|
|
|
|
|
|
|
milliseconds_since_start milliseconds_since_last_log |
38
|
|
|
|
|
|
|
line file package subroutine category priority |
39
|
|
|
|
|
|
|
date host pid stacktrace |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub add_log_event_listener { |
43
|
3
|
|
|
3
|
1
|
776
|
my $self = shift; |
44
|
3
|
|
|
|
|
4
|
my $code = shift; |
45
|
|
|
|
|
|
|
|
46
|
3
|
100
|
100
|
|
|
38
|
die 'log_event_listener must be a coderef!' |
47
|
|
|
|
|
|
|
unless ref $code && ref $code eq 'CODE'; |
48
|
|
|
|
|
|
|
|
49
|
1
|
|
|
|
|
4
|
push @{$self->log_event_listeners}, $code |
|
1
|
|
|
|
|
6
|
|
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub BUILD { |
53
|
4
|
|
|
4
|
0
|
15433
|
$_[0]->start_time([gettimeofday]); |
54
|
4
|
|
|
|
|
114
|
$_[0]->last_event([gettimeofday]); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub log_event { |
58
|
9
|
|
|
9
|
1
|
79036
|
my $self = shift; |
59
|
9
|
|
|
|
|
17
|
my $event_data = shift; |
60
|
|
|
|
|
|
|
|
61
|
9
|
|
100
|
|
|
24
|
$self->${\"log_$_"} and $event_data->{$_} ||= $self->$_ for qw( |
|
108
|
|
100
|
|
|
635
|
|
62
|
|
|
|
|
|
|
milliseconds_since_start milliseconds_since_last_log |
63
|
|
|
|
|
|
|
line file package subroutine category priority |
64
|
|
|
|
|
|
|
date host pid stacktrace |
65
|
|
|
|
|
|
|
); |
66
|
|
|
|
|
|
|
|
67
|
9
|
|
|
|
|
17
|
$self->$_($event_data) for @{$self->log_event_listeners}; |
|
9
|
|
|
|
|
52
|
|
68
|
|
|
|
|
|
|
|
69
|
9
|
|
|
|
|
93
|
$self->last_event([gettimeofday]); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub milliseconds_since_start { |
73
|
1
|
|
|
1
|
1
|
10
|
int tv_interval(shift->start_time, [ gettimeofday ]) * 1000 |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub milliseconds_since_last_log { |
77
|
1
|
|
|
1
|
1
|
7
|
int tv_interval(shift->last_event, [ gettimeofday ]) * 1000 |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
8
|
|
|
8
|
1
|
25
|
sub line { shift->_caller->[2] } |
81
|
|
|
|
|
|
|
|
82
|
8
|
|
|
8
|
1
|
22
|
sub file { shift->_caller->[1] } |
83
|
|
|
|
|
|
|
|
84
|
8
|
|
|
8
|
1
|
17
|
sub package { shift->_caller->[0] } |
85
|
|
|
|
|
|
|
|
86
|
8
|
|
|
8
|
1
|
17
|
sub subroutine { shift->_caller->[3] } |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub _caller { |
89
|
32
|
|
|
32
|
|
44
|
my $self = shift; |
90
|
|
|
|
|
|
|
|
91
|
32
|
|
|
|
|
54
|
$self->_sound_depth->[1] |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
1
|
|
|
1
|
1
|
179
|
sub date { return [localtime] } |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub host { |
97
|
1
|
|
|
1
|
1
|
1082
|
require Sys::Hostname; |
98
|
1
|
|
|
|
|
1826
|
return Sys::Hostname::hostname() |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
1
|
|
|
1
|
1
|
8
|
sub pid { $$ } |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub stacktrace { |
104
|
2
|
|
|
2
|
1
|
3
|
my $self = shift; |
105
|
|
|
|
|
|
|
|
106
|
2
|
|
|
|
|
3
|
my @trace; |
107
|
2
|
|
|
|
|
3
|
my $i = 1; |
108
|
2
|
|
|
|
|
14
|
while (my @callerinfo = caller($i)) { |
109
|
6
|
|
|
|
|
7
|
$i++; |
110
|
6
|
|
|
|
|
30
|
push @trace, \@callerinfo |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
\@trace |
113
|
2
|
|
|
|
|
9
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub _sound_depth { |
116
|
32
|
|
|
32
|
|
37
|
my $self = shift; |
117
|
|
|
|
|
|
|
|
118
|
32
|
100
|
|
|
|
4932
|
my $depth = $self->has_caller_depth ? $self->caller_depth : 0; |
119
|
32
|
|
|
|
|
54
|
my $clan = $self->caller_clan; |
120
|
|
|
|
|
|
|
|
121
|
32
|
|
|
|
|
36
|
$depth += 3; |
122
|
|
|
|
|
|
|
|
123
|
32
|
100
|
|
|
|
55
|
if (defined $clan) { |
124
|
8
|
|
100
|
|
|
11
|
my $c; do { |
|
8
|
|
|
|
|
9
|
|
125
|
12
|
|
|
|
|
76
|
$c = caller ++$depth; |
126
|
|
|
|
|
|
|
} while $c && $c =~ $clan; |
127
|
8
|
|
|
|
|
75
|
return [$depth, [caller $depth]] |
128
|
|
|
|
|
|
|
} else { |
129
|
24
|
|
|
|
|
252
|
return [$depth, [caller $depth]] |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
1; |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
__END__ |