From: Arthur Axel 'fREW' Schmidt Date: Fri, 6 Sep 2013 00:10:40 +0000 (-0500) Subject: add ::Easy::Package for more simple LC usage X-Git-Tag: v0.006000~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=92dc8d35b6d6c3671ad0bad956502ac9aef15a70;p=p5sagit%2FLog-Contextual.git add ::Easy::Package for more simple LC usage --- diff --git a/Changes b/Changes index 3a1a695..89333ab 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,7 @@ Revision history for Log-Contextual - Add Log::Contextual::Easy::Default for simple LC usage (Jakob Voß) + - Add Log::Contextual::Easy::Package for more different simple LC usage 0.005005 2013-08-08 - Fix minimum version of Exporter::Declare diff --git a/lib/Log/Contextual/Easy/Default.pm b/lib/Log/Contextual/Easy/Default.pm index 3150fbe..932a32b 100644 --- a/lib/Log/Contextual/Easy/Default.pm +++ b/lib/Log/Contextual/Easy/Default.pm @@ -53,10 +53,14 @@ L, L, and L) are exported. +For what C<::Default> implies, see L. + =head1 SEE ALSO =over 4 =item L +=item L + =back diff --git a/lib/Log/Contextual/Easy/Package.pm b/lib/Log/Contextual/Easy/Package.pm new file mode 100644 index 0000000..100b2e6 --- /dev/null +++ b/lib/Log/Contextual/Easy/Package.pm @@ -0,0 +1,66 @@ +package Log::Contextual::Easy::Package; + +use base 'Log::Contextual'; + +sub arg_package_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::Package - Import all logging methods with WarnLogger as default package logger + +=head1 SYNOPSIS + +In your module: + + package My::Module; + use Log::Contextual::Easy::Package; + + 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. + +For what C<::Package> implies, see L. + +=head1 SEE ALSO + +=over 4 + +=item L + +=item L + +=back diff --git a/t/easy.t b/t/easy.t index e7440cc..8c18276 100644 --- a/t/easy.t +++ b/t/easy.t @@ -4,7 +4,8 @@ use warnings; use Test::More; use lib 't/lib'; -use My::Module; # makes use of Log::Contextual::Easy; +use My::Module; # makes use of Log::Contextual::Easy::Default; +use My::Module2; # makes use of Log::Contextual::Easy::Package; # capture logging messages of My::Module, mapping "[...] xxx" to "...$sep" sub logshort($$) { @@ -22,13 +23,16 @@ local $SIG{__WARN__} = logshort \$cap_warn, '!'; { My::Module::log(); + My::Module2::log(); is($cap_warn, undef, 'no logging by default'); } { local $ENV{MY_MODULE_UPTO} = 'info'; + local $ENV{MY_MODULE2_UPTO} = 'info'; My::Module::log(); - is($cap_warn, "info!warn!error!fatal!", 'WarnLogger enabled via ENV'); + My::Module2::log(); + is($cap_warn, "info!warn!error!fatal!info!warn!error!fatal!", 'WarnLogger enabled via ENV'); $cap_warn = ''; } @@ -48,10 +52,12 @@ local $SIG{__WARN__} = logshort \$cap_warn, '!'; with_logger $with_logger => sub { My::Module::log(); + My::Module2::log(); # will not be overridden }; is($cap_with, 'trace|info|fatal|', 'with_logger'); My::Module::log(); + My::Module2::log(); # will not be overridden is($cap_set, 'info/warn/error/', 'set_logger'); is($cap_warn, '', 'no warnings if with_logger or set_logger'); diff --git a/t/lib/My/Module2.pm b/t/lib/My/Module2.pm new file mode 100644 index 0000000..f8b0f4d --- /dev/null +++ b/t/lib/My/Module2.pm @@ -0,0 +1,13 @@ +package My::Module2; +use Log::Contextual::Easy::Package; + +sub log { + Dlog_fatal { $_ } + DlogS_error { $_ } + logS_warn { $_[0] } + logS_info { $_[0] } + log_debug { $_[0] } + log_trace { $_[0] } 'xxx'; +} + +1;