4 with 'MooseX::Emulate::Class::Accessor::Fast';
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 psgienv => (is => 'rw', predicate => 'has_psgienv', clearer => 'clear_psgienv');
19 my @levels = qw[ debug info warn error fatal ];
21 my $meta = Class::MOP::get_metaclass_by_name(__PACKAGE__);
23 for ( my $i = $#levels ; $i >= 0 ; $i-- ) {
25 my $name = $levels[$i];
28 $summed_level |= $level;
30 $LEVELS{$name} = $level;
31 $LEVEL_MATCH{$name} = $summed_level;
33 $meta->add_method($name, sub {
36 if ( $self->level & $level ) {
37 $self->_log( $name, @_ );
41 $meta->add_method("is_$name", sub {
43 return $self->level & $level;
51 my $self = $class->$orig;
53 $self->levels( scalar(@_) ? @_ : keys %LEVELS );
59 my ( $self, @levels ) = @_;
61 $self->enable(@levels);
65 my ( $self, @levels ) = @_;
66 my $level = $self->level;
67 for(map { $LEVEL_MATCH{$_} } @levels){
74 my ( $self, @levels ) = @_;
75 my $level = $self->level;
76 for(map { $LEVELS{$_} } @levels){
85 unless ($HAS_DUMPED++) {
86 cluck("Catalyst::Log::_dump is deprecated and will be removed. Please change to using your own Dumper.\n");
88 $self->info( Data::Dump::dump(@_) );
94 my $message = join( "\n", @_ );
95 $message .= "\n" unless $message =~ /\n$/;
96 if ($self->can('has_psgienv') and $self->has_psgienv and $self->psgienv->{'psgix.logger'}) {
97 $self->psgienv->{'psgix.logger'}->({
102 my $body = $self->_body;
103 $body .= sprintf( "[%s] %s", $level, $message );
110 if ( $self->abort || !$self->_body ) {
114 $self->_send_to_log( $self->_body );
121 if ($self->can('has_psgienv') and $self->has_psgienv) {
122 $self->psgienv->{'psgi.errors'}->print(@_);
129 # Alias _body to body, add a before modifier to warn..
130 my $meta = __PACKAGE__->meta; # Calling meta method here fine as we happen at compile time.
131 $meta->add_method('body', $meta->get_method('_body'));
132 my %package_hash; # Only warn once per method, per package.
133 # I haven't provided a way to disable them, patches welcome.
134 $meta->add_before_method_modifier('body', sub {
135 my $class = blessed(shift);
136 $package_hash{$class}++ || do {
137 warn("Class $class is calling the deprecated method Catalyst::Log->body method,\n"
138 . "this will be removed in Catalyst 5.81");
141 # End 5.70 backwards compatibility hacks.
144 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
152 Catalyst::Log - Catalyst Log Class
157 $log->debug($message);
158 $log->info($message);
159 $log->warn($message);
160 $log->error($message);
161 $log->fatal($message);
163 if ( $log->is_debug ) {
164 # expensive debugging
172 This module provides the default, simple logging functionality for Catalyst.
173 If you want something different set C<< $c->log >> in your application module,
176 $c->log( MyLogger->new );
178 Your logging object is expected to provide the interface described here.
179 Good alternatives to consider are Log::Log4Perl and Log::Dispatch.
181 If you want to be able to log arbitrary warnings, you can do something along
184 $SIG{__WARN__} = sub { MyApp->log->warn(@_); };
186 however this is (a) global, (b) hairy and (c) may have unexpected side effects.
187 Don't say we didn't warn you.
194 $log->debug($message);
199 $log->info($message);
204 $log->warn($message);
209 $log->error($message);
214 $log->fatal($message);
220 Constructor. Defaults to enable all levels unless levels are provided in
223 $log = Catalyst::Log->new;
224 $log = Catalyst::Log->new( 'warn', 'error' );
228 Contains a bitmask of the currently set log levels.
234 $log->levels( 'warn', 'error', 'fatal' );
240 $log->enable( 'warn', 'error' );
246 $log->disable( 'warn', 'error' );
258 Is the log level active?
262 Should Catalyst emit logs for this request? Will be reset at the end of
265 *NOTE* This method is not compatible with other log apis, so if you plan
266 to use Log4Perl or another logger, you should call it like this:
268 $c->log->abort(1) if $c->log->can('abort');
272 $log->_send_to_log( @messages );
274 This protected method is what actually sends the log information to STDERR.
275 You may subclass this module and override this method to get finer control
286 Catalyst Contributors, see Catalyst.pm
290 This library is free software. You can redistribute it and/or modify
291 it under the same terms as Perl itself.
295 __PACKAGE__->meta->make_immutable;