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