From: Jakob Voss Date: Sat, 31 Aug 2013 18:55:29 +0000 (+0200) Subject: added Log::Contextual::Easy for zero-config usage X-Git-Tag: v0.006000~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=436c4b82f402aa8a60e549970780c3ed653c4910;p=p5sagit%2FLog-Contextual.git added Log::Contextual::Easy for zero-config usage --- diff --git a/Changes b/Changes index d6988e4..b6355da 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ Revision history for Log-Contextual + - Add Log::Contextual::Easy for simple LC usage (Jakob Voß) + 0.005005 2013-08-08 - Fix minimum version of Exporter::Declare diff --git a/lib/Log/Contextual.pm b/lib/Log/Contextual.pm index 1a2b3dc..0195e69 100644 --- a/lib/Log/Contextual.pm +++ b/lib/Log/Contextual.pm @@ -315,7 +315,7 @@ wide. =head2 -logger When you import this module you may use C<-logger> as a shortcut for -L, for example: +L, for example: use Log::Contextual::SimpleLogger; use Log::Contextual qw( :dlog ), @@ -424,6 +424,9 @@ import. So the following will all work: sub default_import { qw(:dlog :log ) } +See L for an example of a subclass of +C that makes use of default import options. + =head1 FUNCTIONS =head2 set_logger @@ -677,8 +680,12 @@ frew - Arthur Axel "fREW" Schmidt =head1 CONTRIBUTORS +=encoding utf8 + triddle - Tyler Riddle +voj - Jakob Voß + =head1 DESIGNER mst - Matt S. Trout diff --git a/lib/Log/Contextual/Easy.pm b/lib/Log/Contextual/Easy.pm new file mode 100644 index 0000000..ab2e86a --- /dev/null +++ b/lib/Log/Contextual/Easy.pm @@ -0,0 +1,62 @@ +package Log::Contextual::Easy; + +use base 'Log::Contextual'; + +sub arg_default_logger { + if ($_[1]) { + return $_[1]; + } else { + require Log::Contextual::WarnLogger; + my $package = uc(caller(3)); + $package =~ s/::/_/g; + return Log::Contextual::WarnLogger->new({env_prefix => $package}); + } +} + +sub default_import { qw(:dlog :log ) } + +1; + +__END__ + +=head1 NAME + +Log::Contextual::Easy - Import all logging methods with WarnLogger as default + +=head1 SYNOPSIS + +In your module: + + package My::Module; + use Log::Contextual::Easy; + + log_debug { "your message" }; + Dlog_trace { $_ } @vars; + +In your program: + + use My::Module; + + # enable warnings + $ENV{MY_MODULE_UPTO}="TRACE"; + + # or use a specific logger with set_logger / with_logger + +=head1 DESCRIPTION + +By default, this module enables a L +with C based on the module's name that uses +Log::Contextual::Easy. The logging levels are set to C C, +C, C, C, and C (in this order) and all +logging functions (L, +L, +L, and +L) are exported. + +=head1 SEE ALSO + +=over 4 + +=item L + +=back diff --git a/t/easy.t b/t/easy.t new file mode 100644 index 0000000..e7440cc --- /dev/null +++ b/t/easy.t @@ -0,0 +1,62 @@ +use strict; +use warnings; + +use Test::More; + +use lib 't/lib'; +use My::Module; # makes use of Log::Contextual::Easy; + +# capture logging messages of My::Module, mapping "[...] xxx" to "...$sep" +sub logshort($$) { + my ($cap, $sep) = @_; + sub { + local $_ = shift; + s/^\[(.+)\] (xxx|"xxx")\n$/$1$sep/; + $$cap .= $_; + } +} + +# capture warnings +my ($cap_warn, $cap_with, $cap_set); +local $SIG{__WARN__} = logshort \$cap_warn, '!'; + +{ + My::Module::log(); + is($cap_warn, undef, 'no logging by default'); +} + +{ + local $ENV{MY_MODULE_UPTO} = 'info'; + My::Module::log(); + is($cap_warn, "info!warn!error!fatal!", 'WarnLogger enabled via ENV'); + $cap_warn = ''; +} + +{ + use Log::Contextual::SimpleLogger; + use Log::Contextual qw(with_logger set_logger); + + set_logger( Log::Contextual::SimpleLogger->new({ + levels => [qw(info warn error)], + coderef => logshort \$cap_set, '/' + }) ); + + my $with_logger = Log::Contextual::SimpleLogger->new({ + levels => [qw(trace info fatal)], + coderef => logshort \$cap_with, '|' + }); + + with_logger $with_logger => sub { + My::Module::log(); + }; + is($cap_with, 'trace|info|fatal|', 'with_logger'); + + My::Module::log(); + is($cap_set, 'info/warn/error/', 'set_logger'); + + is($cap_warn, '', 'no warnings if with_logger or set_logger'); +} + +done_testing; + + diff --git a/t/lib/My/Module.pm b/t/lib/My/Module.pm new file mode 100644 index 0000000..b3724aa --- /dev/null +++ b/t/lib/My/Module.pm @@ -0,0 +1,13 @@ +package My::Module; +use Log::Contextual::Easy; + +sub log { + Dlog_fatal { $_ } + DlogS_error { $_ } + logS_warn { $_[0] } + logS_info { $_[0] } + log_debug { $_[0] } + log_trace { $_[0] } 'xxx'; +} + +1;