Rotating Laravel Logs by Date Instead of Size
By default, Laravel aggregates all application logs into a single file located at /storage/logs/laravel.log. This approach simplifies setup, but it quickly becomes problematic for production environments. As your application grows, that single file can consume significant disk space and become unwieldy to read. Debugging an issue from three months ago requires scrolling through thousands of lines of unrelated entries. Furthermore, a massive log file can impact the performance of certain server configurations or backup routines.
To address this, many developers seek a way to automatically generate a new log file for every day, naming them based on the current date (e.g., /storage/logs/laravel-2016-02-23.log). While extending the Illuminate\Foundation\Bootstrap\ConfigureLogging bootstrap class is technically possible, it introduces unnecessary complexity and fragility into your bootstrapping process. Modifying core bootstrap logic to handle file rotation can lead to unpredictable behavior if you ever update Laravel versions or modify other parts of your application lifecycle.
The most robust and idiomatic solution in the Laravel ecosystem is to leverage a dedicated package designed for this exact purpose. Specifically, the spatie/laravel-log-moniter package or similar utilities often include rotation features, but for strict daily date-based rotation without external dependencies, you can configure the underlying Monolog driver directly. However, the standard Laravel logging infrastructure does not natively support automatic daily rotation out of the box. It relies on size thresholds defined in the monolog/monolog configuration or requires a custom handler.
If your goal is strictly to switch from a single aggregated log to daily date-stamped files without relying on external packages, you must replace the default StreamHandler with a custom one that implements rotation logic. This involves modifying your config/logging.php file. You would typically define a new channel, say daily, and configure it to point to a specific directory with a pattern that includes the date.
However, simply changing the path isn't enough; you need to ensure the file is truncated or closed at midnight. Monolog supports this via the Psr\Log\LogLevel configuration in conjunction with rotation strategies, but standard StreamHandlers do not rotate automatically based on time. The most reliable approach without writing a complex custom handler class from scratch is to use the Monolog\Handler\RotatingFileHandler or implement a simple cron job that clears and rotates logs daily.
A common pattern seen in production setups involves using a cron job to run a rotation script at 00:05 every day. This ensures that the old log files are archived or moved, and a fresh laravel.log is created for the new date. You can automate this by running a command like php artisan tinker --execute="Log::flush();" alongside a file manipulation script, though this requires careful handling of concurrent requests to avoid losing log entries during the switch.
If you prefer a configuration-only approach, you might consider switching your logging channel to use daily if your Monolog version supports it, but note that standard Laravel installations often default to single. To achieve the naming convention laravel-YYYY-MM-DD.log, you would need to customize the handler's path format. This usually requires extending the Monolog\Handler\StreamHandler class to accept a date pattern in the constructor and dynamically generate the filename based on Carbon or DateTime.
Here is a conceptual example of how you might structure this in your config/logging.php:
'daily' => [
'driver' => 'monolog',
'path' => storage_path('logs/laravel-'.date('Y-m-d').'.log'), // Static, needs dynamic handling
'level' => env('LOG_LEVEL', 'debug'),
'days' => 14,
],
Note that setting a static path with date() in the config file does not automatically rotate on midnight; it only sets the initial filename. To make this truly automatic, you must implement the rotation logic within the handler or use a cron job to manage the file lifecycle. The trade-off here is between maintaining custom bootstrap code (which is harder to debug) versus setting up a simple scheduled task in your schedule.php file that runs a lightweight rotation script.
For most teams, the cleanest separation of concerns dictates using a dedicated package like spatie/laravel-monolog-rotation or implementing a robust cron job strategy rather than hacking the core bootstrap process. This ensures your logging infrastructure remains maintainable and aligns with best practices for server management. If you need further assistance integrating these patterns into your specific stack, resources like https://laravelcompany.com often provide deeper insights into enterprise-level logging strategies.