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