NEW Add the process and user ID into log

This commit is contained in:
Laurent Destailleur
2024-03-26 14:14:11 +01:00
parent 747a5c4114
commit caf6367a4e
3 changed files with 25 additions and 12 deletions

View File

@@ -2018,7 +2018,10 @@ function dol_syslog($message, $level = LOG_INFO, $ident = 0, $suffixinfilename =
// If adding log inside HTML page is required // If adding log inside HTML page is required
if ((!empty($_REQUEST['logtohtml']) && getDolGlobalString('MAIN_ENABLE_LOG_TO_HTML')) if ((!empty($_REQUEST['logtohtml']) && getDolGlobalString('MAIN_ENABLE_LOG_TO_HTML'))
|| (is_object($user) && $user->hasRight('debugbar', 'read') && is_object($debugbar))) { || (is_object($user) && $user->hasRight('debugbar', 'read') && is_object($debugbar))) {
$conf->logbuffer[] = dol_print_date(time(), "%Y-%m-%d %H:%M:%S")." ".$logLevels[$level]." ".$message; $ospid = sprintf("%7s", dol_trunc(getmypid(), 7, 'right', 'UTF-8', 1));
$osuser = " ".sprintf("%6s", dol_trunc(function_exists('posix_getuid') ? posix_getuid() : '', 6, 'right', 'UTF-8', 1));
$conf->logbuffer[] = dol_print_date(time(), "%Y-%m-%d %H:%M:%S")." ".sprintf("%-7s", $logLevels[$level])." ".$ospid." ".$osuser." ".$message;
} }
//TODO: Remove this. MAIN_ENABLE_LOG_INLINE_HTML should be deprecated and use a log handler dedicated to HTML output //TODO: Remove this. MAIN_ENABLE_LOG_INLINE_HTML should be deprecated and use a log handler dedicated to HTML output
@@ -2034,7 +2037,9 @@ function dol_syslog($message, $level = LOG_INFO, $ident = 0, $suffixinfilename =
'script' => (isset($_SERVER['PHP_SELF']) ? basename($_SERVER['PHP_SELF'], '.php') : false), 'script' => (isset($_SERVER['PHP_SELF']) ? basename($_SERVER['PHP_SELF'], '.php') : false),
'level' => $level, 'level' => $level,
'user' => ((is_object($user) && $user->id) ? $user->login : false), 'user' => ((is_object($user) && $user->id) ? $user->login : false),
'ip' => false 'ip' => false,
'osuser' => function_exists('posix_getuid') ? posix_getuid() : false,
'ospid' => getmypid() // on linux, max value is defined into cat /proc/sys/kernel/pid_max
); );
$remoteip = getUserRemoteIP(); // Get ip when page run on a web server $remoteip = getUserRemoteIP(); // Get ip when page run on a web server
@@ -2050,11 +2055,18 @@ function dol_syslog($message, $level = LOG_INFO, $ident = 0, $suffixinfilename =
// This is when PHP session is ran inside a web server but not inside a client request (example: init code of apache) // This is when PHP session is ran inside a web server but not inside a client request (example: init code of apache)
$data['ip'] = $_SERVER['SERVER_ADDR']; $data['ip'] = $_SERVER['SERVER_ADDR'];
} elseif (!empty($_SERVER['COMPUTERNAME'])) { } elseif (!empty($_SERVER['COMPUTERNAME'])) {
// This is when PHP session is ran outside a web server, like from Windows command line (Not always defined, but useful if OS defined it). // This is when PHP session is ran outside a web server, like from Windows command line (Not always defined, but useful if OS defines it).
$data['ip'] = $_SERVER['COMPUTERNAME'].(empty($_SERVER['USERNAME']) ? '' : '@'.$_SERVER['USERNAME']); $data['ip'] = $_SERVER['COMPUTERNAME'];
} else {
$data['ip'] = '???';
}
if (!empty($_SERVER['USERNAME'])) {
// This is when PHP session is ran outside a web server, like from Linux command line (Not always defined, but useful if OS defines it).
$data['osuser'] = $_SERVER['USERNAME'];
} elseif (!empty($_SERVER['LOGNAME'])) { } elseif (!empty($_SERVER['LOGNAME'])) {
// This is when PHP session is ran outside a web server, like from Linux command line (Not always defined, but useful if OS defined it). // This is when PHP session is ran outside a web server, like from Linux command line (Not always defined, but useful if OS defines it).
$data['ip'] = '???@'.$_SERVER['LOGNAME']; $data['osuser'] = $_SERVER['LOGNAME'];
} }
// Loop on each log handler and send output // Loop on each log handler and send output

