Skip to content

Commit bfc7042

Browse files
committed
Rotating the log files if the name changes while the pointer to them is still open.
Fixes issue shaneharter#2... Conflicts: Core/Daemon.php
1 parent 5337ba9 commit bfc7042

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

Core/Daemon.php

+31-20
Original file line numberDiff line numberDiff line change
@@ -396,31 +396,42 @@ public function log($message, $send_alert = false)
396396
{
397397
static $handle = false;
398398
static $raise_logfile_error = true;
399+
static $last_logfile_name = '';
399400

400401
try
401402
{
402-
$header = "Date PID Message\n";
403-
$date = date("Y-m-d H:i:s");
403+
$header = "Date PID Message\n";
404+
$date = date("Y-m-d H:i:s");
404405
$pid = str_pad($this->pid, 5, " ", STR_PAD_LEFT);
405406
$prefix = "[$date] $pid";
406-
407-
if($handle === false)
408-
{
409-
if (strlen($this->log_file()) > 0)
410-
$handle = @fopen($this->log_file(), 'a+');
411-
412-
if($handle === false)
413-
{
414-
// If the log file can't be written-to, dump the errors to stdout with the explination...
415-
if ($raise_logfile_error) {
416-
$raise_logfile_error = false;
417-
$this->log('Unable to write logfile at ' . $this->log_file() . '. Redirecting messages to stdout.');
418-
}
419-
420-
throw new Exception("$prefix $message");
421-
}
422-
423-
fwrite($handle, $header);
407+
$logfile_name = $this->log_file();
408+
409+
// Rotate file if it is open but the desired name has changed (e.g. due to a date change)
410+
if ($handle && $last_logfile_name !== $logfile_name) {
411+
fclose($handle);
412+
$handle = false;
413+
}
414+
415+
416+
if($handle === false)
417+
{
418+
if (strlen($logfile_name) > 0)
419+
$handle = fopen($logfile_name, 'a+');
420+
421+
if($handle === false)
422+
{
423+
// If the log file can't be written-to, dump the errors to stdout with the explination...
424+
if ($raise_logfile_error) {
425+
$raise_logfile_error = false;
426+
$this->log('Unable to write logfile at ' . $logfile_name . '. Redirecting messages to stdout.');
427+
}
428+
429+
throw new Exception("$prefix $message");
430+
}
431+
432+
$last_logfile_name = $logfile_name;
433+
434+
fwrite($handle, $header);
424435

425436
if ($this->verbose)
426437
echo $header;

0 commit comments

Comments
 (0)