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;
45 $self->levels( scalar(@_) ? @_ : keys %LEVELS );
50 my ( $self, @levels ) = @_;
52 $self->enable(@levels);
56 my ( $self, @levels ) = @_;
57 my $level = $self->level;
58 for(map { $LEVELS{$_} } @levels){
65 my ( $self, @levels ) = @_;
66 my $level = $self->level;
67 for(map { $LEVELS{$_} } @levels){
75 $self->info( Data::Dump::dump(@_) );
81 my $message = join( "\n", @_ );
82 $message .= "\n" unless $message =~ /\n$/;
83 my $body = $self->_body;
84 $body .= sprintf( "[%s] %s", $level, $message );
90 if ( $self->abort || !$self->_body ) {
94 $self->_send_to_log( $self->_body );
105 # Alias _body to body, add a before modifier to warn..
106 my $meta = __PACKAGE__->meta; # Calling meta method here fine as we happen at compile time.
107 $meta->add_method('body', $meta->get_method('_body'));
108 my %package_hash; # Only warn once per method, per package.
109 # I haven't provided a way to disable them, patches welcome.
110 $meta->add_before_method_modifier('body', sub {
111 my $class = blessed(shift);
112 $package_hash{$class}++ || do {
113 warn("Class $class is calling the deprecated method Catalyst::Log->body method,\n"
114 . "this will be removed in Catalyst 5.81");
117 # End 5.70 backwards compatibility hacks.
120 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
128 Catalyst::Log - Catalyst Log Class
133 $log->debug($message);
134 $log->info($message);
135 $log->warn($message);
136 $log->error($message);
137 $log->fatal($message);
139 if ( $log->is_debug ) {
140 # expensive debugging
148 This module provides the default, simple logging functionality for Catalyst.
149 If you want something different set C<< $c->log >> in your application module,
152 $c->log( MyLogger->new );
154 Your logging object is expected to provide the interface described here.
155 Good alternatives to consider are Log::Log4Perl and Log::Dispatch.
157 If you want to be able to log arbitrary warnings, you can do something along
160 $SIG{__WARN__} = sub { MyApp->log->warn(@_); };
162 however this is (a) global, (b) hairy and (c) may have unexpected side effects.
163 Don't say we didn't warn you.
170 $log->debug($message);
175 $log->info($message);
180 $log->warn($message);
185 $log->error($message);
190 $log->fatal($message);
196 Constructor. Defaults to enable all levels unless levels are provided in
199 $log = Catalyst::Log->new;
200 $log = Catalyst::Log->new( 'warn', 'error' );
204 Contains a bitmask of the currently set log levels.
210 $log->levels( 'warn', 'error', 'fatal' );
216 $log->enable( 'warn', 'error' );
222 $log->disable( 'warn', 'error' );
234 Is the log level active?
238 Should Catalyst emit logs for this request? Will be reset at the end of
241 *NOTE* This method is not compatible with other log apis, so if you plan
242 to use Log4Perl or another logger, you should call it like this:
244 $c->log->abort(1) if $c->log->can('abort');
248 $log->_send_to_log( @messages );
250 This protected method is what actually sends the log information to STDERR.
251 You may subclass this module and override this method to get finer control
262 Catalyst Contributors, see Catalyst.pm
266 This program is free software, you can redistribute it and/or modify
267 it under the same terms as Perl itself.
271 __PACKAGE__->meta->make_immutable;