You've already forked pgbackrest
							
							
				mirror of
				https://github.com/pgbackrest/pgbackrest.git
				synced 2025-10-30 23:37:45 +02:00 
			
		
		
		
	Improved logging.
This commit is contained in:
		| @@ -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($@); | ||||
|   | ||||
| @@ -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); | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|   | ||||
| @@ -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; | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -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}) | ||||
|     { | ||||
|   | ||||
| @@ -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"; | ||||
|                     } | ||||
|                 } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user