In this article, we will discuss “How to Handle Logging in Laravel”. Laravel logging is quite simple and easy to manage. By default, all the error writes in the file located at “/storage/logs/laravel.log”.
Yes, it’s easy to get the error information in this file. But day by day it’s getting bigger and than not so easy to scroll and find the error.
As one of my project issues, the error log size is more than 50 MB. And it’s pretty difficult to find anything in this file if a client reports that an “unknown bug” happened to him a few days ago.
Now, the question is how to handle it?
Luckily, Laravel provides different channels for logging. You can check those channels in the “config/logging.php” file.
return [ ... 'channels' => [ ... 'daily' => [ 'driver' => 'daily', 'path' => storage_path('logs/laravel.log'), 'level' => 'debug', 'days' => 14, ], ... ], ];
As per the above mention code snippet, I am focusing on the “daily” channel. Using the “daily” log channel, it will create a new file on a daily basis for logging the information. Each file located at the “/storage/logs” directory.
You have to update your “.env” file. And update the following setting.
After that update the setting, instead of one “laravel.log” file. You will get a separate file for each date and each file name – something like “laravel-2020-01-06.log”.
Laravel provides a different kind of drivers to manage the logging. Some of drivers are “single”, “daily”, “slack”, “syslog”, “errorlog”, “monolog”, “custom”, “stack”. You can get more details on the logging in the official Laravel documentation.
I hope this will helps you to understand the logging in Laravel. We will discuss more on the Laravel Logging with its available channels and drivers in our future articles. Also, if you have any query then feel free to add in the comment area 🙂