Skip to content

Instantly share code, notes, and snippets.

@nsouto
Last active June 22, 2024 04:35
Show Gist options
  • Save nsouto/e9d7a123f38d8c6adb39de0bae363403 to your computer and use it in GitHub Desktop.
Save nsouto/e9d7a123f38d8c6adb39de0bae363403 to your computer and use it in GitHub Desktop.
How to make sure Laravel Logs are created with proper permissions

How to have the Laravel Logs created with open permissions

When using Laravel from the CLI and WEB, usually you'll end up creating the log file from two different users, this can prevent your application from loading correctly in either side.

In order to resolve this you just have to edit the file:

bootstrap/app.php

And right before the end of the file where the line reads:

return $app;

Add this bit of code, adapting it to your needs:

$app->configureMonologUsing(function (Monolog\Logger $monolog) {
    $filename = storage_path('/logs/laravel.log');
    $handler = new Monolog\Handler\RotatingFileHandler($filename, 0, \Monolog\Logger::DEBUG, true, 0666);
    $handler->setFormatter(new \Monolog\Formatter\LineFormatter(null, null, true, true));
    $monolog->pushHandler($handler);
});

There are many more options you can customize with it, but this bit only makes sure the log is created with permissions to enable reading and writing by both the CLI user and the WEB user, setting the file permissions to 0666 which is the same as -rw-rw-rw-

Hope you find it useful.

@mortegamtnez
Copy link

mortegamtnez commented May 8, 2019

Thank you. You saved my day.

@fernandomullerjr
Copy link

Fixing Laravel log permission errors permanently:
https://devopsmind.com.br/en/troubleshooting-en/laravel-log-permission-errors/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment