Commit | Line | Data |
fc7ec1d9 |
1 | package Catalyst::Log; |
2 | |
e5ecd5bc |
3 | use Moose; |
f63c03e4 |
4 | use Data::Dump; |
9b2bc37b |
5 | |
a6433a05 |
6 | our %LEVELS = (); |
7 | |
3cdcf968 |
8 | has level => (is => 'rw'); |
5fb12dbb |
9 | has _body => (is => 'rw'); |
3cdcf968 |
10 | has abort => (is => 'rw'); |
11 | |
2f9e6321 |
12 | { |
a6433a05 |
13 | my @levels = qw[ debug info warn error fatal ]; |
846772b7 |
14 | |
4090e3bb |
15 | my $meta = __PACKAGE__->meta; |
846772b7 |
16 | for ( my $i = 0 ; $i < @levels ; $i++ ) { |
17 | |
18 | my $name = $levels[$i]; |
19 | my $level = 1 << $i; |
20 | |
a6433a05 |
21 | $LEVELS{$name} = $level; |
22 | |
4090e3bb |
23 | $meta->add_method($name, sub { |
846772b7 |
24 | my $self = shift; |
25 | |
3cdcf968 |
26 | if ( $self->level & $level ) { |
846772b7 |
27 | $self->_log( $name, @_ ); |
28 | } |
4090e3bb |
29 | }); |
846772b7 |
30 | |
4090e3bb |
31 | $meta->add_method("is_$name", sub { |
846772b7 |
32 | my $self = shift; |
3cdcf968 |
33 | return $self->level & $level; |
4090e3bb |
34 | });; |
846772b7 |
35 | } |
a6433a05 |
36 | } |
37 | |
4090e3bb |
38 | around new => sub { |
39 | my $orig = shift; |
a6433a05 |
40 | my $class = shift; |
4090e3bb |
41 | my $self = $class->$orig; |
a6433a05 |
42 | $self->levels( scalar(@_) ? @_ : keys %LEVELS ); |
43 | return $self; |
4090e3bb |
44 | }; |
a6433a05 |
45 | |
46 | sub levels { |
47 | my ( $self, @levels ) = @_; |
48 | $self->level(0); |
49 | $self->enable(@levels); |
50 | } |
51 | |
52 | sub enable { |
53 | my ( $self, @levels ) = @_; |
3cdcf968 |
54 | my $level = $self->level; |
55 | for(map { $LEVELS{$_} } @levels){ |
56 | $level |= $_; |
57 | } |
58 | $self->level($level); |
a6433a05 |
59 | } |
846772b7 |
60 | |
a6433a05 |
61 | sub disable { |
62 | my ( $self, @levels ) = @_; |
3cdcf968 |
63 | my $level = $self->level; |
64 | for(map { $LEVELS{$_} } @levels){ |
65 | $level &= ~$_; |
66 | } |
67 | $self->level($level); |
846772b7 |
68 | } |
69 | |
a6433a05 |
70 | sub _dump { |
846772b7 |
71 | my $self = shift; |
f63c03e4 |
72 | $self->info( Data::Dump::dump(@_) ); |
846772b7 |
73 | } |
74 | |
75 | sub _log { |
76 | my $self = shift; |
77 | my $level = shift; |
846772b7 |
78 | my $message = join( "\n", @_ ); |
1cf0345b |
79 | $message .= "\n" unless $message =~ /\n$/; |
3cdcf968 |
80 | my $body = $self->_body; |
81 | $body .= sprintf( "[%s] %s", $level, $message ); |
82 | $self->_body($body); |
4be535b1 |
83 | } |
84 | |
85 | sub _flush { |
b5ecfcf0 |
86 | my $self = shift; |
3cdcf968 |
87 | if ( $self->abort || !$self->_body ) { |
b5ecfcf0 |
88 | $self->abort(undef); |
89 | } |
90 | else { |
3cdcf968 |
91 | $self->_send_to_log( $self->_body ); |
4be535b1 |
92 | } |
3cdcf968 |
93 | $self->_body(undef); |
846772b7 |
94 | } |
95 | |
adee716c |
96 | sub _send_to_log { |
97 | my $self = shift; |
98 | print STDERR @_; |
99 | } |
100 | |
6680c772 |
101 | no Moose; |
102 | __PACKAGE__->meta->make_immutable(); |
103 | |
846772b7 |
104 | 1; |
105 | |
106 | __END__ |
fc7ec1d9 |
107 | |
108 | =head1 NAME |
109 | |
110 | Catalyst::Log - Catalyst Log Class |
111 | |
112 | =head1 SYNOPSIS |
113 | |
82d2fcbe |
114 | $log = $c->log; |
846772b7 |
115 | $log->debug($message); |
116 | $log->info($message); |
117 | $log->warn($message); |
118 | $log->error($message); |
119 | $log->fatal($message); |
846772b7 |
120 | |
f4dbb0d9 |
121 | if ( $log->is_debug ) { |
846772b7 |
122 | # expensive debugging |
123 | } |
82d2fcbe |
124 | |
a6433a05 |
125 | |
fc7ec1d9 |
126 | See L<Catalyst>. |
127 | |
128 | =head1 DESCRIPTION |
129 | |
adee716c |
130 | This module provides the default, simple logging functionality for Catalyst. |
131 | If you want something different set C<< $c->log >> in your application module, |
132 | e.g.: |
82d2fcbe |
133 | |
134 | $c->log( MyLogger->new ); |
135 | |
136 | Your logging object is expected to provide the interface described here. |
397be447 |
137 | Good alternatives to consider are Log::Log4Perl and Log::Dispatch. |
138 | |
139 | If you want to be able to log arbitrary warnings, you can do something along |
140 | the lines of |
141 | |
142 | $SIG{__WARN__} = sub { MyApp->log->warn(@_); }; |
143 | |
144 | however this is (a) global, (b) hairy and (c) may have unexpected side effects. |
145 | Don't say we didn't warn you. |
82d2fcbe |
146 | |
a6433a05 |
147 | =head1 LOG LEVELS |
148 | |
b5ecfcf0 |
149 | =head2 debug |
a6433a05 |
150 | |
151 | $log->is_debug; |
152 | $log->debug($message); |
153 | |
b5ecfcf0 |
154 | =head2 info |
a6433a05 |
155 | |
156 | $log->is_info; |
157 | $log->info($message); |
158 | |
b5ecfcf0 |
159 | =head2 warn |
a6433a05 |
160 | |
161 | $log->is_warn; |
162 | $log->warn($message); |
163 | |
b5ecfcf0 |
164 | =head2 error |
a6433a05 |
165 | |
166 | $log->is_error; |
167 | $log->error($message); |
168 | |
b5ecfcf0 |
169 | =head2 fatal |
a6433a05 |
170 | |
171 | $log->is_fatal; |
172 | $log->fatal($message); |
173 | |
b22c6668 |
174 | =head1 METHODS |
fc7ec1d9 |
175 | |
b5ecfcf0 |
176 | =head2 new |
a6433a05 |
177 | |
26e73131 |
178 | Constructor. Defaults to enable all levels unless levels are provided in |
a6433a05 |
179 | arguments. |
180 | |
181 | $log = Catalyst::Log->new; |
26e73131 |
182 | $log = Catalyst::Log->new( 'warn', 'error' ); |
a6433a05 |
183 | |
3cdcf968 |
184 | =head2 level |
185 | |
186 | Contains a bitmask of the currently set log levels. |
187 | |
b5ecfcf0 |
188 | =head2 levels |
a6433a05 |
189 | |
190 | Set log levels |
b22c6668 |
191 | |
a6433a05 |
192 | $log->levels( 'warn', 'error', 'fatal' ); |
fc7ec1d9 |
193 | |
b5ecfcf0 |
194 | =head2 enable |
fc7ec1d9 |
195 | |
a6433a05 |
196 | Enable log levels |
fc7ec1d9 |
197 | |
a6433a05 |
198 | $log->enable( 'warn', 'error' ); |
fc7ec1d9 |
199 | |
b5ecfcf0 |
200 | =head2 disable |
fc7ec1d9 |
201 | |
a6433a05 |
202 | Disable log levels |
fc7ec1d9 |
203 | |
a6433a05 |
204 | $log->disable( 'warn', 'error' ); |
fc7ec1d9 |
205 | |
b5ecfcf0 |
206 | =head2 is_debug |
a2f2cde9 |
207 | |
b5ecfcf0 |
208 | =head2 is_error |
a2f2cde9 |
209 | |
b5ecfcf0 |
210 | =head2 is_fatal |
a2f2cde9 |
211 | |
b5ecfcf0 |
212 | =head2 is_info |
a2f2cde9 |
213 | |
b5ecfcf0 |
214 | =head2 is_warn |
49ba64d7 |
215 | |
216 | Is the log level active? |
217 | |
b5ecfcf0 |
218 | =head2 abort |
4be535b1 |
219 | |
ac5c933b |
220 | Should Catalyst emit logs for this request? Will be reset at the end of |
221 | each request. |
4be535b1 |
222 | |
223 | *NOTE* This method is not compatible with other log apis, so if you plan |
224 | to use Log4Perl or another logger, you should call it like this: |
225 | |
226 | $c->log->abort(1) if $c->log->can('abort'); |
227 | |
adee716c |
228 | =head2 _send_to_log |
229 | |
230 | $log->_send_to_log( @messages ); |
231 | |
232 | This protected method is what actually sends the log information to STDERR. |
233 | You may subclass this module and override this method to get finer control |
234 | over the log output. |
235 | |
3cdcf968 |
236 | =head2 meta |
237 | |
fc7ec1d9 |
238 | =head1 SEE ALSO |
239 | |
240 | L<Catalyst>. |
241 | |
242 | =head1 AUTHOR |
243 | |
244 | Sebastian Riedel, C<sri@cpan.org> |
61b1e958 |
245 | Marcus Ramberg, C<mramberg@cpan.org> |
a6433a05 |
246 | Christian Hansen, C<ch@ngmedia.com> |
fc7ec1d9 |
247 | |
248 | =head1 COPYRIGHT |
249 | |
a6433a05 |
250 | This program is free software, you can redistribute it and/or modify |
61b1e958 |
251 | it under the same terms as Perl itself. |
fc7ec1d9 |
252 | |
253 | =cut |
254 | |
e5ecd5bc |
255 | __PACKAGE__->meta->make_immutable; |
256 | |
fc7ec1d9 |
257 | 1; |