4 with 'MooseX::Emulate::Class::Accessor::Fast';
7 use Class::MOP::Object ();
11 has level => (is => 'rw');
12 has _body => (is => 'rw');
13 has abort => (is => 'rw');
16 my @levels = qw[ debug info warn error fatal ];
18 my $meta = __PACKAGE__->Class::MOP::Object::meta();
19 for ( my $i = 0 ; $i < @levels ; $i++ ) {
21 my $name = $levels[$i];
24 $LEVELS{$name} = $level;
26 $meta->add_method($name, sub {
29 if ( $self->level & $level ) {
30 $self->_log( $name, @_ );
34 $meta->add_method("is_$name", sub {
36 return $self->level & $level;
44 my $self = $class->$orig;
45 $self->levels( scalar(@_) ? @_ : keys %LEVELS );
50 my ( $self, @levels ) = @_;
52 $self->enable(@levels);
56 my ( $self, @levels ) = @_;
57 my $level = $self->level;
58 for(map { $LEVELS{$_} } @levels){
65 my ( $self, @levels ) = @_;
66 my $level = $self->level;
67 for(map { $LEVELS{$_} } @levels){
75 $self->info( Data::Dump::dump(@_) );
81 my $message = join( "\n", @_ );
82 $message .= "\n" unless $message =~ /\n$/;
83 my $body = $self->_body;
84 $body .= sprintf( "[%s] %s", $level, $message );
90 if ( $self->abort || !$self->_body ) {
94 $self->_send_to_log( $self->_body );
105 __PACKAGE__->meta->make_immutable();
113 Catalyst::Log - Catalyst Log Class
118 $log->debug($message);
119 $log->info($message);
120 $log->warn($message);
121 $log->error($message);
122 $log->fatal($message);
124 if ( $log->is_debug ) {
125 # expensive debugging
133 This module provides the default, simple logging functionality for Catalyst.
134 If you want something different set C<< $c->log >> in your application module,
137 $c->log( MyLogger->new );
139 Your logging object is expected to provide the interface described here.
140 Good alternatives to consider are Log::Log4Perl and Log::Dispatch.
142 If you want to be able to log arbitrary warnings, you can do something along
145 $SIG{__WARN__} = sub { MyApp->log->warn(@_); };
147 however this is (a) global, (b) hairy and (c) may have unexpected side effects.
148 Don't say we didn't warn you.
155 $log->debug($message);
160 $log->info($message);
165 $log->warn($message);
170 $log->error($message);
175 $log->fatal($message);
181 Constructor. Defaults to enable all levels unless levels are provided in
184 $log = Catalyst::Log->new;
185 $log = Catalyst::Log->new( 'warn', 'error' );
189 Contains a bitmask of the currently set log levels.
195 $log->levels( 'warn', 'error', 'fatal' );
201 $log->enable( 'warn', 'error' );
207 $log->disable( 'warn', 'error' );
219 Is the log level active?
223 Should Catalyst emit logs for this request? Will be reset at the end of
226 *NOTE* This method is not compatible with other log apis, so if you plan
227 to use Log4Perl or another logger, you should call it like this:
229 $c->log->abort(1) if $c->log->can('abort');
233 $log->_send_to_log( @messages );
235 This protected method is what actually sends the log information to STDERR.
236 You may subclass this module and override this method to get finer control
247 Catalyst Contributors, see Catalyst.pm
251 This program is free software, you can redistribute it and/or modify
252 it under the same terms as Perl itself.
256 __PACKAGE__->meta->make_immutable;