mro compat stuff
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Log.pm
CommitLineData
fc7ec1d9 1package Catalyst::Log;
2
6f1f968a 3use MRO::Compat;
4use mro 'c3';
e5ecd5bc 5use Moose;
f63c03e4 6use Data::Dump;
9b2bc37b 7
a6433a05 8our %LEVELS = ();
9
3cdcf968 10has level => (is => 'rw');
5fb12dbb 11has _body => (is => 'rw');
3cdcf968 12has abort => (is => 'rw');
13
2f9e6321 14{
a6433a05 15 my @levels = qw[ debug info warn error fatal ];
846772b7 16
17 for ( my $i = 0 ; $i < @levels ; $i++ ) {
18
19 my $name = $levels[$i];
20 my $level = 1 << $i;
21
a6433a05 22 $LEVELS{$name} = $level;
23
24 no strict 'refs';
25
846772b7 26 *{$name} = sub {
27 my $self = shift;
28
3cdcf968 29 if ( $self->level & $level ) {
846772b7 30 $self->_log( $name, @_ );
31 }
32 };
33
34 *{"is_$name"} = sub {
35 my $self = shift;
3cdcf968 36 return $self->level & $level;
846772b7 37 };
38 }
a6433a05 39}
40
0fc2d522 41sub new {
a6433a05 42 my $class = shift;
0fc2d522 43 my $self = $class->next::method;
a6433a05 44 $self->levels( scalar(@_) ? @_ : keys %LEVELS );
45 return $self;
0fc2d522 46}
a6433a05 47
48sub levels {
49 my ( $self, @levels ) = @_;
50 $self->level(0);
51 $self->enable(@levels);
52}
53
54sub 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 63sub 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 72sub _dump {
846772b7 73 my $self = shift;
f63c03e4 74 $self->info( Data::Dump::dump(@_) );
846772b7 75}
76
77sub _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
87sub _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 98sub _send_to_log {
99 my $self = shift;
100 print STDERR @_;
101}
102
6680c772 103no Moose;
104__PACKAGE__->meta->make_immutable();
105
846772b7 1061;
107
108__END__
fc7ec1d9 109
110=head1 NAME
111
112Catalyst::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 128See L<Catalyst>.
129
130=head1 DESCRIPTION
131
adee716c 132This module provides the default, simple logging functionality for Catalyst.
133If you want something different set C<< $c->log >> in your application module,
134e.g.:
82d2fcbe 135
136 $c->log( MyLogger->new );
137
138Your logging object is expected to provide the interface described here.
397be447 139Good alternatives to consider are Log::Log4Perl and Log::Dispatch.
140
141If you want to be able to log arbitrary warnings, you can do something along
142the lines of
143
144 $SIG{__WARN__} = sub { MyApp->log->warn(@_); };
145
146however this is (a) global, (b) hairy and (c) may have unexpected side effects.
147Don'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 180Constructor. Defaults to enable all levels unless levels are provided in
a6433a05 181arguments.
182
183 $log = Catalyst::Log->new;
26e73131 184 $log = Catalyst::Log->new( 'warn', 'error' );
a6433a05 185
3cdcf968 186=head2 level
187
188Contains a bitmask of the currently set log levels.
189
b5ecfcf0 190=head2 levels
a6433a05 191
192Set log levels
b22c6668 193
a6433a05 194 $log->levels( 'warn', 'error', 'fatal' );
fc7ec1d9 195
b5ecfcf0 196=head2 enable
fc7ec1d9 197
a6433a05 198Enable log levels
fc7ec1d9 199
a6433a05 200 $log->enable( 'warn', 'error' );
fc7ec1d9 201
b5ecfcf0 202=head2 disable
fc7ec1d9 203
a6433a05 204Disable 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
218Is the log level active?
219
b5ecfcf0 220=head2 abort
4be535b1 221
ac5c933b 222Should Catalyst emit logs for this request? Will be reset at the end of
223each request.
4be535b1 224
225*NOTE* This method is not compatible with other log apis, so if you plan
226to 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
234This protected method is what actually sends the log information to STDERR.
235You may subclass this module and override this method to get finer control
236over the log output.
237
3cdcf968 238=head2 meta
239
fc7ec1d9 240=head1 SEE ALSO
241
242L<Catalyst>.
243
244=head1 AUTHOR
245
246Sebastian Riedel, C<sri@cpan.org>
61b1e958 247Marcus Ramberg, C<mramberg@cpan.org>
a6433a05 248Christian Hansen, C<ch@ngmedia.com>
fc7ec1d9 249
250=head1 COPYRIGHT
251
a6433a05 252This program is free software, you can redistribute it and/or modify
61b1e958 253it under the same terms as Perl itself.
fc7ec1d9 254
255=cut
256
e5ecd5bc 257__PACKAGE__->meta->make_immutable;
258
fc7ec1d9 2591;