line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Log::LTSV::Instance; |
2
|
2
|
|
|
2
|
|
1157
|
use 5.008001; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
81
|
|
3
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
64
|
|
4
|
2
|
|
|
2
|
|
17
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
63
|
|
5
|
2
|
|
|
2
|
|
1275
|
use Time::Piece; |
|
2
|
|
|
|
|
62231
|
|
|
2
|
|
|
|
|
13
|
|
6
|
2
|
|
|
2
|
|
1750
|
use File::RotateLogs; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use Data::Dumper; |
8
|
|
|
|
|
|
|
use Carp; |
9
|
|
|
|
|
|
|
use Log::LTSV::Instance::Flatten; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = "0.03"; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my %LOG_LEVEL_MAP = ( |
14
|
|
|
|
|
|
|
DEBUG => 1, |
15
|
|
|
|
|
|
|
INFO => 2, |
16
|
|
|
|
|
|
|
WARN => 3, |
17
|
|
|
|
|
|
|
CRITICAL => 4, |
18
|
|
|
|
|
|
|
ERROR => 99, |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new { |
22
|
|
|
|
|
|
|
my ($class, %args) = @_; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
my $level = $LOG_LEVEL_MAP{$args{level} || 'DEBUG'}; |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
Carp::croak("level required ERROR or CRITICAL or WARN or INFO or DEBUG") unless $level; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my ($logger, $rotatelogs); |
29
|
|
|
|
|
|
|
if ($args{logger}) { |
30
|
|
|
|
|
|
|
$logger = $args{logger}; |
31
|
|
|
|
|
|
|
} elsif (not defined $args{logfile}) { |
32
|
|
|
|
|
|
|
$logger = sub { print @_ }; |
33
|
|
|
|
|
|
|
} else { |
34
|
|
|
|
|
|
|
my $maxage = $args{maxage} || 60 * 60 * 24 * 1; |
35
|
|
|
|
|
|
|
my $rotatelogs = File::RotateLogs->new( |
36
|
|
|
|
|
|
|
logfile => $args{logfile}, |
37
|
|
|
|
|
|
|
maxage => $maxage, |
38
|
|
|
|
|
|
|
$args{linkname} ? ( linkname => $args{linkname} ) : (), |
39
|
|
|
|
|
|
|
$args{rotationtime} ? ( rotationtime => $args{rotationtime} ) : (), |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
$logger = sub { $rotatelogs->print(@_) }; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my $flatten = Log::LTSV::Instance::Flatten->new; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
bless { |
47
|
|
|
|
|
|
|
rotatelogs => $rotatelogs, |
48
|
|
|
|
|
|
|
logger => $logger, |
49
|
|
|
|
|
|
|
level => $level, |
50
|
|
|
|
|
|
|
sticks => {}, |
51
|
|
|
|
|
|
|
default_key => $args{default_key} || 'message', |
52
|
|
|
|
|
|
|
_flatten => $flatten, |
53
|
|
|
|
|
|
|
}, $class; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub error { shift->print('ERROR', @_) } |
57
|
|
|
|
|
|
|
sub crit { shift->print('CRITICAL', @_) } |
58
|
|
|
|
|
|
|
sub warn { shift->print('WARN', @_) } |
59
|
|
|
|
|
|
|
sub info { shift->print('INFO', @_) } |
60
|
|
|
|
|
|
|
sub debug { shift->print('DEBUG', @_) } |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub sticks { |
63
|
|
|
|
|
|
|
my ($self, @args) = @_; |
64
|
|
|
|
|
|
|
while (@args) { |
65
|
|
|
|
|
|
|
my ($key, $value) = splice @args, 0, 2; |
66
|
|
|
|
|
|
|
$self->{sticks}{$key} = $value; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub _escape { |
71
|
|
|
|
|
|
|
my ($self, $val) = @_; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
$val =~ s/\t/\\t/; |
74
|
|
|
|
|
|
|
$val =~ s/\n/\\n/; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
return $val; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub labeled_values { |
80
|
|
|
|
|
|
|
my ($self, $key, $value) = @_; |
81
|
|
|
|
|
|
|
my %lv = $self->{_flatten}->flatten($key, $value); |
82
|
|
|
|
|
|
|
$lv{$_} = $self->_escape($lv{$_}) for ( keys %lv ); |
83
|
|
|
|
|
|
|
map { join ':', $_, $lv{$_} } keys %lv; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub print { |
87
|
|
|
|
|
|
|
my ($self, $level, @args) = @_; |
88
|
|
|
|
|
|
|
return if ($LOG_LEVEL_MAP{$level} < $self->{level}); |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
if (ref $args[0] eq 'HASH') { |
91
|
|
|
|
|
|
|
@args = %{ $args[0] }; |
92
|
|
|
|
|
|
|
} elsif ( scalar @args == 1 && ref $args[0] eq '' ) { |
93
|
|
|
|
|
|
|
@args = ( $self->{default_key} => $args[0] ); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
my @msgs; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
push @msgs, sprintf("time:%s", localtime->datetime); |
99
|
|
|
|
|
|
|
push @msgs, "log_level:$level"; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
for my $key (keys %{ $self->{sticks} }) { |
102
|
|
|
|
|
|
|
my $value = $self->{sticks}->{$key}; |
103
|
|
|
|
|
|
|
$value = $value->() if ref $value; |
104
|
|
|
|
|
|
|
push @msgs, $self->labeled_values($key, $value); |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
while (@args) { |
108
|
|
|
|
|
|
|
my ($key, $value) = splice @args, 0, 2; |
109
|
|
|
|
|
|
|
push @msgs, $self->labeled_values($key, $value); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
my $ltsv = join "\t", @msgs; |
112
|
|
|
|
|
|
|
$self->{logger}->($ltsv."\n"); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
__END__ |