From: Arthur Axel 'fREW' Schmidt Date: Wed, 3 Mar 2010 05:47:10 +0000 (-0600) Subject: Log::Log4perl works now! X-Git-Tag: v0.00200~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=70a0583043822305c068fe55010f712ff70d51ad;p=p5sagit%2FLog-Contextual.git Log::Log4perl works now! --- diff --git a/Changes b/Changes index 9db714a..8a106bf 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,6 @@ ChangeLog for Log-Contextual + - make Log::Log4perl work out of the box - Added WarnLogger for libraries - Warn if set_logger is called more than once - Fix tiny POD errors diff --git a/lib/Log/Contextual.pm b/lib/Log/Contextual.pm index 78ffcb3..e64c778 100644 --- a/lib/Log/Contextual.pm +++ b/lib/Log/Contextual.pm @@ -111,6 +111,7 @@ sub with_logger { sub log_trace (&@) { my $log = _get_logger( caller ); my $code = shift; + local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1; $log->trace($code->(@_)) if $log->is_trace; @_ @@ -119,6 +120,7 @@ sub log_trace (&@) { sub log_debug (&@) { my $log = _get_logger( caller ); my $code = shift; + local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1; $log->debug($code->(@_)) if $log->is_debug; @_ @@ -127,6 +129,7 @@ sub log_debug (&@) { sub log_info (&@) { my $log = _get_logger( caller ); my $code = shift; + local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1; $log->info($code->(@_)) if $log->is_info; @_ @@ -135,6 +138,7 @@ sub log_info (&@) { sub log_warn (&@) { my $log = _get_logger( caller ); my $code = shift; + local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1; $log->warn($code->(@_)) if $log->is_warn; @_ @@ -143,6 +147,7 @@ sub log_warn (&@) { sub log_error (&@) { my $log = _get_logger( caller ); my $code = shift; + local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1; $log->error($code->(@_)) if $log->is_error; @_ @@ -151,6 +156,7 @@ sub log_error (&@) { sub log_fatal (&@) { my $log = _get_logger( caller ); my $code = shift; + local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1; $log->fatal($code->(@_)) if $log->is_fatal; @_ @@ -161,6 +167,7 @@ sub logS_trace (&$) { my $log = _get_logger( caller ); my $code = shift; my $value = shift; + local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1; $log->trace($code->($value)) if $log->is_trace; $value @@ -170,6 +177,7 @@ sub logS_debug (&$) { my $log = _get_logger( caller ); my $code = shift; my $value = shift; + local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1; $log->debug($code->($value)) if $log->is_debug; $value @@ -179,6 +187,7 @@ sub logS_info (&$) { my $log = _get_logger( caller ); my $code = shift; my $value = shift; + local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1; $log->info($code->($value)) if $log->is_info; $value @@ -188,6 +197,7 @@ sub logS_warn (&$) { my $log = _get_logger( caller ); my $code = shift; my $value = shift; + local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1; $log->warn($code->($value)) if $log->is_warn; $value @@ -197,6 +207,7 @@ sub logS_error (&$) { my $log = _get_logger( caller ); my $code = shift; my $value = shift; + local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1; $log->error($code->($value)) if $log->is_error; $value @@ -206,6 +217,7 @@ sub logS_fatal (&$) { my $log = _get_logger( caller ); my $code = shift; my $value = shift; + local $Log::Log4perl::caller_depth = ($Log::Log4perl::caller_depth || 0 ) + 1; $log->fatal($code->($value)) if $log->is_fatal; $value diff --git a/t/log4perl.conf b/t/log4perl.conf new file mode 100644 index 0000000..c86578f --- /dev/null +++ b/t/log4perl.conf @@ -0,0 +1,12 @@ +############################################################ +# A simple root logger with a Log::Log4perl::Appender::File +# file appender in Perl. +############################################################ +log4perl.rootLogger=ERROR, LOGFILE + +log4perl.appender.LOGFILE=Log::Log4perl::Appender::File +log4perl.appender.LOGFILE.filename=myerrs.log +log4perl.appender.LOGFILE.mode=append + +log4perl.appender.LOGFILE.layout=PatternLayout +log4perl.appender.LOGFILE.layout.ConversionPattern=file:%F line:%L method:%M - %m%n diff --git a/t/log4perl.t b/t/log4perl.t new file mode 100644 index 0000000..e55c125 --- /dev/null +++ b/t/log4perl.t @@ -0,0 +1,30 @@ +use strict; +use warnings; + +use Test::More; + +if (!eval "use Log::Log4perl; 1") { + plan skip_all => 'Log::Log4perl not installed' +} else { + plan tests => 2; +} + +use FindBin; +unlink 'myerrs.log' if -e 'myerrs.log'; +Log::Log4perl->init("$FindBin::Bin/log4perl.conf"); +use Log::Contextual qw( :log set_logger ); +set_logger(Log::Log4perl->get_logger); + +log_error { 'err 14' }; + +sub foo { + log_error { 'err 17' }; +} +foo(); +open my $log, '<', 'myerrs.log'; +my @datas = <$log>; +close $log; + +is $datas[0], "file:t/log4perl.t line:18 method:main:: - err 14\n", 'file and line work with Log4perl'; +is $datas[1], "file:t/log4perl.t line:21 method:main::foo - err 17\n", 'file and line work with Log4perl in a sub'; +