Search code examples
phplaravelloggingpermissionsmonolog

How do I change the default file permissions for log files created by PHP Monologger?


Laravel documentation shows that you can define custom configurations for your Monologger by placing the following code into your bootstrap/app.php file:

    $app->configureMonologUsing(function($monolog) {
    $monolog->pushHandler();
});

What are the possible custom configurations & syntaxes for them?

I'd like to change the daily log file's default permissions to 664 instead of the default 644, to avoid 'Permission denied' issues in the application.


Solution

  • For FileHandler and RotatingFileHandler you can easily set permission during construct. For RotatingFileHandler you have to set an optional parameter. These are parameters:

    ($filename, $maxFiles = 0, $level = Logger::DEBUG, $bubble = true, $filePermission = null, $useLocking = false)
    

    A code like this will work for you:

    $handler = new Monolog\Handler\RotatingFileHandler($filename,0,Logger::DEBUG,true,0664);