add remote_IP to log entries on php side

This commit is contained in:
Isaac Connor 2020-01-07 17:08:29 -05:00
parent 1865df3fe0
commit 0571909d05
1 changed files with 77 additions and 69 deletions

View File

@ -274,7 +274,7 @@ class Logger {
}
}
}
return( $this->databaseLevel );
return $this->databaseLevel;
}
public function fileLevel( $fileLevel ) {
@ -288,7 +288,7 @@ class Logger {
$this->openFile();
}
}
return( $this->fileLevel );
return $this->fileLevel;
}
public function weblogLevel( $weblogLevel ) {
@ -303,7 +303,7 @@ class Logger {
$this->weblogLevel = $weblogLevel;
}
}
return( $this->weblogLevel );
return $this->weblogLevel;
}
public function syslogLevel( $syslogLevel ) {
@ -317,30 +317,31 @@ class Logger {
$this->openSyslog();
}
}
return( $this->syslogLevel );
return $this->syslogLevel;
}
private function openSyslog() {
openlog( $this->id, LOG_PID|LOG_NDELAY, LOG_LOCAL1 );
openlog($this->id, LOG_PID|LOG_NDELAY, LOG_LOCAL1);
}
private function closeSyslog() {
closelog();
}
private function logFile( $logFile ) {
if ( preg_match( '/^(.+)\+$/', $logFile, $matches ) )
private function logFile($logFile) {
if ( preg_match('/^(.+)\+$/', $logFile, $matches) ) {
$this->logFile = $matches[1].'.'.getmypid();
else
} else {
$this->logFile = $logFile;
}
}
private function openFile() {
if ( !$this->useErrorLog ) {
if ( $this->logFd = fopen( $this->logFile, 'a+' ) ) {
if ( strnatcmp( phpversion(), '5.2.0' ) >= 0 ) {
if ( $this->logFd = fopen($this->logFile, 'a+') ) {
if ( strnatcmp(phpversion(), '5.2.0') >= 0 ) {
$error = error_get_last();
trigger_error( "Can't open log file '$logFile': ".$error['message'].' @ '.$error['file'].'/'.$error['line'], E_USER_ERROR );
trigger_error("Can't open log file '$logFile': ".$error['message'].' @ '.$error['file'].'/'.$error['line'], E_USER_ERROR);
}
$this->fileLevel = self::NOLOG;
}
@ -349,16 +350,19 @@ class Logger {
private function closeFile() {
if ( $this->logFd )
fclose( $this->logFd );
fclose($this->logFd);
}
public function logPrint( $level, $string, $file=NULL, $line=NULL ) {
if ( $level <= $this->effectiveLevel ) {
$string = preg_replace( '/[\r\n]+$/', '', $string );
if ( $level > $this->effectiveLevel ) {
return;
}
$string = preg_replace('/[\r\n]+$/', '', $string);
$code = self::$codes[$level];
$time = gettimeofday();
$message = sprintf( '%s.%06d %s[%d].%s [%s]', strftime( '%x %H:%M:%S', $time['sec'] ), $time['usec'], $this->id, getmypid(), $code, $string );
$message = sprintf('%s.%06d %s[%d].%s [%s] [%s]', strftime('%x %H:%M:%S', $time['sec']), $time['usec'], $this->id, getmypid(), $code, $_SERVER['REMOTE_ADDR'], $string);
if ( is_null($file) ) {
if ( $this->useErrorLog || $this->databaseLevel > self::NOLOG ) {
@ -369,42 +373,46 @@ class Logger {
$rootPath = getcwd();
else
$rootPath = $_SERVER['DOCUMENT_ROOT'];
$file = preg_replace( '/^'.addcslashes($rootPath,'/').'\/?/', '', $file );
$file = preg_replace('/^'.addcslashes($rootPath,'/').'\/?/', '', $file);
}
}
if ( $this->useErrorLog )
$message .= ' at '.$file.' line '.$line;
else
$message = $message;
if ( $level <= $this->termLevel )
if ( $this->hasTerm )
print( $message."\n" );
else
print( preg_replace( "/\n/", '<br/>', htmlspecialchars($message) ).'<br/>' );
if ( $level <= $this->fileLevel )
if ( $this->useErrorLog ) {
if ( !error_log( $message."\n", 3, $this->logFile ) ) {
if ( strnatcmp( phpversion(), '5.2.0' ) >= 0 ) {
$message .= ' at '.$file.' line '.$line;
} else {
$message = $message;
}
if ( $level <= $this->termLevel ) {
if ( $this->hasTerm )
print($message."\n");
else
print(preg_replace("/\n/", '<br/>', htmlspecialchars($message) ).'<br/>');
}
if ( $level <= $this->fileLevel ) {
if ( $this->useErrorLog ) {
if ( !error_log($message."\n", 3, $this->logFile) ) {
if ( strnatcmp(phpversion(), '5.2.0') >= 0 ) {
$error = error_get_last();
trigger_error( "Can't write to log file '".$this->logFile."': ".$error['message'].' @ '.$error['file'].'/'.$error['line'], E_USER_ERROR );
trigger_error("Can't write to log file '".$this->logFile."': ".$error['message'].' @ '.$error['file'].'/'.$error['line'], E_USER_ERROR);
}
}
} elseif ( $this->logFd ) {
fprintf( $this->logFd, $message."\n" );
} else if ( $this->logFd ) {
fprintf($this->logFd, $message."\n");
}
}
$message = $code.' ['.$string.']';
if ( $level <= $this->syslogLevel )
syslog( self::$syslogPriorities[$level], $message );
if ( $level <= $this->databaseLevel ) {
try {
global $dbConn;
$sql = 'INSERT INTO Logs ( TimeKey, Component, Pid, Level, Code, Message, File, Line ) values ( ?, ?, ?, ?, ?, ?, ?, ? )';
$stmt = $dbConn->prepare( $sql );
$result = $stmt->execute( array( sprintf( '%d.%06d', $time['sec'], $time['usec'] ), $this->id, getmypid(), $level, $code, $string, $file, $line ) );
$sql = 'INSERT INTO Logs ( TimeKey, Component, Pid, Level, Code, Message, File, Line ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? )';
$stmt = $dbConn->prepare($sql);
$result = $stmt->execute(array(sprintf('%d.%06d', $time['sec'], $time['usec']), $this->id, getmypid(), $level, $code, $string, $file, $line));
} catch(PDOException $ex) {
$this->databaseLevel = self::NOLOG;
Error("Can't write log entry '$sql': ". $ex->getMessage());
@ -412,10 +420,10 @@ class Logger {
}
// This has to be last as trigger_error can be fatal
if ( $level <= $this->weblogLevel ) {
if ( $this->useErrorLog )
error_log( $message, 0 );
else
trigger_error( $message, self::$phpErrorLevels[$level] );
if ( $this->useErrorLog ) {
error_log($message, 0);
} else {
trigger_error($message, self::$phpErrorLevels[$level]);
}
}
}