PHP Monolog logger RotatingFileHandler never rotates files
Asked Answered
A

1

7

PHP 7.1, ubuntu 12.04 LTS, monolog version is 1.23.0.

Logger initialisation:

<?php
    $logger = new Logger('app');
    $logger->pushHandler(
        new RotatingFileHandler(
            Main\Application::getDocumentRoot() . '/runtime/logs/app.log',
            5,
            Logger::DEBUG
        )
    );

Currently there is 24 log files in the path, not 5 as expected.

How to force monolog to rotate files as expected? Do I need do some extra steps to rotate files, what reason may cause not deleting old logs?

Alvord answered 2/8, 2018 at 5:59 Comment(8)
Could not reproduce your issue, for me it's rotating after 5 files. I would try to debug this part: github.com/Seldaek/monolog/blob/… - if the old file is not writeable or an error occurs during unlink nothing is escalated and it silently does nothing. I suppose you checked file permissions (a bit unlikely, the user that created the file should also be able to delete it again ...).Serialize
@Alvord can you share the file names you have in that directoryJoachima
@Tobias K, file permission is 600. I run script via cli. I've added test code to my script: is_writeable returns true, unlink is ok when call them argument equal to path to one of the logs. @Tzook Bar Noy, app-2018-08-20.log, app-2018-08-19.log and so on. Nothing special, I think.Alvord
The main log file app.log is writable but the old log files maybe not. The second reason is if you run the script with the cronjob, it may run with another user permission.Whall
agree with @Jared Chu. If this is a cronjob, then in your debugging, make sure you are testing it as the same user that runs cronjob. e.g. use sudo -u cronuser /usr/bin/php /path/to/cronjobDemeanor
It's better if @Alvord can show us the files permission (output of ls -al)Whall
Thanks for replies! Script is really runned via cron. I've added echo exec('id -gn') . ' ' . exec('id -un') . "\n"; exit; code to the start of the script. It should output the group and name of user who runs script I think. Direct cli run and run via cron produces the same results. Maybe there were some changes in server configuration but for now in my tests logs are rotated ok without any changes in code. I'am really surprised.Alvord
I think that somebody may give an answer with some recipes to diagnose the problem, for ex. some words about file permissions. And I will accept the answer.Alvord
M
3

Try the below code...

$logger = new Logger('app');
$handler = new RotatingFileHandler(Main\Application::getDocumentRoot() . '/runtime/logs/app.log', 5, Logger::DEBUG, true, 0664);

//$handler->setFilenameFormat('{date}-{filename}', 'Y/m/d');
$logger->pushHandler($handler);

$array = ["x" => "y"];
$logger->addInfo('new message', $array);
Mastic answered 20/8, 2018 at 9:50 Comment(1)
As I understood, $filePermission parameter you specified should force 664 permissions to newly created log files. It should give permissions of read and write to file for all users of groups, which current user is member of. As I commented to my question, my problem seems dissapeared by itself and I have no time now to make full test (project is in development). Also some time ago project moved to another server, so some changes in configurations may be possibly made. This answer has not enought info about, just try. Not enought clear explanation, some comments have more info to go, I think.Alvord

© 2022 - 2024 — McMap. All rights reserved.