Skip to content

Commit

Permalink
Implemented suggestion by Neil Hooey to check for and report
Browse files Browse the repository at this point in the history
undefined message elements before they're assembled in Appender.pm
and a warning is issued without a proper code location:
mschilli#15
  • Loading branch information
mschilli committed Jul 10, 2012
1 parent b60c85a commit 3bfef78
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* (ms) Meir Guttman reported a use case for logging messages
with dynamic levels on log4perl-devel@lists.sourceforge.net.
Added to manual page.
* (ms) Implemented suggestion by Neil Hooey to check for and report
undefined message elements before they're assembled in Appender.pm
and a warning is issued without a proper code location:
https://github.com/mschilli/log4perl/issues/15

1.37 (2012/05/30)
* (ms) [rt.cpan.org #75655] Meir Guttman found the module to make
Expand Down
21 changes: 16 additions & 5 deletions lib/Log/Log4perl/Appender.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use 5.006;
use strict;
use warnings;

use Log::Log4perl::Level;
use Log::Log4perl::Config;
use Log::Log4perl::Level;
use Carp;

use constant _INTERNAL_DEBUG => 0;

Expand Down Expand Up @@ -162,10 +163,20 @@ sub log {
#not defined, the normal case
if (! defined $self->{warp_message} ){
#join any message elements
$p->{message} =
join($Log::Log4perl::JOIN_MSG_ARRAY_CHAR,
@{$p->{message}}
) if ref $p->{message} eq "ARRAY";
if (ref $p->{message} eq "ARRAY") {
for my $i (0..$#{$p->{message}}) {
if( !defined $p->{message}->[ $i ] ) {
local $Carp::CarpLevel =
$Carp::CarpLevel + $Log::Log4perl::caller_depth + 1;
carp "Warning: Log message argument #" .
($i+1) . " undefined";
}
}
$p->{message} =
join($Log::Log4perl::JOIN_MSG_ARRAY_CHAR,
@{$p->{message}}
);
}

#defined but false, e.g. Appender::DBI
} elsif (! $self->{warp_message}) {
Expand Down
28 changes: 28 additions & 0 deletions t/065Undef.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use strict;

use File::Temp qw( tempfile );

BEGIN {
if($ENV{INTERNAL_DEBUG}) {
require Log::Log4perl::InternalDebug;
Log::Log4perl::InternalDebug->enable();
}
}

my($tmpfh, $tempfile) = tempfile( UNLINK => 1 );

use Test::More;
BEGIN { plan tests => 1 };
use Log::Log4perl qw( :easy );

Log::Log4perl->easy_init( { level => $DEBUG, file => $tempfile } );

my $warnings = "";

$SIG{__WARN__} = sub {
$warnings .= $_[0];
};

DEBUG "foo", undef, "bar";

like $warnings, qr/Log message argument #2/, "warning for undef element issued";

0 comments on commit 3bfef78

Please sign in to comment.