Skip to content

Commit

Permalink
Fix output message in log rotation job
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Mar 21, 2018
1 parent 7e5c3cc commit 200fd88
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
24 changes: 14 additions & 10 deletions htdocs/core/class/utils.class.php
Expand Up @@ -676,7 +676,7 @@ function generateDoc($module)
* This saves syslog files and compresses older ones
* Used from cronjob
*
* @return int 0 if OK, < 0 if KO
* @return int 0 if OK, < 0 if KO
*/
function compressSyslogs() {
global $conf;
Expand All @@ -686,14 +686,15 @@ function compressSyslogs() {
}

if(! function_exists('gzopen')) {
$this->error = 'Support for gzopen not available in this PHP';
return -1;
}

dol_include_once('/core/lib/files.lib.php');

$nbSaves = ! empty($conf->global->SYSLOG_FILE_SAVES) ? intval($conf->global->SYSLOG_FILE_SAVES) : 14;

if(empty($conf->global->SYSLOG_FILE)) {
if (empty($conf->global->SYSLOG_FILE)) {
$mainlogdir = DOL_DATA_ROOT;
$mainlog = 'dolibarr.log';
} else {
Expand All @@ -710,7 +711,7 @@ function compressSyslogs() {
$logname = $file['name'];
$logpath = $file['path'];

// Handle already compressed files
// Handle already compressed files to rename them and add +1

$filter = '^'.preg_quote($logname, '/').'\.([0-9]+)\.gz$';

Expand All @@ -729,7 +730,7 @@ function compressSyslogs() {
krsort($gzfiles, SORT_NUMERIC);

foreach($gzfiles as $numsave => $dummy) {
if(dol_is_file($logpath.'/'.$logname.'.'.($numsave+1).'.gz')) {
if (dol_is_file($logpath.'/'.$logname.'.'.($numsave+1).'.gz')) {
return -2;
}

Expand All @@ -741,18 +742,19 @@ function compressSyslogs() {
}

// Compress last save

if(dol_is_file($logpath.'/'.$logname.'.1')) {
if (dol_is_file($logpath.'/'.$logname.'.1')) {
if($nbSaves > 1) {
$gzfilehandle = gzopen($logpath.'/'.$logname.'.2.gz', 'wb9');

if(empty($gzfilehandle)) {
if (empty($gzfilehandle)) {
$this->error = 'Failted to open file '.$logpath.'/'.$logname.'.2.gz';
return -3;
}

$sourcehandle = fopen($logpath.'/'.$logname.'.1', 'r');

if(empty($sourcehandle)) {
if (empty($sourcehandle)) {
$this->error = 'Failed to open file '.$logpath.'/'.$logname.'.1';
return -4;
}

Expand All @@ -769,14 +771,16 @@ function compressSyslogs() {

// Compress current file et recreate it

if(dol_is_file($logpath.'/'.$logname)) {
if(dol_move($logpath.'/'.$logname, $logpath.'/'.$logname.'.1', 0, 1, 0, 0)) {
if (dol_is_file($logpath.'/'.$logname)) {
if (dol_move($logpath.'/'.$logname, $logpath.'/'.$logname.'.1', 0, 1, 0, 0))
{
$newlog = fopen($logpath.'/'.$logname, 'a+');
fclose($newlog);
}
}
}

$this->output = 'Archive log files (keeping last SYSLOG_FILE_SAVES='.$nbSaves.' files) done.';
return 0;
}
}
2 changes: 1 addition & 1 deletion htdocs/core/modules/modSyslog.class.php
Expand Up @@ -82,7 +82,7 @@ function __construct($db)

// Cronjobs
$this->cronjobs = array(
0=>array('label'=>'CompressSyslogs', 'jobtype'=>'method', 'class'=>'core/class/utils.class.php', 'objectname'=>'Utils', 'method'=>'compressSyslogs', 'parameters'=>'', 'comment'=>'PurgeDeleteTemporaryFiles', 'frequency'=>1, 'unitfrequency'=> 3600 * 24, 'priority'=>50, 'status'=>0, 'test'=>true),
0=>array('label'=>'CompressSyslogs', 'jobtype'=>'method', 'class'=>'core/class/utils.class.php', 'objectname'=>'Utils', 'method'=>'compressSyslogs', 'parameters'=>'', 'comment'=>'Compress and archive log files', 'frequency'=>1, 'unitfrequency'=> 3600 * 24, 'priority'=>50, 'status'=>0, 'test'=>true),
);
}
}

0 comments on commit 200fd88

Please sign in to comment.