Skip to content

Instantly share code, notes, and snippets.

@arun07as
Last active March 18, 2024 13:57
Show Gist options
  • Save arun07as/30a3983cf03d0e40afac450a842ad4f7 to your computer and use it in GitHub Desktop.
Save arun07as/30a3983cf03d0e40afac450a842ad4f7 to your computer and use it in GitHub Desktop.
Laravel split logs files by year and month folders
'channels' => [
'error' => [
'driver' => 'monolog',
'handler' => \App\Logging\RotatingFileHandler::class,
'with' => [
'filename' => storage_path('logs/errors/laravel.log'),
'level' => 'error',
'maxFiles' => 0,
],
],
// rest of your channels
]

Laravel's daily driver for generating logs is great, but sometimes you wish you could break it up even more. These code snippets will help you break up your laravel logs by year and month folders

For example, the channel error from these snippets will gennerate files such as

/storage/logs/errors/2021/02/laravel-2021-02-28.log
/storage/logs/errors/2021/03/laravel-2021-03-01.log
/storage/logs/errors/2021/03/laravel-2021-03-02.log

Our custom RotatingFileHandler.php extends Monolog's Monolog\Handler\RotatingFileHandler, and we simply prepend the year and month before the file name before passing it to the parent constructor which will deal with the rest.

Hope this can help others who wishes for similar sturctures, and hope you all try out customizing it in any way you like. You can also split the folders by log levels and get files like

/storage/logs/error/laravel-2021-03-02.log
/storage/logs/warning/laravel-2021-03-02.log
/storage/logs/debug/laravel-2021-03-02.log

or basically any way you want by modifying the constructor.

Happy coding :)

<?php
namespace App\Logging;
use Monolog\Logger;
use Monolog\Handler\RotatingFileHandler as MonologRotatingFileHandler;
/**
* This is a custom log handler that generates a year folder, month folder and appends the date to the file name
*
* So if the path given is
* /storage/logs/errors/laravel.log
*
* it will generate files such as
* /storage/logs/errors/2021/03/laravel-2021-03-09.log
*
* @author Arun A S <arun@webandcrafts.in>
*/
class RotatingFileHandler extends MonologRotatingFileHandler
{
/**
* @param string $filename
* @param int $maxFiles The maximal amount of files to keep (0 means unlimited)
* @param string|int $level The minimum logging level at which this handler will be triggered
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
* @param int|null $filePermission Optional file permissions (default (0644) are only for owner read/write)
* @param bool $useLocking Try to lock log file before doing any writes
*/
public function __construct(string $filename, int $maxFiles = 0, $level = Logger::DEBUG, bool $bubble = true, ?int $filePermission = null, bool $useLocking = false)
{
$date = now();
$year = $date->format('Y');
$month = $date->format('m');
$fileInfo = pathinfo($filename);
$customFileName = $fileInfo['dirname'] . '/' . $year . '/' . $month . '/' . $fileInfo['basename'];
parent::__construct($customFileName, $maxFiles, $level, $bubble, $filePermission, $useLocking);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment