[GH-ISSUE #2011] StreamHandler doesn't handle non-blocking streams properly, truncating logs #861

Open
opened 2026-03-04 03:01:39 +03:00 by kerem · 1 comment
Owner

Originally created by @martin-heralecky on GitHub (Dec 22, 2025).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/2011

When writing large logs to a non-blocking stream, logs are often truncated. The exact length of the line that is actually written varies between calls.

Here's a snippet that attempts to write around 1M characters to a non-blocking stream:

stream_set_blocking(STDOUT, false);

$log = new Logger("log", [new StreamHandler(STDOUT)]);

$log->info(join(",", array_fill(0, 100000, "123456789"))); // ~1MB

Run with php snippet.php | wc -c to see the number of characters actually written. On my platform, this is typically around 64K. I'm using monolog version 3.9.0.


The problem lies in the StreamHandler::streamWrite() method, which ignores the return value of fwrite(). I believe the correct solution is to call fwrite() in a loop until all characters are written. If it is desirable to have this behavior in monolog, I am willing to put together a patch.

Originally created by @martin-heralecky on GitHub (Dec 22, 2025). Original GitHub issue: https://github.com/Seldaek/monolog/issues/2011 When writing large logs to a non-blocking stream, logs are often truncated. The exact length of the line that is actually written varies between calls. Here's a snippet that attempts to write around 1M characters to a non-blocking stream: ```php stream_set_blocking(STDOUT, false); $log = new Logger("log", [new StreamHandler(STDOUT)]); $log->info(join(",", array_fill(0, 100000, "123456789"))); // ~1MB ``` Run with `php snippet.php | wc -c` to see the number of characters actually written. On my platform, this is typically around 64K. I'm using monolog version 3.9.0. --- The problem lies in the `StreamHandler::streamWrite()` method, which ignores the return value of `fwrite()`. I believe the correct solution is to call `fwrite()` in a loop until all characters are written. If it is desirable to have this behavior in monolog, I am willing to put together a patch.
Author
Owner

@Seldaek commented on GitHub (Dec 30, 2025):

Yeah that sounds very reasonable, definitely should be fixed.

<!-- gh-comment-id:3700631182 --> @Seldaek commented on GitHub (Dec 30, 2025): Yeah that sounds very reasonable, definitely should be fixed.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/monolog#861
No description provided.