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,7 +317,7 @@ class Logger {
$this->openSyslog();
}
}
return( $this->syslogLevel );
return $this->syslogLevel;
}
private function openSyslog() {
@ -329,11 +329,12 @@ class Logger {
}
private function logFile($logFile) {
if ( preg_match( '/^(.+)\+$/', $logFile, $matches ) )
if ( preg_match('/^(.+)\+$/', $logFile, $matches) ) {
$this->logFile = $matches[1].'.'.getmypid();
else
} else {
$this->logFile = $logFile;
}
}
private function openFile() {
if ( !$this->useErrorLog ) {
@ -353,12 +354,15 @@ class Logger {
}
public function logPrint( $level, $string, $file=NULL, $line=NULL ) {
if ( $level <= $this->effectiveLevel ) {
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 ) {
@ -373,18 +377,20 @@ class Logger {
}
}
if ( $this->useErrorLog )
if ( $this->useErrorLog ) {
$message .= ' at '.$file.' line '.$line;
else
} else {
$message = $message;
}
if ( $level <= $this->termLevel )
if ( $level <= $this->termLevel ) {
if ( $this->hasTerm )
print($message."\n");
else
print(preg_replace("/\n/", '<br/>', htmlspecialchars($message) ).'<br/>');
}
if ( $level <= $this->fileLevel )
if ( $level <= $this->fileLevel ) {
if ( $this->useErrorLog ) {
if ( !error_log($message."\n", 3, $this->logFile) ) {
if ( strnatcmp(phpversion(), '5.2.0') >= 0 ) {
@ -395,14 +401,16 @@ class Logger {
} 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 ( ?, ?, ?, ?, ?, ?, ?, ? )';
$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) {
@ -412,9 +420,9 @@ class Logger {
}
// 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);
else
} else {
trigger_error($message, self::$phpErrorLevels[$level]);
}
}