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 autoflush => (is => 'rw', default => sub {1});
17 has _psgi_logger => (is => 'rw', predicate => '_has_psgi_logger', clearer => '_clear_psgi_logger');
18 has _psgi_errors => (is => 'rw', predicate => '_has_psgi_errors', clearer => '_clear_psgi_errors');
22 $self->_clear_psgi_logger;
23 $self->_clear_psgi_errors;
27 my ($self, $env) = @_;
29 $self->_psgi_logger($env->{'psgix.logger'}) if $env->{'psgix.logger'};
30 $self->_psgi_errors($env->{'psgi.errors'}) if $env->{'psgi.errors'};
35 my @levels = qw[ debug info warn error fatal ];
37 my $meta = find_meta(__PACKAGE__);
39 for ( my $i = $#levels ; $i >= 0 ; $i-- ) {
41 my $name = $levels[$i];
44 $summed_level |= $level;
46 $LEVELS{$name} = $level;
47 $LEVEL_MATCH{$name} = $summed_level;
49 $meta->add_method($name, sub {
52 if ( $self->level & $level ) {
53 $self->_log( $name, @_ );
57 $meta->add_method("is_$name", sub {
59 return $self->level & $level;
68 if (@_ == 1 && ref $_[0] eq 'HASH') {
73 levels => [@_ ? @_ : keys %LEVELS],
77 if (delete $args->{levels}) {
80 for map $LEVEL_MATCH{$_}, @_ ? @_ : keys %LEVELS;
81 $args->{level} = $level;
87 my ( $self, @levels ) = @_;
89 $self->enable(@levels);
93 my ( $self, @levels ) = @_;
94 my $level = $self->level;
95 for(map { $LEVEL_MATCH{$_} } @levels){
102 my ( $self, @levels ) = @_;
103 my $level = $self->level;
104 for(map { $LEVELS{$_} } @levels){
107 $self->level($level);
113 unless ($HAS_DUMPED++) {
114 cluck("Catalyst::Log::_dump is deprecated and will be removed. Please change to using your own Dumper.\n");
116 $self->info( Data::Dump::dump(@_) );
122 my $message = join( "\n", @_ );
123 if ($self->can('_has_psgi_logger') and $self->_has_psgi_logger) {
124 $self->_psgi_logger->({
129 $message .= "\n" unless $message =~ /\n$/;
130 my $body = $self->_body;
131 $body .= sprintf( "[%s] %s", $level, $message );
134 if( $self->autoflush && !$self->abort ) {
142 if ( $self->abort || !$self->_body ) {
146 $self->_send_to_log( $self->_body );
153 if ($self->can('_has_psgi_errors') and $self->_has_psgi_errors) {
154 $self->_psgi_errors->print(@_);
156 binmode STDERR, ":utf8";
162 # Alias _body to body, add a before modifier to warn..
163 my $meta = __PACKAGE__->meta; # Calling meta method here fine as we happen at compile time.
164 $meta->add_method('body', $meta->get_method('_body'));
165 my %package_hash; # Only warn once per method, per package.
166 # I haven't provided a way to disable them, patches welcome.
167 $meta->add_before_method_modifier('body', sub {
168 my $class = blessed(shift);
169 $package_hash{$class}++ || do {
170 warn("Class $class is calling the deprecated method Catalyst::Log->body method,\n"
171 . "this will be removed in Catalyst 5.81");
174 # End 5.70 backwards compatibility hacks.
177 __PACKAGE__->meta->make_immutable;
183 =for stopwords psgienv
187 Catalyst::Log - Catalyst Log Class
192 $log->debug($message);
193 $log->info($message);
194 $log->warn($message);
195 $log->error($message);
196 $log->fatal($message);
198 if ( $log->is_debug ) {
199 # expensive debugging
207 This module provides the default, simple logging functionality for Catalyst.
208 If you want something different set C<< $c->log >> in your application module,
211 $c->log( MyLogger->new );
213 Your logging object is expected to provide the interface described here.
214 Good alternatives to consider are Log::Log4Perl and Log::Dispatch.
216 If you want to be able to log arbitrary warnings, you can do something along
219 $SIG{__WARN__} = sub { MyApp->log->warn(@_); };
221 however this is (a) global, (b) hairy and (c) may have unexpected side effects.
222 Don't say we didn't warn you.
229 $log->debug($message);
234 $log->info($message);
239 $log->warn($message);
244 $log->error($message);
249 $log->fatal($message);
255 Constructor. Defaults to enable all levels unless levels are provided in
258 $log = Catalyst::Log->new;
259 $log = Catalyst::Log->new( 'warn', 'error' );
263 Contains a bitmask of the currently set log levels.
269 $log->levels( 'warn', 'error', 'fatal' );
275 $log->enable( 'warn', 'error' );
281 $log->disable( 'warn', 'error' );
293 Is the log level active?
297 Should Catalyst emit logs for this request? Will be reset at the end of
300 *NOTE* This method is not compatible with other log apis, so if you plan
301 to use Log4Perl or another logger, you should call it like this:
303 $c->log->abort(1) if $c->log->can('abort');
307 When enabled (default), messages are written to the log immediately instead
308 of queued until the end of the request.
310 This option, as well as C<abort>, is provided for modules such as
311 L<Catalyst::Plugin::Static::Simple> to be able to programmatically
312 suppress the output of log messages. By turning off C<autoflush> (application-wide
313 setting) and then setting the C<abort> flag within a given request, all log
314 messages for the given request will be suppressed. C<abort> can still be set
315 independently of turning off C<autoflush>, however. It just means any messages
316 sent to the log up until that point in the request will obviously still be emitted,
317 since C<autoflush> means they are written in real-time.
319 If you need to turn off autoflush you should do it like this (in your main app
322 after setup_finalize => sub {
324 $c->log->autoflush(0) if $c->log->can('autoflush');
329 $log->_send_to_log( @messages );
331 This protected method is what actually sends the log information to STDERR.
332 You may subclass this module and override this method to get finer control
339 NOTE: This is not meant for public consumption.
341 Set the PSGI environment for this request. This ensures logs will be sent to
342 the right place. If the environment has a C<psgix.logger>, it will be used. If
343 not, we will send logs to C<psgi.errors> if that exists. As a last fallback, we
344 will send to STDERR as before.
348 Clears the PSGI environment attributes set by L</psgienv>.
358 Catalyst Contributors, see Catalyst.pm
362 This library is free software. You can redistribute it and/or modify
363 it under the same terms as Perl itself.