4 use base 'Class::Accessor::Fast';
9 __PACKAGE__->mk_accessors('level');
10 __PACKAGE__->mk_accessors('body');
11 __PACKAGE__->mk_accessors('abort');
14 my @levels = qw[ debug info warn error fatal ];
16 for ( my $i = 0 ; $i < @levels ; $i++ ) {
18 my $name = $levels[$i];
21 $LEVELS{$name} = $level;
28 if ( $self->{level} & $level ) {
29 $self->_log( $name, @_ );
35 return $self->{level} & $level;
42 my $self = $class->SUPER::new;
43 $self->levels( scalar(@_) ? @_ : keys %LEVELS );
48 my ( $self, @levels ) = @_;
50 $self->enable(@levels);
54 my ( $self, @levels ) = @_;
55 $self->{level} |= $_ for map { $LEVELS{$_} } @levels;
59 my ( $self, @levels ) = @_;
60 $self->{level} &= ~$_ for map { $LEVELS{$_} } @levels;
65 $self->info( Data::Dump::dump(@_) );
71 my $message = join( "\n", @_ );
72 $message .= "\n" unless $message =~ /\n$/;
73 $self->{body} .= sprintf( "[%s] %s", $level, $message );
78 if ( $self->abort || !$self->body ) {
82 $self->_send_to_log( $self->body );
98 Catalyst::Log - Catalyst Log Class
103 $log->debug($message);
104 $log->info($message);
105 $log->warn($message);
106 $log->error($message);
107 $log->fatal($message);
109 if ( $log->is_debug ) {
110 # expensive debugging
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,
122 $c->log( MyLogger->new );
124 Your logging object is expected to provide the interface described here.
125 Good alternatives to consider are Log::Log4Perl and Log::Dispatch.
127 If you want to be able to log arbitrary warnings, you can do something along
130 $SIG{__WARN__} = sub { MyApp->log->warn(@_); };
132 however this is (a) global, (b) hairy and (c) may have unexpected side effects.
133 Don't say we didn't warn you.
140 $log->debug($message);
145 $log->info($message);
150 $log->warn($message);
155 $log->error($message);
160 $log->fatal($message);
166 Constructor. Defaults to enable all levels unless levels are provided in
169 $log = Catalyst::Log->new;
170 $log = Catalyst::Log->new( 'warn', 'error' );
176 $log->levels( 'warn', 'error', 'fatal' );
182 $log->enable( 'warn', 'error' );
188 $log->disable( 'warn', 'error' );
200 Is the log level active?
204 Should Catalyst emit logs for this request? Will be reset at the end of
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:
210 $c->log->abort(1) if $c->log->can('abort');
214 $log->_send_to_log( @messages );
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
226 Catalyst Contributors, see Catalyst.pm
230 This program is free software, you can redistribute it and/or modify
231 it under the same terms as Perl itself.