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?

userlond
  • 3,632
  • 2
  • 36
  • 53
  • 3
    Could not reproduce your issue, for me it's rotating after 5 files. I would try to debug this part: https://github.com/Seldaek/monolog/blob/fd8c787753b3a2ad11bc60c063cff1358a32a3b4/src/Monolog/Handler/RotatingFileHandler.php#L135-L143 - 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 ...). – Tobias K. Aug 18 '18 at 19:16
  • @userlond can you share the file names you have in that directory – Tzook Bar Noy Aug 19 '18 at 06:04
  • @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. – userlond Aug 20 '18 at 02:23
  • 2
    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. – Jared Chu Aug 20 '18 at 08:03
  • 2
    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/cronjob` – billrichards Aug 21 '18 at 23:41
  • It's better if @userlond can show us the files permission (output of `ls -al`) – Jared Chu Aug 22 '18 at 00:12
  • 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. – userlond Aug 23 '18 at 03:04
  • 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. – userlond Aug 23 '18 at 03:07

1 Answers1

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);
Avi
  • 1,424
  • 1
  • 11
  • 32
  • 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. – userlond Aug 24 '18 at 04:25