4 with 'MooseX::Emulate::Class::Accessor::Fast';
7 use Moose::Util 'find_meta';
10 our %LEVELS = (); # Levels stored as bit field, ergo debug = 1, warn = 2 etc
11 our %LEVEL_MATCH = (); # Stored as additive, thus debug = 31, warn = 30 etc
13 has level => (is => 'rw');
14 has _body => (is => 'rw');
15 has abort => (is => 'rw');
16 has _psgi_logger => (is => 'rw', predicate => '_has_psgi_logger', clearer => '_clear_psgi_logger');
17 has _psgi_errors => (is => 'rw', predicate => '_has_psgi_errors', clearer => '_clear_psgi_errors');
21 $self->_clear_psgi_logger;
22 $self->_clear_psgi_errors;
26 my ($self, $env) = @_;
28 $self->_psgi_logger($env->{'psgix.logger'}) if $env->{'psgix.logger'};
29 $self->_psgi_errors($env->{'psgi.errors'}) if $env->{'psgi.errors'};
34 my @levels = qw[ debug info warn error fatal ];
36 my $meta = find_meta(__PACKAGE__);
38 for ( my $i = $#levels ; $i >= 0 ; $i-- ) {
40 my $name = $levels[$i];
43 $summed_level |= $level;
45 $LEVELS{$name} = $level;
46 $LEVEL_MATCH{$name} = $summed_level;
48 $meta->add_method($name, sub {
51 if ( $self->level & $level ) {
52 $self->_log( $name, @_ );
56 $meta->add_method("is_$name", sub {
58 return $self->level & $level;
66 my $self = $class->$orig;
68 $self->levels( scalar(@_) ? @_ : keys %LEVELS );
74 my ( $self, @levels ) = @_;
76 $self->enable(@levels);
80 my ( $self, @levels ) = @_;
81 my $level = $self->level;
82 for(map { $LEVEL_MATCH{$_} } @levels){
89 my ( $self, @levels ) = @_;
90 my $level = $self->level;
91 for(map { $LEVELS{$_} } @levels){
100 unless ($HAS_DUMPED++) {
101 cluck("Catalyst::Log::_dump is deprecated and will be removed. Please change to using your own Dumper.\n");
103 $self->info( Data::Dump::dump(@_) );
109 my $message = join( "\n", @_ );
110 if ($self->can('_has_psgi_logger') and $self->_has_psgi_logger) {
111 $self->_psgi_logger->({
116 $message .= "\n" unless $message =~ /\n$/;
117 my $body = $self->_body;
118 $body .= sprintf( "[%s] %s", $level, $message );
125 if ( $self->abort || !$self->_body ) {
129 $self->_send_to_log( $self->_body );
136 if ($self->can('_has_psgi_errors') and $self->_has_psgi_errors) {
137 $self->_psgi_errors->print(@_);
144 # Alias _body to body, add a before modifier to warn..
145 my $meta = __PACKAGE__->meta; # Calling meta method here fine as we happen at compile time.
146 $meta->add_method('body', $meta->get_method('_body'));
147 my %package_hash; # Only warn once per method, per package.
148 # I haven't provided a way to disable them, patches welcome.
149 $meta->add_before_method_modifier('body', sub {
150 my $class = blessed(shift);
151 $package_hash{$class}++ || do {
152 warn("Class $class is calling the deprecated method Catalyst::Log->body method,\n"
153 . "this will be removed in Catalyst 5.81");
156 # End 5.70 backwards compatibility hacks.
159 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
165 =for stopwords psgienv
169 Catalyst::Log - Catalyst Log Class
174 $log->debug($message);
175 $log->info($message);
176 $log->warn($message);
177 $log->error($message);
178 $log->fatal($message);
180 if ( $log->is_debug ) {
181 # expensive debugging
189 This module provides the default, simple logging functionality for Catalyst.
190 If you want something different set C<< $c->log >> in your application module,
193 $c->log( MyLogger->new );
195 Your logging object is expected to provide the interface described here.
196 Good alternatives to consider are Log::Log4Perl and Log::Dispatch.
198 If you want to be able to log arbitrary warnings, you can do something along
201 $SIG{__WARN__} = sub { MyApp->log->warn(@_); };
203 however this is (a) global, (b) hairy and (c) may have unexpected side effects.
204 Don't say we didn't warn you.
211 $log->debug($message);
216 $log->info($message);
221 $log->warn($message);
226 $log->error($message);
231 $log->fatal($message);
237 Constructor. Defaults to enable all levels unless levels are provided in
240 $log = Catalyst::Log->new;
241 $log = Catalyst::Log->new( 'warn', 'error' );
245 Contains a bitmask of the currently set log levels.
251 $log->levels( 'warn', 'error', 'fatal' );
257 $log->enable( 'warn', 'error' );
263 $log->disable( 'warn', 'error' );
275 Is the log level active?
279 Should Catalyst emit logs for this request? Will be reset at the end of
282 *NOTE* This method is not compatible with other log apis, so if you plan
283 to use Log4Perl or another logger, you should call it like this:
285 $c->log->abort(1) if $c->log->can('abort');
289 $log->_send_to_log( @messages );
291 This protected method is what actually sends the log information to STDERR.
292 You may subclass this module and override this method to get finer control
299 NOTE: This is not meant for public consumption.
301 Set the PSGI environment for this request. This ensures logs will be sent to
302 the right place. If the environment has a C<psgix.logger>, it will be used. If
303 not, we will send logs to C<psgi.errors> if that exists. As a last fallback, we
304 will send to STDERR as before.
308 Clears the PSGI environment attributes set by L</psgienv>.
318 Catalyst Contributors, see Catalyst.pm
322 This library is free software. You can redistribute it and/or modify
323 it under the same terms as Perl itself.
327 __PACKAGE__->meta->make_immutable;