line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::CustomLog; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
$Mojolicious::Plugin::CustomLog::VERSION = '0.06'; |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
607
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
171
|
use Mojo::Util 'encode'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
52
|
|
8
|
1
|
|
|
1
|
|
9
|
use Fcntl ':flock'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
267
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
my $PATH = +{}; |
11
|
|
|
|
|
|
|
my $MODE; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub register { |
14
|
1
|
|
|
1
|
1
|
32
|
my $self = shift; |
15
|
1
|
|
|
|
|
1
|
my ($app, $config) = @_; |
16
|
|
|
|
|
|
|
|
17
|
1
|
|
|
|
|
6
|
$MODE = $app->mode; |
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
|
|
4
|
while ( my ($key, $value) = each(%{$config->{path}}) ) { |
|
3
|
|
|
|
|
11
|
|
20
|
2
|
|
|
|
|
5
|
my $file = $app->home->rel_file($value . "_" . $MODE . ".log"); |
21
|
2
|
|
|
|
|
61
|
$PATH->{$key} = $file; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# attach log instance to $app->clog |
25
|
|
|
|
|
|
|
# if there is no helper name provided |
26
|
1
|
|
50
|
|
|
3
|
$config->{helper} ||= 'clog'; |
27
|
1
|
|
|
1
|
|
9
|
$app->attr("_clog", sub { return $self; }); |
|
1
|
|
|
|
|
10
|
|
28
|
|
|
|
|
|
|
$app->helper( |
29
|
|
|
|
|
|
|
$config->{helper} => sub { |
30
|
1
|
|
|
1
|
|
7617
|
my $self = shift; |
31
|
1
|
|
|
|
|
2
|
return $self->app->_clog; |
32
|
|
|
|
|
|
|
} |
33
|
1
|
|
|
|
|
37
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# export alias of this logger |
36
|
|
|
|
|
|
|
# |
37
|
|
|
|
|
|
|
# alias is very useful when $app can not be easily accessed |
38
|
|
|
|
|
|
|
# for example, using $Alias::CLog->debug instead of $app->clog |
39
|
|
|
|
|
|
|
# |
40
|
|
|
|
|
|
|
# however, this will currupt other namespace |
41
|
|
|
|
|
|
|
# so please use this with caution |
42
|
1
|
50
|
|
|
|
25
|
if ($config->{alias}) { |
43
|
1
|
|
|
1
|
|
4
|
no strict 'refs'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
515
|
|
44
|
1
|
|
|
|
|
2
|
*{"$config->{alias}::CLog"} = \$self; |
|
1
|
|
|
|
|
5
|
|
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub debug { |
49
|
1
|
|
|
1
|
0
|
1
|
my $self = shift; |
50
|
1
|
|
|
|
|
1
|
my $target = shift; |
51
|
1
|
|
|
|
|
2
|
my @contents = @_; |
52
|
1
|
|
|
|
|
4
|
$self->_out($target, 'debug', @contents); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub info { |
56
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
57
|
0
|
|
|
|
|
0
|
my $target = shift; |
58
|
0
|
|
|
|
|
0
|
my @contents = @_; |
59
|
0
|
|
|
|
|
0
|
$self->_out($target, 'info', @contents); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub warn { |
63
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
64
|
0
|
|
|
|
|
0
|
my $target = shift; |
65
|
0
|
|
|
|
|
0
|
my @contents = @_; |
66
|
0
|
|
|
|
|
0
|
$self->_out($target, 'warn', @contents); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub error { |
70
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
71
|
1
|
|
|
|
|
2
|
my $target = shift; |
72
|
1
|
|
|
|
|
2
|
my @contents = @_; |
73
|
1
|
|
|
|
|
2
|
$self->_out($target, 'error', @contents); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub fatal { |
77
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
78
|
0
|
|
|
|
|
0
|
my $target = shift; |
79
|
0
|
|
|
|
|
0
|
my @contents = @_; |
80
|
0
|
|
|
|
|
0
|
$self->_out($target, 'fatal', @contents); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub _out { |
84
|
2
|
|
|
2
|
|
3
|
my $self = shift; |
85
|
2
|
|
|
|
|
2
|
my $target = shift; |
86
|
2
|
|
|
|
|
2
|
my $level = shift; |
87
|
2
|
|
|
|
|
2
|
my @contents = @_; |
88
|
|
|
|
|
|
|
|
89
|
2
|
|
|
|
|
5
|
my $path = $PATH->{$target}; |
90
|
|
|
|
|
|
|
|
91
|
2
|
50
|
|
|
|
5
|
if (!$path) { |
92
|
0
|
|
|
|
|
0
|
die "Undefined target: $target. path: $path"; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
2
|
|
|
|
|
4
|
my $log = join("\t", @contents); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# remove line break for contents |
98
|
|
|
|
|
|
|
# there should always be one line per output |
99
|
2
|
|
|
|
|
2
|
$log =~ s/\n//g; |
100
|
|
|
|
|
|
|
|
101
|
2
|
|
|
|
|
6
|
$self->_append($path, $self->_format($level, $log)); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub _append { |
105
|
2
|
|
|
2
|
|
3
|
my $self = shift; |
106
|
2
|
|
|
|
|
2
|
my ($path, $log) = @_; |
107
|
|
|
|
|
|
|
|
108
|
2
|
|
|
|
|
4
|
$path .= "." . _get_local_date(); |
109
|
2
|
50
|
|
|
|
138
|
open HANDLE, ">>", $path or die "Can not open file $path"; |
110
|
|
|
|
|
|
|
|
111
|
2
|
|
|
|
|
6
|
flock HANDLE, LOCK_EX; |
112
|
2
|
50
|
|
|
|
6
|
HANDLE->print(encode('UTF-8', $log)) or die "Can't write to log: $!"; |
113
|
2
|
|
|
|
|
100
|
flock HANDLE, LOCK_UN; |
114
|
|
|
|
|
|
|
|
115
|
2
|
|
|
|
|
15
|
close HANDLE; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub _format { |
119
|
2
|
|
|
2
|
|
3
|
my $self = shift; |
120
|
2
|
|
|
|
|
39
|
return'[' . localtime . '] [' . shift . '] ' . shift . "\n"; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub _get_local_date { |
124
|
2
|
|
|
2
|
|
16
|
my ($second, $minute, $hour, $day, $month, $year, $weekday, $yesterday, $is_dst) = localtime; |
125
|
2
|
|
|
|
|
13
|
return sprintf("%04d%02d%02d", $year + 1900, $month + 1, $day); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
1; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
__END__ |