Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Monolog logger RotatingFileHandler never rotates files

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?

like image 534
userlond Avatar asked Aug 02 '18 05:08

userlond


1 Answers

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);
like image 108
Avi Avatar answered Oct 16 '22 09:10

Avi