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