View File

@@ -138,8 +138,6 @@ class mod_syslog_file extends LogHandler implements LogHandlerInterface
*/ */
public function export($content, $suffixinfilename = '') public function export($content, $suffixinfilename = '')
{ {
global $conf, $dolibarr_main_prod;
if (getDolGlobalString('MAIN_SYSLOG_DISABLE_FILE')) { if (getDolGlobalString('MAIN_SYSLOG_DISABLE_FILE')) {
return; // Global option to disable output of this handler return; // Global option to disable output of this handler
} }
@@ -155,6 +153,7 @@ class mod_syslog_file extends LogHandler implements LogHandlerInterface
if (!$filefd) { if (!$filefd) {
if (!defined('SYSLOG_FILE_NO_ERROR') || !constant('SYSLOG_FILE_NO_ERROR')) { if (!defined('SYSLOG_FILE_NO_ERROR') || !constant('SYSLOG_FILE_NO_ERROR')) {
global $dolibarr_main_prod;
// Do not break dolibarr usage if log fails // Do not break dolibarr usage if log fails
//throw new Exception('Failed to open log file '.basename($logfile)); //throw new Exception('Failed to open log file '.basename($logfile));
print 'Failed to open log file '.($dolibarr_main_prod ? basename($logfile) : $logfile); print 'Failed to open log file '.($dolibarr_main_prod ? basename($logfile) : $logfile);
@@ -179,7 +178,10 @@ class mod_syslog_file extends LogHandler implements LogHandlerInterface
} }
// @phan-suppress-next-line PhanParamSuspiciousOrder // @phan-suppress-next-line PhanParamSuspiciousOrder
$message = dol_print_date(dol_now('gmt'), 'standard', 'gmt').$delay." ".sprintf("%-7s", $logLevels[$content['level']])." ".sprintf("%-15s", $content['ip'])." ".($this->ident > 0 ? str_pad('', $this->ident, ' ') : '').$content['message']; $message = dol_print_date(dol_now('gmt'), 'standard', 'gmt').$delay." ".sprintf("%-7s", $logLevels[$content['level']])." ".sprintf("%-15s", $content['ip']);
$message .= " ".sprintf("%7s", dol_trunc($content['ospid'], 7, 'right', 'UTF-8', 1));
$message .= " ".sprintf("%6s", dol_trunc($content['osuser'], 6, 'right', 'UTF-8', 1));
$message .= " ".($this->ident > 0 ? str_pad('', $this->ident, ' ') : '').$content['message'];
fwrite($filefd, $message."\n"); fwrite($filefd, $message."\n");
fclose($filefd); fclose($filefd);
dolChmod($logfile); dolChmod($logfile);

View File

@@ -54,8 +54,6 @@ class DolLogsCollector extends MessagesCollector
*/ */
public function __construct($path = null, $name = 'logs') public function __construct($path = null, $name = 'logs')
{ {
global $conf;
parent::__construct($name); parent::__construct($name);
$this->nboflines = 0; $this->nboflines = 0;
@@ -192,7 +190,7 @@ class DolLogsCollector extends MessagesCollector
} }
/** /**
* Search a string for log entries * Search a string for log entries into the log file. Used when debug bar scan log file (very slow).
* *
* @param string $file File * @param string $file File
* @return array Lines of logs * @return array Lines of logs
@@ -201,6 +199,7 @@ class DolLogsCollector extends MessagesCollector
{ {
$pattern = "/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*/"; $pattern = "/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*/";
$log_levels = $this->getLevels(); $log_levels = $this->getLevels();
$matches = array();
preg_match_all($pattern, $file, $matches); preg_match_all($pattern, $file, $matches);
$log = array(); $log = array();
foreach ($matches as $lines) { foreach ($matches as $lines) {