Logger: Always use STDERR as output filehandle

There's no good reason for the caller of the `log` method to control
the output filehandle.
This commit is contained in:
Richard Hansen 2024-08-02 04:36:59 -04:00
parent 439b0fd0e1
commit f36c2f45aa
2 changed files with 5 additions and 21 deletions

View file

@ -2364,7 +2364,6 @@ sub ynu {
# Takes the following keyword arguments: # Takes the following keyword arguments:
# * `msg` (string): The message to log. # * `msg` (string): The message to log.
# * `label` (string): Severity ('DEBUG', 'WARNING', etc.) to prefix each line with. # * `label` (string): Severity ('DEBUG', 'WARNING', etc.) to prefix each line with.
# * `fh` (file handle): Where to write the log messages.
# * `email` (boolean): Whether to include the message in the next email. # * `email` (boolean): Whether to include the message in the next email.
# * `raw` (boolean): Whether to omit `label` and the contexts (output `msg` as-is). # * `raw` (boolean): Whether to omit `label` and the contexts (output `msg` as-is).
# * `ctx` (optional string): If defined, this is temporarily pushed onto the context stack # * `ctx` (optional string): If defined, this is temporarily pushed onto the context stack
@ -2378,7 +2377,6 @@ sub ynu {
my %args = ( my %args = (
msg => '', msg => '',
label => '', label => '',
fh => *STDERR,
(@_ % 2) ? (msg => pop) : (), (@_ % 2) ? (msg => pop) : (),
@_, @_,
); );
@ -2393,7 +2391,7 @@ sub ynu {
$buffer =~ s/\n/\n$prefix/g; $buffer =~ s/\n/\n$prefix/g;
} }
$buffer .= "\n"; $buffer .= "\n";
print({$args{fh}} $buffer); print(STDERR $buffer);
if ($args{email}) { if ($args{email}) {
$emailbody .= $buffer; $emailbody .= $buffer;

View file

@ -2,23 +2,6 @@ use Test::More;
SKIP: { eval { require Test::Warnings; } or skip($@, 1); } SKIP: { eval { require Test::Warnings; } or skip($@, 1); }
eval { require 'ddclient'; } or BAIL_OUT($@); eval { require 'ddclient'; } or BAIL_OUT($@);
{
my $output;
open(my $fh, '>', \$output);
local *STDERR = $fh;
ddclient::logmsg('to STDERR');
close($fh);
is($output, "to STDERR\n", 'logs to STDERR by default');
}
{
my $output;
open(my $fh, '>', \$output);
ddclient::logmsg(fh => $fh, 'to file handle');
close($fh);
is($output, "to file handle\n", 'logs to provided file handle');
}
my @test_cases = ( my @test_cases = (
{ {
desc => 'adds a newline', desc => 'adds a newline',
@ -116,7 +99,10 @@ for my $tc (@test_cases) {
local $ddclient::emailbody = $tc->{init_email} // ''; local $ddclient::emailbody = $tc->{init_email} // '';
local $ddclient::_l = $ddclient::_l; local $ddclient::_l = $ddclient::_l;
$ddclient::_l = ddclient::pushlogctx($_) for @{$tc->{ctxs} // []}; $ddclient::_l = ddclient::pushlogctx($_) for @{$tc->{ctxs} // []};
ddclient::logmsg(fh => $fh, @{$tc->{args}}); {
local *STDERR = $fh;
ddclient::logmsg(@{$tc->{args}});
}
close($fh); close($fh);
is($output, $tc->{want}, 'output text matches'); is($output, $tc->{want}, 'output text matches');
is($ddclient::emailbody, $tc->{want_email} // '', 'email content matches'); is($ddclient::emailbody, $tc->{want_email} // '', 'email content matches');