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