line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catalyst::Plugin::File::RotateLogs; |
2
|
2
|
|
|
2
|
|
5298
|
use feature qw(switch); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
209
|
|
3
|
2
|
|
|
2
|
|
14
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
48
|
|
4
|
2
|
|
|
2
|
|
14
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
85
|
|
5
|
2
|
|
|
2
|
|
307
|
use MRO::Compat; |
|
2
|
|
|
|
|
1257
|
|
|
2
|
|
|
|
|
62
|
|
6
|
2
|
|
|
2
|
|
246
|
use Path::Class (); |
|
2
|
|
|
|
|
35807
|
|
|
2
|
|
|
|
|
680
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = "0.06"; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub setup { |
11
|
1
|
|
|
1
|
0
|
457036
|
my $c = shift; |
12
|
1
|
|
50
|
|
|
9
|
my $mode_prefix = $ENV{PLACK_ENV} // 'development'; |
13
|
1
|
|
|
|
|
3
|
my $default_autodump = 0; |
14
|
1
|
|
|
|
|
3
|
my $default_color = 0; |
15
|
1
|
50
|
|
|
|
5
|
if ($mode_prefix eq 'development') { |
16
|
1
|
|
|
|
|
2
|
$default_autodump = 1; |
17
|
1
|
|
|
|
|
3
|
$default_color = 1; |
18
|
|
|
|
|
|
|
} |
19
|
1
|
|
|
|
|
9
|
my $home = $c->config->{home}; |
20
|
1
|
|
50
|
|
|
193
|
my $config = $c->config->{'File::RotateLogs'} || { |
21
|
|
|
|
|
|
|
logfile => Path::Class::file($home, "root", "${mode_prefix}.error_log.%Y%m%d%H")->absolute->stringify, |
22
|
|
|
|
|
|
|
linkname => Path::Class::file($home, "root", "${mode_prefix}.error_log")->absolute->stringify, |
23
|
|
|
|
|
|
|
rotationtime => 86400, #default 1day |
24
|
|
|
|
|
|
|
maxage => 86400 * 3, #3day |
25
|
|
|
|
|
|
|
autodump => $default_autodump, |
26
|
|
|
|
|
|
|
color => $default_color, |
27
|
|
|
|
|
|
|
}; |
28
|
1
|
|
|
|
|
873
|
$config->{maxage} = int eval($config->{maxage}); |
29
|
1
|
|
|
|
|
13
|
$c->log((__PACKAGE__ . '::Backend')->new($config)); |
30
|
1
|
|
|
|
|
79
|
return $c->maybe::next::method(@_); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
package Catalyst::Plugin::File::RotateLogs::Backend; |
34
|
2
|
|
|
2
|
|
527
|
use Moose; |
|
2
|
|
|
|
|
375581
|
|
|
2
|
|
|
|
|
17
|
|
35
|
2
|
|
|
2
|
|
17173
|
use Time::Piece; |
|
2
|
|
|
|
|
17555
|
|
|
2
|
|
|
|
|
10
|
|
36
|
2
|
|
|
2
|
|
902
|
use File::RotateLogs; |
|
2
|
|
|
|
|
60848
|
|
|
2
|
|
|
|
|
70
|
|
37
|
2
|
|
|
2
|
|
935
|
use Data::Dumper; |
|
2
|
|
|
|
|
10828
|
|
|
2
|
|
|
|
|
144
|
|
38
|
2
|
|
|
2
|
|
808
|
use Term::ANSIColor; |
|
2
|
|
|
|
|
16072
|
|
|
2
|
|
|
|
|
199
|
|
39
|
|
|
|
|
|
|
|
40
|
2
|
|
|
2
|
|
14
|
BEGIN { extends 'Catalyst::Log' } |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $ROTATE_LOGS; |
43
|
|
|
|
|
|
|
my $CALLER_DEPTH = 1; |
44
|
|
|
|
|
|
|
my $AUTODUMP = 0; |
45
|
|
|
|
|
|
|
my $COLOR = 0; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub new { |
48
|
1
|
|
|
1
|
1
|
22
|
my $class = shift; |
49
|
1
|
|
|
|
|
2
|
my $config = shift; |
50
|
|
|
|
|
|
|
|
51
|
1
|
|
50
|
|
|
9
|
$AUTODUMP = $config->{autodump} //= 0; |
52
|
1
|
|
50
|
|
|
7
|
$COLOR = $config->{color} //= 0; |
53
|
1
|
|
|
|
|
4
|
delete $config->{autodump}; |
54
|
1
|
|
|
|
|
2
|
delete $config->{color}; |
55
|
|
|
|
|
|
|
|
56
|
1
|
|
|
|
|
10
|
my $self = $class->next::method(); |
57
|
1
|
|
|
|
|
2180
|
$ROTATE_LOGS = File::RotateLogs->new($config); |
58
|
|
|
|
|
|
|
|
59
|
1
|
|
|
|
|
172
|
return $self; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
{ |
63
|
|
|
|
|
|
|
foreach my $handler (qw/debug info warn error fatal/) { |
64
|
|
|
|
|
|
|
override $handler => sub { |
65
|
|
|
|
|
|
|
my ($self, $message) = @_; |
66
|
|
|
|
|
|
|
if ($AUTODUMP && ref($message) ) { |
67
|
|
|
|
|
|
|
local $Data::Dumper::Terse = 1; |
68
|
|
|
|
|
|
|
local $Data::Dumper::Indent = 0; |
69
|
|
|
|
|
|
|
local $Data::Dumper::Sortkeys = 1; |
70
|
|
|
|
|
|
|
$message = Data::Dumper::Dumper($message); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
my ($package, $file, $line) = caller($CALLER_DEPTH); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
my $datetime = localtime->datetime; |
76
|
|
|
|
|
|
|
my $uc_handler = uc $handler; |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
if ($COLOR) { |
79
|
|
|
|
|
|
|
my $level_color; |
80
|
|
|
|
|
|
|
given ($uc_handler) { |
81
|
|
|
|
|
|
|
when (/DEBUG/) { $level_color = 'magenta'} |
82
|
|
|
|
|
|
|
when (/INFO/) { $level_color = 'cyan' } |
83
|
|
|
|
|
|
|
when (/WARN/) { $level_color = 'yellow' } |
84
|
|
|
|
|
|
|
default { $level_color = 'red' } |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
$datetime = colored(['clear yellow'], $datetime), |
87
|
|
|
|
|
|
|
$uc_handler = colored(["clear $level_color"], $uc_handler), |
88
|
|
|
|
|
|
|
$package = colored(['clear white'], $package), |
89
|
|
|
|
|
|
|
$message = colored(['clear green'], $message), |
90
|
|
|
|
|
|
|
$file = colored(['dark white'], $file), |
91
|
|
|
|
|
|
|
$line = colored(['dark white'], $line) |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
$ROTATE_LOGS->print(sprintf(qq{%s: [%s] [%s] %s %s %s\n}, |
95
|
|
|
|
|
|
|
$datetime, $uc_handler, $package, $message, $file, $line |
96
|
|
|
|
|
|
|
)); |
97
|
|
|
|
|
|
|
}; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |
103
|
|
|
|
|
|
|
__END__ |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=pod |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 NAME |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
Catalyst::Plugin::File::RotateLogs - Catalyst Plugin for File::RotateLogs |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 SYNOPSIS |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# plugin is loaded |
114
|
|
|
|
|
|
|
use Catalyst qw/ |
115
|
|
|
|
|
|
|
ConfigLoader |
116
|
|
|
|
|
|
|
Static::Simple |
117
|
|
|
|
|
|
|
File::RotateLogs |
118
|
|
|
|
|
|
|
/; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
$c->log->info("hello catalyst"); |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 DESCRIPTION |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
This module allows you to initialize File::RotateLogs within the application's configuration. File::RotateLogs is utility for file logger and very simple logfile rotation. I wanted easier catalyst log rotation. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 Configuration |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# Catalyst configuration file (e. g. in YAML format): |
129
|
|
|
|
|
|
|
File::RotateLogs: |
130
|
|
|
|
|
|
|
logfile: '/[absolute path]/root/error.log.%Y%m%d%H' |
131
|
|
|
|
|
|
|
linkname: '/[absolute path]/root/error.log' |
132
|
|
|
|
|
|
|
rotationtime: 86400 |
133
|
|
|
|
|
|
|
maxage: 86400 * 3 |
134
|
|
|
|
|
|
|
autodump: 0 |
135
|
|
|
|
|
|
|
color: 0 |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 SEE ALSO |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=over 2 |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item L<Catalyst::Log> |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=item L<File::RotateLogs> |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=back |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head1 LICENSE |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
151
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 AUTHOR |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
masakyst E<lt>masakyst.public@gmail.comE<gt> |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=cut |