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 ) { public function fileLevel( $fileLevel ) {
@ -288,7 +288,7 @@ class Logger {
$this->openFile(); $this->openFile();
} }
} }
return( $this->fileLevel ); return $this->fileLevel;
} }
public function weblogLevel( $weblogLevel ) { public function weblogLevel( $weblogLevel ) {
@ -303,7 +303,7 @@ class Logger {
$this->weblogLevel = $weblogLevel; $this->weblogLevel = $weblogLevel;
} }
} }
return( $this->weblogLevel ); return $this->weblogLevel;
} }
public function syslogLevel( $syslogLevel ) { public function syslogLevel( $syslogLevel ) {
@ -317,30 +317,31 @@ class Logger {
$this->openSyslog(); $this->openSyslog();
} }
} }
return( $this->syslogLevel ); return $this->syslogLevel;
} }
private function openSyslog() { private function openSyslog() {
openlog( $this->id, LOG_PID|LOG_NDELAY, LOG_LOCAL1 ); openlog($this->id, LOG_PID|LOG_NDELAY, LOG_LOCAL1);
} }
private function closeSyslog() { private function closeSyslog() {
closelog(); closelog();
} }
private function logFile( $logFile ) { private function logFile($logFile) {
if ( preg_match( '/^(.+)\+$/', $logFile, $matches ) ) if ( preg_match('/^(.+)\+$/', $logFile, $matches) ) {
$this->logFile = $matches[1].'.'.getmypid(); $this->logFile = $matches[1].'.'.getmypid();
else } else {
$this->logFile = $logFile; $this->logFile = $logFile;
}
} }
private function openFile() { private function openFile() {
if ( !$this->useErrorLog ) { if ( !$this->useErrorLog ) {
if ( $this->logFd = fopen( $this->logFile, 'a+' ) ) { if ( $this->logFd = fopen($this->logFile, 'a+') ) {
if ( strnatcmp( phpversion(), '5.2.0' ) >= 0 ) { if ( strnatcmp(phpversion(), '5.2.0') >= 0 ) {
$error = error_get_last(); $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; $this->fileLevel = self::NOLOG;
} }
@ -349,73 +350,80 @@ class Logger {
private function closeFile() { private function closeFile() {
if ( $this->logFd ) if ( $this->logFd )
fclose( $this->logFd ); fclose($this->logFd);
} }
public function logPrint( $level, $string, $file=NULL, $line=NULL ) { public function logPrint( $level, $string, $file=NULL, $line=NULL ) {
if ( $level <= $this->effectiveLevel ) { if ( $level > $this->effectiveLevel ) {
$string = preg_replace( '/[\r\n]+$/', '', $string ); return;
$code = self::$codes[$level]; }
$time = gettimeofday(); $string = preg_replace('/[\r\n]+$/', '', $string);
$message = sprintf( '%s.%06d %s[%d].%s [%s]', strftime( '%x %H:%M:%S', $time['sec'] ), $time['usec'], $this->id, getmypid(), $code, $string ); $code = self::$codes[$level];
if ( is_null($file) ) { $time = gettimeofday();
if ( $this->useErrorLog || $this->databaseLevel > self::NOLOG ) { $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);
$backTrace = debug_backtrace();
$file = $backTrace[1]['file'];
$line = $backTrace[1]['line'];
if ( $this->hasTerm )
$rootPath = getcwd();
else
$rootPath = $_SERVER['DOCUMENT_ROOT'];
$file = preg_replace( '/^'.addcslashes($rootPath,'/').'\/?/', '', $file );
}
}
if ( $this->useErrorLog ) if ( is_null($file) ) {
$message .= ' at '.$file.' line '.$line; if ( $this->useErrorLog || $this->databaseLevel > self::NOLOG ) {
else $backTrace = debug_backtrace();
$message = $message; $file = $backTrace[1]['file'];
$line = $backTrace[1]['line'];
if ( $level <= $this->termLevel )
if ( $this->hasTerm ) if ( $this->hasTerm )
print( $message."\n" ); $rootPath = getcwd();
else else
print( preg_replace( "/\n/", '<br/>', htmlspecialchars($message) ).'<br/>' ); $rootPath = $_SERVER['DOCUMENT_ROOT'];
$file = preg_replace('/^'.addcslashes($rootPath,'/').'\/?/', '', $file);
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 );
}
}
} elseif ( $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 ) );
} catch(PDOException $ex) {
$this->databaseLevel = self::NOLOG;
Error("Can't write log entry '$sql': ". $ex->getMessage());
}
} }
// This has to be last as trigger_error can be fatal }
if ( $level <= $this->weblogLevel ) {
if ( $this->useErrorLog ) if ( $this->useErrorLog ) {
error_log( $message, 0 ); $message .= ' at '.$file.' line '.$line;
else } else {
trigger_error( $message, self::$phpErrorLevels[$level] ); $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);
}
}
} 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));
} catch(PDOException $ex) {
$this->databaseLevel = self::NOLOG;
Error("Can't write log entry '$sql': ". $ex->getMessage());
}
}
// 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]);
} }
} }
} }