From: Arthur Axel 'fREW' Schmidt Date: Sat, 20 Feb 2010 10:12:25 +0000 (-0600) Subject: basic SimpleLogger code X-Git-Tag: v0.00100~18 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=88c83a83fe516bfcab21e4a680e38e6b5fd1d54b;p=p5sagit%2FLog-Contextual.git basic SimpleLogger code --- diff --git a/lib/Log/Contextual/SimpleLogger.pm b/lib/Log/Contextual/SimpleLogger.pm new file mode 100644 index 0000000..55d11f1 --- /dev/null +++ b/lib/Log/Contextual/SimpleLogger.pm @@ -0,0 +1,43 @@ +package Log::Contextual::SimpleLogger; + +use strict; +use warnings; + +{ + for my $name (qw[ trace debug info warn error fatal ]) { + + no strict 'refs'; + + *{$name} = sub { + my $self = shift; + + $self->_log( $name, @_ ) + if ($self->{$name}); + }; + + *{"is_$name"} = sub { + my $self = shift; + return $self->{$name}; + }; + } +} + +sub new { + my ($class, $args) = @_; + my $self = bless {}, $class; + + $self->{$_} = 1 for @{$args->{levels}}; + $self->{coderef} = $args->{coderef} || sub { print STDERR @_}; + return $self; +} + +sub _log { + my $self = shift; + my $level = shift; + my $message = join( "\n", @_ ); + $message .= "\n" unless $message =~ /\n$/; + $self->{coderef}->(sprintf( "[%s] %s", $level, $message )); +} + +1; + diff --git a/t/simplelogger.t b/t/simplelogger.t new file mode 100644 index 0000000..772f03b --- /dev/null +++ b/t/simplelogger.t @@ -0,0 +1,13 @@ +use strict; +use warnings; + +use Log::Contextual::SimpleLogger; +use Log::Contextual qw{:log} => -logger => + Log::Contextual::SimpleLogger->new({levels => [qw{debug}]}); +use Test::More qw(no_plan); +my $l = Log::Contextual::SimpleLogger->new({levels => [qw{debug}]}); + +ok($l->is_debug, 'is_debug is true on SimpleLogger'); + +log_debug { 'set_logger' }; +log_trace { die 'this should live' };