Blog

Laravel. Error Prone Logging

[{"Id":"201902142225320866","ParentId":"20190214222500079147","Type":"Text","Sequence":1,"Attributes":{"Text":"When Laravel logs the errors thrown by your application it will write these to a log file in your storage directory: /storage/logs/laravel-2019-02-14.log"}},{"Id":"201902142228580716","ParentId":"20190214222500079147","Type":"Text","Sequence":2,"Attributes":{"Text":"However, if you have commands in your application, it is quite usual to have the commands running under a more previleged user (i.e. admin). At the same time the web pages will be using a less privileged user (i.e. www-data). If the more privileged user do write to the log file first, the file will automatically be attributed with the more privileged user as owner. When later the less privileged user attempts to write to the same file an ugly error will be thrown. "}},{"Id":"201902142234040456","ParentId":"20190214222500079147","Type":"Text","Sequence":3,"Attributes":{"Text":"Here is how this can be solved:"}},{"Id":"201902142234240302","ParentId":"20190214222500079147","Type":"Heading","Sequence":4,"Attributes":{"Level":"2","Text":"Laravel 5.7 and Above"}},{"Id":"201902142235200390","ParentId":"20190214222500079147","Type":"Text","Sequence":5,"Attributes":{"Text":"Open your /config/logging.php file and add the following to the top:"}},{"Id":"201902142236170955","ParentId":"20190214222500079147","Type":"Code","Sequence":6,"Attributes":{"Language":"php","Code":"$logFilename = storage_path('logs/laravel-'.php_sapi_name().'.log');"}},{"Id":"201902142235210147","ParentId":"20190214222500079147","Type":"Text","Sequence":7,"Attributes":{"Text":"Then down the file repace the loging path with the one above:"}},{"Id":"201902142237190496","ParentId":"20190214222500079147","Type":"Code","Sequence":8,"Attributes":{"Language":"php","Code":"'daily' => [\r\n 'driver' => 'daily',\r\n 'path' => $logFilename, // storage_path('logs/laravel.log'),\r\n 'level' => 'debug',\r\n 'days' => 14,\r\n ],"}},{"Id":"201902142227270999","ParentId":"20190214222500079147","Type":"Text","Sequence":9,"Attributes":{"Text":"This will split the log file to two depending on the user:\r\nlaravel-apache2handler-2019-02-14.log and laravel-cii-2019-02-14.log"}},{"Id":"201902142234510694","ParentId":"20190214222500079147","Type":"Heading","Sequence":10,"Attributes":{"Level":"2","Text":"Laravel 5.6 and Below"}},{"Id":"201902142236380003","ParentId":"20190214222500079147","Type":"Text","Sequence":11,"Attributes":{"Text":"For Laravel 5.6 and below open the /bootstrap/app.php and add the following code:"}},{"Id":"201902142239460990","ParentId":"20190214222500079147","Type":"Code","Sequence":12,"Attributes":{"Language":"php","Code":"$app->configureMonologUsing(function(Monolog\\Logger $monolog) {\r\n $filename = storage_path('logs/laravel-'.php_sapi_name().'.log');\r\n $handler = new Monolog\\Handler\\RotatingFileHandler($filename);\r\n $monolog->pushHandler($handler);\r\n});"}},{"Id":"201902142240370142","ParentId":"20190214222500079147","Type":"Text","Sequence":13,"Attributes":{"Text":"The above will do the same as the code for Laravel 5.7 and will split the log files of the web user and the command line user."}}]