diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index c376da32463..a5555b0511b 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -2018,7 +2018,10 @@ function dol_syslog($message, $level = LOG_INFO, $ident = 0, $suffixinfilename = // If adding log inside HTML page is required if ((!empty($_REQUEST['logtohtml']) && getDolGlobalString('MAIN_ENABLE_LOG_TO_HTML')) || (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 @@ -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), 'level' => $level, '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 @@ -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) $data['ip'] = $_SERVER['SERVER_ADDR']; } 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). - $data['ip'] = $_SERVER['COMPUTERNAME'].(empty($_SERVER['USERNAME']) ? '' : '@'.$_SERVER['USERNAME']); + // 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']; + } 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'])) { - // 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). - $data['ip'] = '???@'.$_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 defines it). + $data['osuser'] = $_SERVER['LOGNAME']; } // Loop on each log handler and send output diff --git a/htdocs/core/modules/syslog/mod_syslog_file.php b/htdocs/core/modules/syslog/mod_syslog_file.php index 5468923120e..ffeb142c57a 100644 --- a/htdocs/core/modules/syslog/mod_syslog_file.php +++ b/htdocs/core/modules/syslog/mod_syslog_file.php @@ -138,8 +138,6 @@ class mod_syslog_file extends LogHandler implements LogHandlerInterface */ public function export($content, $suffixinfilename = '') { - global $conf, $dolibarr_main_prod; - if (getDolGlobalString('MAIN_SYSLOG_DISABLE_FILE')) { return; // Global option to disable output of this handler } @@ -155,6 +153,7 @@ class mod_syslog_file extends LogHandler implements LogHandlerInterface if (!$filefd) { if (!defined('SYSLOG_FILE_NO_ERROR') || !constant('SYSLOG_FILE_NO_ERROR')) { + global $dolibarr_main_prod; // Do not break dolibarr usage if log fails //throw new Exception('Failed to open log file '.basename($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 - $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"); fclose($filefd); dolChmod($logfile); diff --git a/htdocs/debugbar/class/DataCollector/DolLogsCollector.php b/htdocs/debugbar/class/DataCollector/DolLogsCollector.php index 2fa085be1af..2013540b862 100644 --- a/htdocs/debugbar/class/DataCollector/DolLogsCollector.php +++ b/htdocs/debugbar/class/DataCollector/DolLogsCollector.php @@ -54,8 +54,6 @@ class DolLogsCollector extends MessagesCollector */ public function __construct($path = null, $name = 'logs') { - global $conf; - parent::__construct($name); $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 * @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}.*/"; $log_levels = $this->getLevels(); + $matches = array(); preg_match_all($pattern, $file, $matches); $log = array(); foreach ($matches as $lines) {