Fixed some typos
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Log.pm
index d7776f3..1bd646a 100644 (file)
@@ -4,65 +4,118 @@ use strict;
 use base 'Class::Accessor::Fast';
 use Data::Dumper;
 
-$Data::Dumper::Terse = 1;
+our @levels = qw[ debug info warn error fatal ];
 
-=head1 NAME
+{
+    no strict 'refs';
 
-Catalyst::Log - Catalyst Log Class
+    for ( my $i = 0 ; $i < @levels ; $i++ ) {
 
-=head1 SYNOPSIS
+        my $name  = $levels[$i];
+        my $level = 1 << $i;
 
-See L<Catalyst>.
+        *{$name} = sub {
+            my $self = shift;
 
-=head1 DESCRIPTION
+            if ( $self->{level} & $level ) {
+                $self->_log( $name, @_ );
+            }
+        };
 
-Simple logging functionality for Catalyst.
+        *{"is_$name"} = sub {
+            my $self = shift;
 
-=head2 METHODS
+            if (@_) {
+                if ( $_[0] ) {
+                    $self->{level} |= $level;
+                }
+                else {
+                    $self->{level} &= ~$level;
+                }
+            }
+            return $self->{level} & $level;
+        };
+    }
 
-=head3 debug
+    *new = sub { bless( { level => ( 1 << @levels ) - 1 }, shift ) }
+}
 
-Log debug informations.
+sub _dump { 
+    my $self = shift;
+    local $Data::Dumper::Terse = 1;
+    $self->info( Dumper( $_[0] ) );
+}
 
-=cut
+sub _log {
+    my $self    = shift;
+    my $level   = shift;
+    my $time    = localtime(time);
+    my $message = join( "\n", @_ );
+    printf( STDERR "[%s] [catalyst] [%s] %s\n", $time, $level, $message );
+}
 
-sub debug { _format( 'debug', $_[1] ) }
+1;
 
-=head3 dump
+__END__
 
-Dump stuff.
+=head1 NAME
 
-=cut
+Catalyst::Log - Catalyst Log Class
 
-sub dump { _format( 'dump', Dumper( $_[1] ) ) }
+=head1 SYNOPSIS
 
-=head3 error
+    $log = $c->log;
+    $log->debug($message);
+    $log->info($message);
+    $log->warn($message);
+    $log->error($message);
+    $log->fatal($message);
+    
+    $log->is_debug;   # true if debug messages is enabled
+    $log->is_info;    # true if info messages is enabled
+    $log->is_warn;    # true if warn messages is enabled 
+    $log->is_error;   # true if error messages is enabled
+    $log->is_fatal;   # true if fatal messages is enabled
+
+    if ( $log->is_debug ) {
+         # expensive debugging
+    }
 
-Log error informations.
+See L<Catalyst>.
 
-=cut
+=head1 DESCRIPTION
 
-sub error { _format( 'error', $_[1] ) }
+This module provides the default, simple logging functionality for 
+Catalyst.
+If you want something different set C<$c->log> in your application 
+module, e.g.:
 
-=head3 info
+    $c->log( MyLogger->new );
 
-Log informations.
+Your logging object is expected to provide the interface described here.
 
-=cut
 
-sub info { _format( 'info', $_[1] ) }
+=head1 METHODS
 
-=head3 warn
+=over 4
 
-Log warnings.
+=item $log->debug($message)
 
-=cut
+Logs a debugging message.
 
-sub warn { _format( 'warn', $_[1] ) }
+=item $log->error($message)
 
-sub _format {
-    print STDERR '[' . localtime(time) . "] [catalyst] [$_[0]] $_[1]\n";
-}
+Logs an error message.
+
+=item $log->info($message)
+
+Logs an informational message.
+
+=item $log->warn($message)
+
+Logs a warning message.
+
+=back
 
 =head1 SEE ALSO
 
@@ -71,11 +124,12 @@ L<Catalyst>.
 =head1 AUTHOR
 
 Sebastian Riedel, C<sri@cpan.org>
+Marcus Ramberg, C<mramberg@cpan.org>
 
 =head1 COPYRIGHT
 
-This program is free software, you can redistribute it and/or modify it under
-the same terms as Perl itself.
+This program is free software, you can redistribute it and/or modify 
+it under the same terms as Perl itself.
 
 =cut