From: Arthur Axel 'fREW' Schmidt Date: Fri, 9 Jul 2010 01:09:37 +0000 (-0500) Subject: add levels_upto to SimpleLogger X-Git-Tag: v0.00302~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=13955c5370339ff2c026e338496049b455ffedb5;p=p5sagit%2FLog-Contextual.git add levels_upto to SimpleLogger --- diff --git a/Changes b/Changes index 378da9d..2ee225b 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,6 @@ ChangeLog for Log-Contextual + - Add levels_upto RT58558 - Use Log::Log4perl 1.29 to clean up caller stuff 0.00300 2010-06-03 diff --git a/lib/Log/Contextual/SimpleLogger.pm b/lib/Log/Contextual/SimpleLogger.pm index 094fb16..5c4e3bc 100644 --- a/lib/Log/Contextual/SimpleLogger.pm +++ b/lib/Log/Contextual/SimpleLogger.pm @@ -28,6 +28,20 @@ sub new { $self->{$_} = 1 for @{$args->{levels}}; $self->{coderef} = $args->{coderef} || sub { print STDERR @_}; + + if (my $upto = $args->{levels_upto}) { + + my @levels = (qw( trace debug info warn error fatal )); + my $i = 0; + for (@levels) { + last if $upto eq $_; + $i++ + } + for ($i..$#levels) { + $self->{$levels[$_]} = 1 + } + + } return $self; } @@ -70,16 +84,29 @@ L. =head2 new -Arguments: C<< Dict[ levels => ArrayRef[Str], coderef => Optional[CodeRef] ] $conf >> +Arguments: C<< Dict[ + levels => Optional[ArrayRef[Str]], + levels_upto => Level, + coderef => Optional[CodeRef], +] $conf >> my $l = Log::Contextual::SimpleLogger->new({ - levels => [qw( info warn )], + levels => [qw( info warn )], coderef => sub { print @_ }, # the default prints to STDERR }); +or + + my $l = Log::Contextual::SimpleLogger->new({ + levels_upto => 'debug', + coderef => sub { print @_ }, # the default prints to STDERR + }); + Creates a new SimpleLogger object with the passed levels enabled and optionally a C may be passed to modify how the logs are output/stored. +C enables all the levels upto and including the level passed. + Levels may contain: trace diff --git a/t/simplelogger.t b/t/simplelogger.t index 968ff83..f9fd708 100644 --- a/t/simplelogger.t +++ b/t/simplelogger.t @@ -35,8 +35,8 @@ my $l2 = Log::Contextual::SimpleLogger->new({ coderef => sub { $response = $_[0] }, }); { - local $SIG{__WARN__} = sub {}; # do this just to hide warning for tests - set_logger($l2); + local $SIG{__WARN__} = sub {}; # do this just to hide warning for tests + set_logger($l2); } log_trace { 'trace' }; is($response, "[trace] trace\n", 'trace renders correctly'); @@ -54,3 +54,12 @@ is($response, "[fatal] fatal\n", 'fatal renders correctly'); log_debug { 'line 1', 'line 2' }; is($response, "[debug] line 1\nline 2\n", 'multiline log renders correctly'); +my $u = Log::Contextual::SimpleLogger->new({levels_upto => 'debug'}); + +ok(!$u->is_trace, 'is_trace is false on SimpleLogger'); +ok($u->is_debug, 'is_debug is true on SimpleLogger'); +ok($u->is_info, 'is_info is true on SimpleLogger'); +ok($u->is_warn, 'is_warn is true on SimpleLogger'); +ok($u->is_error, 'is_error is true on SimpleLogger'); +ok($u->is_fatal, 'is_fatal is true on SimpleLogger'); +