Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interleaved log records using StreamHandler #1552

Closed
dsch opened this issue May 5, 2021 · 1 comment
Closed

Interleaved log records using StreamHandler #1552

dsch opened this issue May 5, 2021 · 1 comment
Labels
Milestone

Comments

@dsch
Copy link
Contributor

dsch commented May 5, 2021

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.

@dsch dsch added the Bug label May 5, 2021
@dsch
Copy link
Contributor Author

dsch commented May 7, 2021

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!..... [] []
............ [] []

Seldaek added a commit that referenced this issue May 28, 2021
Set StreamHandler stream chunk size, fixes #1552
@Seldaek Seldaek added this to the 2.x milestone May 28, 2021
robocoder pushed a commit to robocoder/monolog that referenced this issue May 24, 2022
This ensures atomic log record writing to the output stream.
Seldaek pushed a commit that referenced this issue Jun 9, 2022
This ensures atomic log record writing to the output stream.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants