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