4 with 'MooseX::Emulate::Class::Accessor::Fast';
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 = Class::MOP::get_metaclass_by_name(__PACKAGE__);
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;
46 if (@_ == 1 && $_[0] eq 'debug') {
47 $self->levels( keys %LEVELS );
49 $self->levels( scalar(@_) ? @_ : keys %LEVELS );
56 my ( $self, @levels ) = @_;
58 $self->enable(@levels);
62 my ( $self, @levels ) = @_;
63 my $level = $self->level;
64 for(map { $LEVELS{$_} } @levels){
71 my ( $self, @levels ) = @_;
72 my $level = $self->level;
73 for(map { $LEVELS{$_} } @levels){
81 $self->info( Data::Dump::dump(@_) );
87 my $message = join( "\n", @_ );
88 $message .= "\n" unless $message =~ /\n$/;
89 my $body = $self->_body;
90 $body .= sprintf( "[%s] %s", $level, $message );
96 if ( $self->abort || !$self->_body ) {
100 $self->_send_to_log( $self->_body );
111 # Alias _body to body, add a before modifier to warn..
112 my $meta = __PACKAGE__->meta; # Calling meta method here fine as we happen at compile time.
113 $meta->add_method('body', $meta->get_method('_body'));
114 my %package_hash; # Only warn once per method, per package.
115 # I haven't provided a way to disable them, patches welcome.
116 $meta->add_before_method_modifier('body', sub {
117 my $class = blessed(shift);
118 $package_hash{$class}++ || do {
119 warn("Class $class is calling the deprecated method Catalyst::Log->body method,\n"
120 . "this will be removed in Catalyst 5.81");
123 # End 5.70 backwards compatibility hacks.
126 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
134 Catalyst::Log - Catalyst Log Class
139 $log->debug($message);
140 $log->info($message);
141 $log->warn($message);
142 $log->error($message);
143 $log->fatal($message);
145 if ( $log->is_debug ) {
146 # expensive debugging
154 This module provides the default, simple logging functionality for Catalyst.
155 If you want something different set C<< $c->log >> in your application module,
158 $c->log( MyLogger->new );
160 Your logging object is expected to provide the interface described here.
161 Good alternatives to consider are Log::Log4Perl and Log::Dispatch.
163 If you want to be able to log arbitrary warnings, you can do something along
166 $SIG{__WARN__} = sub { MyApp->log->warn(@_); };
168 however this is (a) global, (b) hairy and (c) may have unexpected side effects.
169 Don't say we didn't warn you.
176 $log->debug($message);
181 $log->info($message);
186 $log->warn($message);
191 $log->error($message);
196 $log->fatal($message);
202 Constructor. Defaults to enable all levels unless levels are provided in
205 $log = Catalyst::Log->new;
206 $log = Catalyst::Log->new( 'warn', 'error' );
210 Contains a bitmask of the currently set log levels.
216 $log->levels( 'warn', 'error', 'fatal' );
222 $log->enable( 'warn', 'error' );
228 $log->disable( 'warn', 'error' );
240 Is the log level active?
244 Should Catalyst emit logs for this request? Will be reset at the end of
247 *NOTE* This method is not compatible with other log apis, so if you plan
248 to use Log4Perl or another logger, you should call it like this:
250 $c->log->abort(1) if $c->log->can('abort');
254 $log->_send_to_log( @messages );
256 This protected method is what actually sends the log information to STDERR.
257 You may subclass this module and override this method to get finer control
268 Catalyst Contributors, see Catalyst.pm
272 This program is free software, you can redistribute it and/or modify
273 it under the same terms as Perl itself.
277 __PACKAGE__->meta->make_immutable;