Subject: | [Patch] Extraneous error message when logging is enabled |
When logging is enabled in Sphinx::Search 0.30, an extraneous error message is generated before connecting to the Sphinx server:
#!/usr/bin/env perl
use strict;
use warnings 'all';
use 5.010;
package MyLogger {
use Data::Dump qw(dump);
sub new {
my $self = shift;
return bless {}, $self;
}
sub AUTOLOAD {
my $self = shift;
(my $name = $MyLogger::AUTOLOAD) =~ s/^.*:://;
warn uc($name), ' - ', dump(@_), "\n";
}
}
use Sphinx::Search;
say "Sphinx::Search v$Sphinx::Search::VERSION";
my $sphinx = Sphinx::Search->new({ debug => 1, log => MyLogger->new });
$sphinx->SetServer('sphinx.example.com', 3312);
$sphinx->Query('foo');
__END__
Sphinx::Search v0.30
ERROR - undef
DEBUG - "Connecting to sphinx.example.com:3312"
DEBUG - "Got version 1 from searchd"
DEBUG - "Sending version"
DEBUG - "Writing to socket"
DEBUG - "Connection complete"
DEBUG - "Writing to socket"
DESTROY - ()
This happens because Sphinx::Search::_Connect calls the _Error method with no arguments:
sub _Connect {
my $self = shift;
$self->_Error(); #reset old errors in new connection
and the _Error method doesn't check for undef:
sub _Error {
my ($self, $msg) = @_;
$self->{_error} = $msg;
$self->{_log}->error($msg) if $self->{_log};
return;
}
It's confusing to see an error message before a successful connection (especially since most loggers will also spit out an "uninitialized variable" warning). The attached patch makes it so _Error and _Warning only send a message to the logger if the message is actually defined.
Subject: | Search.pm.dont_log_undef.patch |
--- lib/Sphinx/Search.pm 2017-12-08 12:24:25.000000000 -0600
+++ lib/Sphinx/Search.pm.new 2017-12-08 12:24:11.000000000 -0600
@@ -415,7 +415,7 @@
my ($self, $msg) = @_;
$self->{_error} = $msg;
- $self->{_log}->error($msg) if $self->{_log};
+ $self->{_log}->error($msg) if $self->{_log} && defined $msg;
return;
}
@@ -442,7 +442,7 @@
my ($self, $msg) = @_;
$self->{_warning} = $msg;
- $self->{_log}->warn($msg) if $self->{_log};
+ $self->{_log}->warn($msg) if $self->{_log} && defined $msg;
return;
}