4 with 'MooseX::Emulate::Class::Accessor::Fast';
10 has level => (is => 'rw');
11 has _body => (is => 'rw');
12 has abort => (is => 'rw');
15 my @levels = qw[ debug info warn error fatal ];
17 my $meta = __PACKAGE__->meta;
18 for ( my $i = 0 ; $i < @levels ; $i++ ) {
20 my $name = $levels[$i];
23 $LEVELS{$name} = $level;
25 $meta->add_method($name, sub {
28 if ( $self->level & $level ) {
29 $self->_log( $name, @_ );
33 $meta->add_method("is_$name", sub {
35 return $self->level & $level;
43 my $self = $class->$orig;
44 $self->levels( scalar(@_) ? @_ : keys %LEVELS );
49 my ( $self, @levels ) = @_;
51 $self->enable(@levels);
55 my ( $self, @levels ) = @_;
56 my $level = $self->level;
57 for(map { $LEVELS{$_} } @levels){
64 my ( $self, @levels ) = @_;
65 my $level = $self->level;
66 for(map { $LEVELS{$_} } @levels){
74 $self->info( Data::Dump::dump(@_) );
80 my $message = join( "\n", @_ );
81 $message .= "\n" unless $message =~ /\n$/;
82 my $body = $self->_body;
83 $body .= sprintf( "[%s] %s", $level, $message );
89 if ( $self->abort || !$self->_body ) {
93 $self->_send_to_log( $self->_body );
104 __PACKAGE__->meta->make_immutable();
112 Catalyst::Log - Catalyst Log Class
117 $log->debug($message);
118 $log->info($message);
119 $log->warn($message);
120 $log->error($message);
121 $log->fatal($message);
123 if ( $log->is_debug ) {
124 # expensive debugging
132 This module provides the default, simple logging functionality for Catalyst.
133 If you want something different set C<< $c->log >> in your application module,
136 $c->log( MyLogger->new );
138 Your logging object is expected to provide the interface described here.
139 Good alternatives to consider are Log::Log4Perl and Log::Dispatch.
141 If you want to be able to log arbitrary warnings, you can do something along
144 $SIG{__WARN__} = sub { MyApp->log->warn(@_); };
146 however this is (a) global, (b) hairy and (c) may have unexpected side effects.
147 Don't say we didn't warn you.
154 $log->debug($message);
159 $log->info($message);
164 $log->warn($message);
169 $log->error($message);
174 $log->fatal($message);
180 Constructor. Defaults to enable all levels unless levels are provided in
183 $log = Catalyst::Log->new;
184 $log = Catalyst::Log->new( 'warn', 'error' );
188 Contains a bitmask of the currently set log levels.
194 $log->levels( 'warn', 'error', 'fatal' );
200 $log->enable( 'warn', 'error' );
206 $log->disable( 'warn', 'error' );
218 Is the log level active?
222 Should Catalyst emit logs for this request? Will be reset at the end of
225 *NOTE* This method is not compatible with other log apis, so if you plan
226 to use Log4Perl or another logger, you should call it like this:
228 $c->log->abort(1) if $c->log->can('abort');
232 $log->_send_to_log( @messages );
234 This protected method is what actually sends the log information to STDERR.
235 You may subclass this module and override this method to get finer control
246 Catalyst Contributors, see Catalyst.pm
250 This program is free software, you can redistribute it and/or modify
251 it under the same terms as Perl itself.
255 __PACKAGE__->meta->make_immutable;