@@ -396,31 +396,42 @@ public function log($message, $send_alert = false)
396
396
{
397
397
static $ handle = false ;
398
398
static $ raise_logfile_error = true ;
399
+ static $ last_logfile_name = '' ;
399
400
400
401
try
401
402
{
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 " );
404
405
$ pid = str_pad ($ this ->pid , 5 , " " , STR_PAD_LEFT );
405
406
$ 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 );
424
435
425
436
if ($ this ->verbose )
426
437
echo $ header ;
0 commit comments