Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sky93's avatar
Level 1

Buffer for daily logs

Hello everyone,

I'd like to implement a buffer for my daily Laravel log. My goal is to flush the log to a file after it reaches a certain size, for example, 100 KB. I have been trying to achieve this by modifying the config in my logging.php file. Could someone please help me understand the proper way to set this up? Any guidance or suggestions would be greatly appreciated.

        'my_daily' => [
            'driver' => 'monolog',
            'handler'=> Monolog\Handler\BufferHandler::class,
            'with' => [
                'handler' => Monolog\Handler\RotatingFileHandler::class,
                'filename' => storage_path('logs/daily-buffer.log')
            ],
        ],

Using the config above I get this error:

[2023-05-01 12:43:14] laravel.EMERGENCY: Unable to create configured logger. Using emergency logger. {"exception":"[object] (TypeError(code: 0): Monolog\Handler\BufferHandler::__construct(): Argument #1 ($handler) must be of type Monolog\Handler\HandlerInterface, string given at /vendor/monolog/monolog/src/Monolog/Handler/BufferHandler.php:50)
[stacktrace]
#0 [internal function]: Monolog\Handler\BufferHandler->__construct('Monolog\\Handler...', 0, 100, true, false)

Thanks in advance!

0 likes
5 replies
LaryAI's avatar
Level 58

The error message suggests that the first argument passed to the BufferHandler constructor should be an instance of HandlerInterface, but a string was passed instead. To fix this, you need to create an instance of the RotatingFileHandler class and pass it as the first argument to the BufferHandler constructor. Here's an example configuration that should work:

'my_daily' => [
    'driver' => 'monolog',
    'handler' => Monolog\Handler\BufferHandler::class,
    'with' => [
        'handler' => new Monolog\Handler\RotatingFileHandler(storage_path('logs/daily-buffer.log'), 100, Monolog\Logger::DEBUG, true, 0664),
        'buffer_size' => 100 * 1024, // 100 KB
    ],
],

In this example, we're creating a new instance of RotatingFileHandler and passing it the log file path, the maximum file size (100 KB), the log level (DEBUG), whether to create the log file if it doesn't exist, and the file permissions. We're then passing this instance as the handler argument to the BufferHandler constructor, and setting the buffer_size option to 100 KB. This should create a buffer that flushes to the log file whenever it reaches 100 KB in size.

Sky93's avatar
Level 1

I changed my configuration to:

'my_daily' => [
            'driver' => 'monolog',
            'handler'=> Monolog\Handler\BufferHandler::class,
            'with' => [
                'handler' => new Monolog\Handler\RotatingFileHandler(storage_path('logs/daily-buffer.log'), 7),
                'bufferLimit' => 100, // 100 KB
            ],
        ],

The error disappeared but the buffer still not working.

Snapey's avatar

What problem are you trying to solve? Maybe just log less?

Sky93's avatar
Level 1

@Snapey I write about 2-5K lines of logs in a second so it has an impact on the disk. So I wanted to flush logs to disk slowly to reduce IO/s.

Snapey's avatar

@Sky93 wow, thats way too much logging to be of practical use.

Please or to participate in this conversation.