Qual: Improve dol_syslog (#30782)

* Qual: 🛟 Fix reference to $db with $this->db

# Qual: reference to $db with $this->db

$db was undeclared, use $this->db.

* Fix suspected issue with environment max size and md5sum working on limited files

* Qual: Fix LDAP typing

* Qual: Improve dol_syslog

# Qual: Improve dol_syslog

The changes:
- Keep the log file open for less time (better for concurrency);
- Open the log file only to append data (no internal preparation for writing);
- Avoids re-assigning a constant array;
- Add PHPDoc information about the array parameters and return values;
- May reduce the computation of global settings;
- Result in a slight performance improvement.
This commit is contained in:
MDW 2024-08-29 08:49:27 +02:00 committed by GitHub
parent 5f968dcebe
commit e6630cd7d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 63 additions and 41 deletions

View file

@ -129,6 +129,14 @@ if [ "$DB" = 'mysql' ] || [ "$DB" = 'mariadb' ] || [ "$DB" = 'postgresql' ]; the
${SUDO} "${MYSQL}" -u "$DB_ROOT" -h 127.0.0.1 $PASS_OPT -e 'FLUSH PRIVILEGES;'
sum=$(find "${TRAVIS_BUILD_DIR}/htdocs/install" -type f -exec md5sum {} + | LC_ALL=C sort | md5sum)
cnt=$(find "${TRAVIS_BUILD_DIR}/htdocs/install" -type f -exec md5sum {} + | wc)
echo "OLDSUM $sum COUNT:$cnt"
# shellcheck disable=2046
sum=$(md5sum $(find "${TRAVIS_BUILD_DIR}/htdocs/install" -type f) | LC_ALL=C sort | md5sum)
# shellcheck disable=2046
cnt=$(md5sum $(find "${TRAVIS_BUILD_DIR}/htdocs/install" -type f) | wc)
echo "NEWSUM $sum COUNT:$cnt"
load_cache=0
if [ -r "$DB_CACHE_FILE".md5 ] && [ -r "$DB_CACHE_FILE" ] && [ -x "$(which "${MYSQLDUMP}")" ] ; then
cache_sum="$(<"$DB_CACHE_FILE".md5)"

View file

@ -540,6 +540,7 @@ class Ldap
}
} else {
if (is_resource($this->connection)) {
// @phan-suppress-next-line PhanTypeMismatchArgumentInternalReal
$this->result = @ldap_unbind($this->connection);
}
}

View file

@ -255,7 +255,7 @@ function check_user_password_ldap($usertotest, $passwordtotest, $entitytotest)
$ldap->ldapErrorText = ldap_error($ldap->connection);
dol_syslog("functions_ldap::check_user_password_ldap ".$ldap->ldapErrorCode." ".$ldap->ldapErrorText);
} catch (Throwable $exception) {
$ldap->ldapErrorCode = '';
$ldap->ldapErrorCode = 0;
$ldap->ldapErrorText = '';
dol_syslog('functions_ldap::check_user_password_ldap '.$exception, LOG_WARNING);
}

View file

@ -10,7 +10,13 @@ require_once DOL_DOCUMENT_ROOT.'/core/modules/syslog/logHandler.php';
*/
class mod_syslog_file extends LogHandler
{
/**
* @var string
*/
public $code = 'file';
/**
* @var float|int Last log time, used to compute delay
*/
public $lastTime = 0;
/**
@ -50,7 +56,7 @@ class mod_syslog_file extends LogHandler
/**
* Is the logger active ?
*
* @return int 1 if logger enabled
* @return int<0,1> If logger enabled
*/
public function isActive()
{
@ -60,7 +66,7 @@ class mod_syslog_file extends LogHandler
/**
* Return array of configuration data
*
* @return array Return array of configuration data
* @return array<array{name:string,constant:string,default:string,css:string}> Return array of configuration data
*/
public function configure()
{
@ -128,10 +134,23 @@ class mod_syslog_file extends LogHandler
return $suffixinfilename ? preg_replace('/\.log$/i', $suffixinfilename.'.log', $tmp) : $tmp;
}
// @phan-suppress-next-line PhanPluginDuplicateArrayKey
const DOL_LOG_LEVELS = array(
LOG_EMERG => 'EMERG',
LOG_ALERT => 'ALERT',
LOG_CRIT => 'CRIT',
LOG_ERR => 'ERR',
LOG_WARNING => 'WARNING',
LOG_NOTICE => 'NOTICE',
LOG_INFO => 'INFO',
LOG_DEBUG => 'DEBUG'
);
/**
* Export the message
*
* @param array $content Array containing the info about the message
* @param array{level:int,ip:string,ospid:string,osuser:string,message:string} $content Array containing the info about the message
* @param string $suffixinfilename When output is a file, append this suffix into default log filename.
* @return void
* @phan-suppress PhanPluginDuplicateArrayKey
@ -142,50 +161,44 @@ class mod_syslog_file extends LogHandler
return; // Global option to disable output of this handler
}
// Prepare log message
$delay = "";
if (getDolGlobalString('MAIN_SYSLOG_SHOW_DELAY')) {
$now = microtime(true);
$delay = " ".sprintf("%05.3f", $this->lastTime != 0 ? $now - $this->lastTime : 0);
$this->lastTime = $now;
}
$message = dol_print_date(dol_now('gmt'), 'standard', 'gmt').$delay." ".sprintf("%-7s", self::DOL_LOG_LEVELS[$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));
// @phan-suppress-next-line PhanParamSuspiciousOrder
$message .= " ".($this->ident > 0 ? str_pad('', ((int) $this->ident), ' ') : '').$content['message'];
$message .= "\n";
// Write log message
$logfile = $this->getFilename($suffixinfilename);
// Test constant SYSLOG_FILE_NO_ERROR (should stay a constant defined with define('SYSLOG_FILE_NO_ERROR',1);
$result = false;
if (defined('SYSLOG_FILE_NO_ERROR')) {
$filefd = @fopen($logfile, 'a+');
$filefd = @fopen($logfile, "a");
} else {
$filefd = fopen($logfile, 'a+');
$filefd = fopen($logfile, "a");
}
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);
}
} else {
// @phan-suppress-next-line PhanPluginDuplicateArrayKey
$logLevels = array(
LOG_EMERG => 'EMERG',
LOG_ALERT => 'ALERT',
LOG_CRIT => 'CRIT',
LOG_ERR => 'ERR',
LOG_WARNING => 'WARNING',
LOG_NOTICE => 'NOTICE',
LOG_INFO => 'INFO',
LOG_DEBUG => 'DEBUG'
);
$delay = "";
if (getDolGlobalString('MAIN_SYSLOG_SHOW_DELAY')) {
$now = microtime(true);
$delay = " ".sprintf("%05.3f", $this->lastTime != 0 ? $now - $this->lastTime : 0);
$this->lastTime = $now;
}
// @phan-suppress-next-line PhanParamSuspiciousOrder
$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(((string) ''), ((int) $this->ident), ((string) ' ')) : '').$content['message'];
fwrite($filefd, $message."\n");
if ($filefd !== false) {
$result = fwrite($filefd, $message);
fclose($filefd);
dolChmod($logfile);
}
if ($result === false && (!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 write to log file '.($dolibarr_main_prod ? basename($logfile) : $logfile);
}
}
}

View file

@ -707,7 +707,7 @@ class WebsitePage extends CommonObject
if ($istranslation) {
if (is_null($website)) {
$website = new Website($db);
$website = new Website($this->db);
}
$website->fetch($object->fk_website);