1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-14 10:13:05 +02:00

Improved logging.

This commit is contained in:
David Steele 2014-06-07 15:30:13 -04:00
parent e60e2b22ed
commit ea1a158d82
5 changed files with 47 additions and 39 deletions

View File

@ -93,7 +93,7 @@ while ($strCommand ne 'exit')
{
confess "path must be defined";
}
$oRemote->output_write($oFile->exists(PATH_ABSOLUTE, $oParamHash{path}) ? "Y" : "N");
}
else
@ -104,7 +104,7 @@ while ($strCommand ne 'exit')
}
}
};
if ($@)
{
$oRemote->error_write($@);

View File

@ -1132,27 +1132,28 @@ sub exists
my $strPathOp = $self->path_get($strPathType, $strPath);
my $strErrorPrefix = "File->exists";
my $strTrace = "${strPathType}:${strPathOp}";
my $strDebug = "${strPathType}:${strPathOp}";
# Run remotely
if ($self->is_remote($strPathType))
{
# Build param hash
my %oParamHash;
$oParamHash{path} = ${strPathOp};
# Build trace string
$strTrace = "${strErrorPrefix}: remote (" . $self->{oRemote}->command_param_string(\%oParamHash) . "): " . $strTrace;
&log(TRACE, $strTrace);
$strDebug = "${strErrorPrefix}: remote (" . $self->{oRemote}->command_param_string(\%oParamHash) . "): " . $strDebug;
&log(DEBUG, $strDebug);
# Execute the command
$bExists = $self->{oRemote}->command_execute("exists", \%oParamHash, $strTrace) eq "Y";
$bExists = $self->{oRemote}->command_execute("exists", \%oParamHash, $strDebug) eq "Y";
}
# Run locally
else
{
&log(TRACE, "${strErrorPrefix}: ${strTrace}");
$strDebug = "${strErrorPrefix}: local: " . $strDebug;
&log(DEBUG, ${strDebug});
# Stat the file/path to determine if it exists
my $oStat = lstat($strPathOp);
@ -1169,7 +1170,7 @@ sub exists
}
else
{
confess &log(ERROR, "${strErrorPrefix}: ${strTrace}: " . $!, COMMAND_ERR_FILE_READ);
confess &log(ERROR, "${strDebug}: " . $!, COMMAND_ERR_FILE_READ);
}
}

View File

