Commit | Line | Data |
fc7ec1d9 |
1 | package Catalyst::Log; |
2 | |
3 | use strict; |
4 | use base 'Class::Accessor::Fast'; |
9b2bc37b |
5 | use Data::Dumper; |
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; |
65 | local $Data::Dumper::Terse = 1; |
475616f8 |
66 | $self->info( Dumper( @_ ) ); |
846772b7 |
67 | } |
68 | |
69 | sub _log { |
70 | my $self = shift; |
71 | my $level = shift; |
72 | my $time = localtime(time); |
73 | my $message = join( "\n", @_ ); |
b5ecfcf0 |
74 | $self->{body} .= |
75 | sprintf( "[%s] [catalyst] [%s] %s\n", $time, $level, $message ); |
4be535b1 |
76 | } |
77 | |
78 | sub _flush { |
b5ecfcf0 |
79 | my $self = shift; |
80 | if ( $self->abort || !$self->body ) { |
81 | $self->abort(undef); |
82 | } |
83 | else { |
adee716c |
84 | $self->_send_to_log( $self->body ); |
4be535b1 |
85 | } |
86 | $self->body(undef); |
846772b7 |
87 | } |
88 | |
adee716c |
89 | sub _send_to_log { |
90 | my $self = shift; |
91 | print STDERR @_; |
92 | } |
93 | |
846772b7 |
94 | 1; |
95 | |
96 | __END__ |
fc7ec1d9 |
97 | |
98 | =head1 NAME |
99 | |
100 | Catalyst::Log - Catalyst Log Class |
101 | |
102 | =head1 SYNOPSIS |
103 | |
82d2fcbe |
104 | $log = $c->log; |
846772b7 |
105 | $log->debug($message); |
106 | $log->info($message); |
107 | $log->warn($message); |
108 | $log->error($message); |
109 | $log->fatal($message); |
846772b7 |
110 | |
f4dbb0d9 |
111 | if ( $log->is_debug ) { |
846772b7 |
112 | # expensive debugging |
113 | } |
82d2fcbe |
114 | |
a6433a05 |
115 | |
fc7ec1d9 |
116 | See L<Catalyst>. |
117 | |
118 | =head1 DESCRIPTION |
119 | |
adee716c |
120 | This module provides the default, simple logging functionality for Catalyst. |
121 | If you want something different set C<< $c->log >> in your application module, |
122 | e.g.: |
82d2fcbe |
123 | |
124 | $c->log( MyLogger->new ); |
125 | |
126 | Your logging object is expected to provide the interface described here. |
127 | |
a6433a05 |
128 | =head1 LOG LEVELS |
129 | |
b5ecfcf0 |
130 | =head2 debug |
a6433a05 |
131 | |
132 | $log->is_debug; |
133 | $log->debug($message); |
134 | |
b5ecfcf0 |
135 | =head2 info |
a6433a05 |
136 | |
137 | $log->is_info; |
138 | $log->info($message); |
139 | |
b5ecfcf0 |
140 | =head2 warn |
a6433a05 |
141 | |
142 | $log->is_warn; |
143 | $log->warn($message); |
144 | |
b5ecfcf0 |
145 | =head2 error |
a6433a05 |
146 | |
147 | $log->is_error; |
148 | $log->error($message); |
149 | |
b5ecfcf0 |
150 | =head2 fatal |
a6433a05 |
151 | |
152 | $log->is_fatal; |
153 | $log->fatal($message); |
154 | |
b22c6668 |
155 | =head1 METHODS |
fc7ec1d9 |
156 | |
b5ecfcf0 |
157 | =head2 new |
a6433a05 |
158 | |
26e73131 |
159 | Constructor. Defaults to enable all levels unless levels are provided in |
a6433a05 |
160 | arguments. |
161 | |
162 | $log = Catalyst::Log->new; |
26e73131 |
163 | $log = Catalyst::Log->new( 'warn', 'error' ); |
a6433a05 |
164 | |
b5ecfcf0 |
165 | =head2 levels |
a6433a05 |
166 | |
167 | Set log levels |
b22c6668 |
168 | |
a6433a05 |
169 | $log->levels( 'warn', 'error', 'fatal' ); |
fc7ec1d9 |
170 | |
b5ecfcf0 |
171 | =head2 enable |
fc7ec1d9 |
172 | |
a6433a05 |
173 | Enable log levels |
fc7ec1d9 |
174 | |
a6433a05 |
175 | $log->enable( 'warn', 'error' ); |
fc7ec1d9 |
176 | |
b5ecfcf0 |
177 | =head2 disable |
fc7ec1d9 |
178 | |
a6433a05 |
179 | Disable log levels |
fc7ec1d9 |
180 | |
a6433a05 |
181 | $log->disable( 'warn', 'error' ); |
fc7ec1d9 |
182 | |
b5ecfcf0 |
183 | =head2 is_debug |
a2f2cde9 |
184 | |
b5ecfcf0 |
185 | =head2 is_error |
a2f2cde9 |
186 | |
b5ecfcf0 |
187 | =head2 is_fatal |
a2f2cde9 |
188 | |
b5ecfcf0 |
189 | =head2 is_info |
a2f2cde9 |
190 | |
b5ecfcf0 |
191 | =head2 is_warn |
49ba64d7 |
192 | |
193 | Is the log level active? |
194 | |
b5ecfcf0 |
195 | =head2 abort |
4be535b1 |
196 | |
197 | Should Catalyst emit logs for this request? Will be reset at the end of |
198 | each request. |
199 | |
200 | *NOTE* This method is not compatible with other log apis, so if you plan |
201 | to use Log4Perl or another logger, you should call it like this: |
202 | |
203 | $c->log->abort(1) if $c->log->can('abort'); |
204 | |
adee716c |
205 | =head2 _send_to_log |
206 | |
207 | $log->_send_to_log( @messages ); |
208 | |
209 | This protected method is what actually sends the log information to STDERR. |
210 | You may subclass this module and override this method to get finer control |
211 | over the log output. |
212 | |
fc7ec1d9 |
213 | =head1 SEE ALSO |
214 | |
215 | L<Catalyst>. |
216 | |
217 | =head1 AUTHOR |
218 | |
219 | Sebastian Riedel, C<sri@cpan.org> |
61b1e958 |
220 | Marcus Ramberg, C<mramberg@cpan.org> |
a6433a05 |
221 | Christian Hansen, C<ch@ngmedia.com> |
fc7ec1d9 |
222 | |
223 | =head1 COPYRIGHT |
224 | |
a6433a05 |
225 | This program is free software, you can redistribute it and/or modify |
61b1e958 |
226 | it under the same terms as Perl itself. |
fc7ec1d9 |
227 | |
228 | =cut |
229 | |
230 | 1; |