Seldaek / monolog

Sends your logs to files, sockets, inboxes, databases and various web services

Home Page:https://seldaek.github.io/monolog/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Interleaved log records using StreamHandler

dsch opened this issue · comments

Monolog version 1.24.0
PHP version 7.0.33

We get interleaved log records while using StreamHandler. The under-laying PHP stream writer implements a chunked write with a default chunk size of 8192 bytes. In an application with many concurrent processes writing to a single log file this leads to interleaved log records.

Example (if the chunk size would be 4):

my log line 1
my lmy log line 3
og line 2 
my log line 4

This issue could be fix in a simular way as in #1129 by calling stream_set_chunk_size.

Minimal reproducible example

index.php

<?php
require_once(__DIR__ . '/vendor/autoload.php');

use Monolog\Handler\StreamHandler;
use Monolog\Logger;

$logger = new Logger('channel-name');
$logger->pushHandler(new StreamHandler(__DIR__ . '/app.log'));
$logger->info('This is a log!' . str_repeat('.', 8192));

Run it once

> php index.php 
> wc --max-line-length app.log 
8266 app.log

Run it in parallel (output shortened)

> parallel php index.php ::: {1..1000}
> wc --lines --max-line-length app.log 
1000 16458 app.log
> grep -A1 "INFO.*INFO" app.log
[2021-05-07T11:01:21.395883+02:00] channel-name.INFO: This is a log!.....[2021-05-07T11:01:21.395883+02:00] channel-name.INFO: This is a log!..... [] []
............ [] []