@ -64,7 +64,7 @@ sub BUILD
# $strOptionSSHCompression = "Compression=yes";
# }
# &log(TRACE, "connecting to remote ssh host " . $self->{strHost});
&log(TRACE, "connecting to remote ssh host " . $self->{strHost});
# Make SSH connection
$self->{oSSH} = Net::OpenSSH->new($self->{strHost}, timeout => 300, user => $self->{strUser},
@ -74,7 +74,7 @@ sub BUILD
# Execute remote command
($self->{hIn}, $self->{hOut}, $self->{hErr}, $self->{pId}) = $self->{oSSH}->open3($self->{strCommand});
$self->greeting_read();
}
}
@ -129,9 +129,9 @@ sub string_write
my $self = shift;
my $hOut = shift;
my $strBuffer = shift;
$strBuffer =~ s/\n/\n\./g;
if (!syswrite($hOut, "." . $strBuffer))
{
confess "unable to write string";
@ -145,13 +145,13 @@ sub error_write
{
my $self = shift;
my $oMessage = shift;
my $iCode;
my $strMessage;
if (blessed($oMessage))
{
if ($oMessage->isa("BackRest::Exception"))
if ($oMessage->isa("BackRest::Exception"))
{
$iCode = $oMessage->code();
$strMessage = $oMessage->message();
@ -196,9 +196,9 @@ sub output_read
if ($strLine =~ /^ERROR.*/)
{
$bError = true;
$iErrorCode = (split(' ', trim($strLine)))[1];
last;
}
@ -220,7 +220,7 @@ sub output_write
{
my $self = shift;
my $strOutput = shift;
$self->string_write(*STDOUT, $strOutput);
if (!syswrite(*STDOUT, "\nOK\n"))
@ -236,9 +236,9 @@ sub command_param_string
{
my $self = shift;
my $oParamHashRef = shift;
my $strParamList;
foreach my $strParam (sort(keys $oParamHashRef))
{
$strParamList .= (defined($strParamList) ? "," : "") . "${strParam}=" .
@ -264,7 +264,7 @@ sub command_read
while ($strLine = readline(*STDIN))
{
$strLine = trim($strLine);
if (!defined($strCommand))
{
if ($strLine =~ /:$/)
@ -285,19 +285,19 @@ sub command_read
{
last;
}
my $iPos = index($strLine, "=");
if ($iPos == -1)
{
confess "param \"${strLine}\" is missing = character";
}
my $strParam = substr($strLine, 0, $iPos);
my $strValue = substr($strLine, $iPos + 1);
${$oParamHashRef}{"${strParam}"} = ${strValue};
# print "${strParam}=${strValue}\n";
}
}
@ -319,7 +319,7 @@ sub command_write
if (defined($oParamRef))
{
$strOutput = "${strCommand}:\n";
foreach my $strParam (sort(keys $oParamRef))
{
if ($strParam =~ /=/)
@ -333,7 +333,7 @@ sub command_write
{
confess &log(ASSERT, "param \"${strParam}\" value cannot end with LF");
}
if (defined(${strValue}))
{
$strOutput .= "${strParam}=${strValue}\n";
@ -343,7 +343,7 @@ sub command_write
$strOutput .= "end";
}
# &log(TRACE, "Remote->command_write:\n" . trim($strOutput));
&log(TRACE, "Remote->command_write:\n" . trim($strOutput));
if (!syswrite($self->{hIn}, "${strOutput}\n"))
{
@ -362,16 +362,16 @@ sub command_execute
my $strErrorPrefix = shift;
$self->command_write($strCommand, $strOptions);
my ($strOutput, $bError, $iErrorCode) = $self->output_read();
# Capture any errors
if ($bError)
{
confess &log(ERROR, (defined($strErrorPrefix) ? "${strErrorPrefix}" : "") .
(defined($strOutput) ? ": ${strOutput}" : ""), $iErrorCode);
}
return $strOutput;
}

View File

@ -341,16 +341,23 @@ sub log
if ($strLevel eq "TRACE")
{
$strMessageFormat =~ s/\n/\n /g;
$strMessageFormat = " " . $strMessageFormat;
}
elsif ($strLevel eq "DEBUG")
{
$strMessageFormat =~ s/\n/\n /g;
$strMessageFormat = " " . $strMessageFormat;
}
else
{
$strMessageFormat =~ s/\n/\n /g;
}
$strMessageFormat = sprintf("%4d-%02d-%02d %02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec) .
(" " x (7 - length($strLevel))) . "${strLevel} " . (" " x (2 - length(threads->tid()))) .
threads->tid() . ": ${strMessageFormat}\n";
threads->tid() . ": ${strMessageFormat}" .
(defined($iCode) ? " (code ${iCode})" : "") . "\n";
if ($oLogLevelRank{"${strLevel}"}{rank} <= $oLogLevelRank{"${strLogLevelConsole}"}{rank})
{

View File

@ -753,10 +753,10 @@ sub BackRestFileTest
my $oMessage = $@;
my $iCode;
my $strMessage;
if (blessed($oMessage))
{
if ($oMessage->isa("BackRest::Exception"))
if ($oMessage->isa("BackRest::Exception"))
{
$iCode = $oMessage->code();
$strMessage = $oMessage->message();
@ -770,12 +770,12 @@ sub BackRestFileTest
{
$strMessage = $oMessage;
}
if ($bError && defined($iCode) && $iCode == COMMAND_ERR_FILE_READ)
{
next;
}
confess "error raised: " . $strMessage . "\n";
}
}