line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Squatting::With::Log; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
67161
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
61
|
|
4
|
1
|
|
|
1
|
|
5
|
no strict 'refs'; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
26
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
33
|
|
6
|
1
|
|
|
1
|
|
1300
|
use Squatting::H; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
117
|
|
7
|
1
|
|
|
1
|
|
1244
|
use IO::All; |
|
1
|
|
|
|
|
35062
|
|
|
1
|
|
|
|
|
155
|
|
8
|
1
|
|
|
1
|
|
126
|
use Clone 'clone'; |
|
1
|
|
|
|
|
13
|
|
|
1
|
|
|
|
|
820
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub timestamp { |
11
|
0
|
|
|
0
|
0
|
|
my ($sec, $min, $hour, $mday, $mon, $year) = localtime; |
12
|
0
|
|
|
|
|
|
sprintf( |
13
|
|
|
|
|
|
|
'%d-%02d-%02dT%02d:%02d:%02d', |
14
|
|
|
|
|
|
|
$year + 1900, $mon + 1, $mday, |
15
|
|
|
|
|
|
|
$hour, $min, $sec |
16
|
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $Log = Squatting::H->new({ |
20
|
|
|
|
|
|
|
_path => '=', |
21
|
|
|
|
|
|
|
_levels => {}, |
22
|
|
|
|
|
|
|
enable => sub { |
23
|
|
|
|
|
|
|
my $self = shift; |
24
|
|
|
|
|
|
|
$self->{_levels}->{$_} = 1 for (@_); |
25
|
|
|
|
|
|
|
keys %{$self->{_levels}}; |
26
|
|
|
|
|
|
|
}, |
27
|
|
|
|
|
|
|
disable => sub { |
28
|
|
|
|
|
|
|
my $self = shift; |
29
|
|
|
|
|
|
|
delete($self->{_levels}->{$_}) for (@_); |
30
|
|
|
|
|
|
|
keys %{$self->{_levels}}; |
31
|
|
|
|
|
|
|
}, |
32
|
|
|
|
|
|
|
}); |
33
|
|
|
|
|
|
|
$Log->{levels} = sub { |
34
|
|
|
|
|
|
|
keys %{$_[0]->{_levels}}; |
35
|
|
|
|
|
|
|
}; |
36
|
|
|
|
|
|
|
for my $level (qw(debug info warn error fatal)) { |
37
|
|
|
|
|
|
|
$Log->{$level} = sub { |
38
|
|
|
|
|
|
|
my ($self, @messages) = @_; |
39
|
|
|
|
|
|
|
my $is_level = "is_$level"; |
40
|
|
|
|
|
|
|
return unless $self->$is_level; |
41
|
|
|
|
|
|
|
for (@messages) { |
42
|
|
|
|
|
|
|
sprintf('%-5s %s ! %s'."\n", $level, timestamp, $_) >> io($self->{_path}); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
}; |
45
|
|
|
|
|
|
|
$Log->{"is_$level"} = sub { |
46
|
|
|
|
|
|
|
$_[0]->{_levels}->{$level}; |
47
|
|
|
|
|
|
|
}; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# every app gets its own log object in %log |
51
|
|
|
|
|
|
|
our %log; |
52
|
|
|
|
|
|
|
my $log_object = sub { |
53
|
|
|
|
|
|
|
my ($app) = @_; |
54
|
|
|
|
|
|
|
my $config = \%{$app.'::CONFIG'}; |
55
|
|
|
|
|
|
|
$log{$app} ||= do { |
56
|
|
|
|
|
|
|
my $path = $config->{'with.log.path'} || '='; # (default STDERR) |
57
|
|
|
|
|
|
|
my $level = $config->{'with.log.levels'} || 'debug,info,warn,error,fatal'; |
58
|
|
|
|
|
|
|
my $levels = +{ map { $_ => 1 } split(/\s*,\s*/, $level) }; |
59
|
|
|
|
|
|
|
$Log->clone({ path => $path, levels => $levels }); |
60
|
|
|
|
|
|
|
}; |
61
|
|
|
|
|
|
|
}; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub service { |
64
|
0
|
|
|
0
|
0
|
|
my ($app, $c, @args) = @_; |
65
|
0
|
|
0
|
|
|
|
$c->log ||= $log_object->($app); |
66
|
0
|
|
|
|
|
|
$app->next::method($c, @args); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
1; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 NAME |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Squatting::With::Log - a simple error log for Squatting apps |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 SYNOPSIS |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Adding simple logging to your Squatting app: |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
use App 'With::Log', 'On::CGI'; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
This will let log from within your controllers: |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
C( |
84
|
|
|
|
|
|
|
Day => [ '/(\d+)/(\d+)/(\d+)' ], |
85
|
|
|
|
|
|
|
get => sub { |
86
|
|
|
|
|
|
|
my ($self, $year, $month, $day) = @_; |
87
|
|
|
|
|
|
|
my $log = $self->log; |
88
|
|
|
|
|
|
|
$log->debug(" year: $year"); |
89
|
|
|
|
|
|
|
$log->info ("month: $month"); |
90
|
|
|
|
|
|
|
$log->warn (" day: $day"); |
91
|
|
|
|
|
|
|
# you also get $log->error and $log->fatal |
92
|
|
|
|
|
|
|
$self->render('day'); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
) |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 DESCRIPTION |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Squatting::With::Log provides a simple logging object that can be used from |
99
|
|
|
|
|
|
|
within your controllers to send messages to either a log file or STDERR for |
100
|
|
|
|
|
|
|
informational purposes. Typically, these messages would be useful during |
101
|
|
|
|
|
|
|
development and debugging but would be disabled for production use. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
To use this module, pass the string C<'With::Log'> to the C |
104
|
|
|
|
|
|
|
loads your Squatting app. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 CONFIGURATION |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Squatting apps may set the following values in their C<%CONFIG> hash to control |
109
|
|
|
|
|
|
|
the behavior of this module. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=over 4 |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=item with.log.path |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
This should be a string that specifies the full path to where you want the |
116
|
|
|
|
|
|
|
logs to be sent. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
B: |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
$CONFIG{'with.log.path'} = "/tmp/error_log"; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item with.log.levels |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
This should be a comma-separated string that lists all the log levels you |
125
|
|
|
|
|
|
|
want to enable. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
B: Only output messages with a log level of C or C. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
$CONFIG{'with.log.levels'} = "error,fatal"; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=back |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 API |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head2 Object Construction |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head3 $log = Squatting::Log->new(\%config) |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head2 Configuration |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head3 $log->enable(@levels) |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
This method enables the list of log levels you send it. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head3 $log->disable(@levels) |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
This method disables the list of log levels you send it. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 Introspection |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head3 $log->is_debug |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head3 $log->is_info |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head3 $log->is_warn |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head3 $log->is_error |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head3 $log->is_fatal |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
These methods return true if their respective log levels are enabled. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 Logging |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head3 $log->debug(@messages) |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head3 $log->info(@messages) |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head3 $log->warn(@messages) |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head3 $log->error(@messages) |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head3 $log->fatal(@messages) |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
These methods output the list of log messages you send it using the |
176
|
|
|
|
|
|
|
specified log level. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 SEE ALSO |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
L - The Squatting::Log API is the same as the Catalyst::Log API. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=cut |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# Local Variables: *** |
185
|
|
|
|
|
|
|
# mode: cperl *** |
186
|
|
|
|
|
|
|
# indent-tabs-mode: nil *** |
187
|
|
|
|
|
|
|
# cperl-close-paren-offset: -2 *** |
188
|
|
|
|
|
|
|
# cperl-continued-statement-offset: 2 *** |
189
|
|
|
|
|
|
|
# cperl-indent-level: 2 *** |
190
|
|
|
|
|
|
|
# cperl-indent-parens-as-block: t *** |
191
|
|
|
|
|
|
|
# cperl-tab-always-indent: nil *** |
192
|
|
|
|
|
|
|
# End: *** |
193
|
|
|
|
|
|
|
# vim:tabstop=8 softtabstop=2 shiftwidth=2 shiftround expandtab |