Skip to content
Browse files

[RT 73462] Changed logwarn/logcluck/logcarp/error_warn to

warn() unconditionally and send the message to log4perl which
will log it only if the log level conditions are met.
  • Loading branch information...
1 parent d180447 commit 7d7fbbb45254d6c903f5b3cb91d7059c8267c40d @mschilli committed
Showing with 48 additions and 13 deletions.
  1. +6 −0 Changes
  2. +11 −3 lib/Log/Log4perl.pm
  3. +8 −4 lib/Log/Log4perl/Logger.pm
  4. +21 −4 t/024WarnDieCarp.t
  5. +2 −2 t/051Extra.t
View
6 Changes
@@ -1,6 +1,12 @@
##################################################
Revision history for Log::Log4perl
##################################################
+
+1.35 (not yet released)
+ * (ms) [RT 73462] Changed logwarn/logcluck/logcarp/error_warn to
+ warn() unconditionally and send the message to log4perl which
+ will log it only if the log level conditions are met.
+
1.34 (2011/11/04)
* (ms) InternalDebug now replaces all instances of INTERNAL_DEBUG,
not just the first one.
View
14 lib/Log/Log4perl.pm
@@ -898,11 +898,19 @@ Rather than doing the following:
you can use the following:
- $logger->logwarn();
$logger->logdie();
-These print out log messages in the WARN and FATAL level, respectively,
-and then call the built-in warn() and die() functions. Since there is
+And if instead of using
+
+ warn($message);
+ $logger->warn($message);
+
+to both issue a warning via Perl's warn() mechanism and make sure you have
+the same message in the log file as well, use:
+
+ $logger->logwarn();
+
+Since there is
an ERROR level between WARN and FATAL, there are two additional helper
functions in case you'd like to use ERROR for either warn() or die():
View
12 lib/Log/Log4perl/Logger.pm
@@ -862,8 +862,9 @@ sub logwarn {
my @chomped = @_;
chomp($chomped[-1]);
$self->warn(@chomped);
- $self->and_warn(@_);
}
+
+ $self->and_warn(@_);
}
##################################################
@@ -923,8 +924,9 @@ sub logcluck {
foreach (split(/\n/, $message)) {
$self->warn("$_\n");
}
- Carp::cluck($msg);
}
+
+ Carp::cluck($msg);
}
##################################################
@@ -944,8 +946,9 @@ sub logcarp {
foreach (split(/\n/, $message)) {
$self->warn("$_\n");
}
- Carp::carp($msg) if $Log::Log4perl::LOGDIE_MESSAGE_ON_STDERR;
}
+
+ Carp::carp($msg);
}
##################################################
@@ -1012,8 +1015,9 @@ sub error_warn {
if ($self->is_error()) {
$self->error(@_);
- $self->and_warn(@_);
}
+
+ $self->and_warn(@_);
}
##################################################
View
25 t/024WarnDieCarp.t
@@ -62,12 +62,28 @@ sub warndietest_nooutput {
eval { &$method($in_str) };
- unlike($warnstr, qr/$out_str/,
+ unlike($warnstr, qr/\Q$out_str\E/,
"$mname($in_str): STDERR does NOT contain \"$out_str\"");
unlike($app->buffer(), qr/$out_str/,
"$mname($in_str): Buffer does NOT contain \"$out_str\"");
}
+# warn() still prints to stderr, but nothing gets logged
+sub warndietest_stderronly {
+ my ($method, $in_str, $out_str, $app, $mname) = @_;
+
+ eval { &$method($in_str) };
+
+ my($pkg, $file, $line) = caller();
+
+ # it's in stderr
+ like($warnstr, qr/\Q$out_str\E/,
+ "$mname($in_str): STDERR does contain \"$out_str\" ($file:$line)");
+ # but not logged by log4perl
+ unlike($app->buffer(), qr/$out_str/,
+ "$mname($in_str): Buffer does NOT contain \"$out_str\" ($file:$line)");
+}
+
# same as above, just look for no output in buffer, but output in STDERR
sub dietest_nooutput {
my ($method, $in_str, $out_str, $app, $mname) = @_;
@@ -107,7 +123,8 @@ foreach my $f ("logwarn", "logdie", "logcarp", "logcroak", "logcluck",
}
######################################################################
-# change the log level to ERROR... warns should produce nothing now
+# change the log level to ERROR... warns should produce nothing in
+# log4perl now, but logwarn still triggers warn()
$log->level($ERROR);
@@ -119,7 +136,7 @@ foreach my $f ("logdie", "logcroak",
foreach my $f ("logwarn", "logcarp", "logcluck",
) {
- warndietest_nooutput(sub {$log->$f(@_)}, "Test $test: $f", "Test $test: $f", $app, "$f");
+ warndietest_stderronly(sub {$log->$f(@_)}, "Test $test: $f", "Test $test: $f", $app, "$f");
$test++;
}
@@ -129,7 +146,7 @@ foreach my $f ("logwarn", "logcarp", "logcluck",
$log->level($OFF); # $OFF == $FATAL... although I suspect thats a bug in the log4j spec
foreach my $f ("logwarn", "logcarp", "logcluck", "error_warn") {
- warndietest_nooutput(sub {$log->$f(@_)}, "Test $test: $f", "Test $test: $f", $app, "$f");
+ warndietest_stderronly(sub {$log->$f(@_)}, "Test $test: $f", "Test $test: $f", $app, "$f");
$test++;
}
View
4 t/051Extra.t
@@ -68,7 +68,7 @@ $buf->buffer("");
my $line_ref = __LINE__ + 1;
LOGCARP("logcarp");
-is(readstderr(), "", "No output to stderr");
+like(readstderr(), qr/logcarp at /, "Output to stderr");
SKIP: { use Carp;
skip "Detected buggy Carp.pm (upgrade to perl-5.8.*)", 3 unless
defined $Carp::VERSION;
@@ -76,7 +76,7 @@ SKIP: { use Carp;
$line_ref += 9;
$buf->buffer("");
LOGCARP("logcarp");
- is(readstderr(), "", "No output to stderr");
+ like(readstderr(), qr/logcarp at /, "Output to stderr");
like($buf->buffer(), qr/logcarp.*$line_ref/, "Appender output intact");
}

0 comments on commit 7d7fbbb

Please sign in to comment.
Something went wrong with that request. Please try again.