BatchStreamHandler
A Monolog handler that takes a batch of records and pushes them to a stream at once., (*1)
(Use ^2.0 or Monolog ^2.0 and ^1.0 for Monolog ^1.x), (*2)
Why
When logging calls to webservers I like all log records of one call grouped together. I changed the default StreamHandler to handle only batches of records (handleBatch) and write them to the stream at once - voilĂ the BatchStreamHandler., (*3)
Installation
Install with, (*4)
composer require spazzmarticus/monolog-batchstreamhandler
Usage
use SpazzMarticus\BatchStreamHandler\BatchStreamHandler;
use Monolog\Handler\BufferHandler;
use Monolog\Logger;
$batchStreamHandler = new BatchStreamHandler('supsi-looking.log');
//Optional - Envelop the records with head and foot lines
$batchStreamHandler->pushHeadLine('-------');
$batchStreamHandler->pushFootLine('=======');
$bufferHandler = new BufferHandler($batchStreamHandler);
$logger = new Logger('supsi');
$logger->pushHandler($bufferHandler);
//PewPew - Do your stuff here
StreamHandler vs BatchStreamHandler
The default StreamHandler pushes each record to the stream immediatly. Even when put after a BufferHandler (which buffers records until flush() is called) each record is processed and written to the stream seperatly., (*5)
Let's assume there are 3 parallel calls to the webserver A, B, C, (*6)
------------------- Time ------------------->
A --[Debug]------[Notice]--------[Error]---|
B -[Warning][Warning]---------------|
C -----[Notice][Error]------------------------|
The log looks something like:, (*7)
Warning
Debug
Notice
Warning
Error
Notice
Error
And that makes it hard to look at what happend at a specific calls., (*8)
With the BatchStreamHandler the log looks something like:, (*9)
Warning
Warning
Debug
Notice
Error
Notice
Error
Which is still not superb, so I added enveloping with head and foot lines., (*10)
Now this looks like a log I can work with:, (*11)
-------
Warning
Warning
=======
-------
Debug
Notice
Error
=======
-------
Notice
Error
=======