Skip to content

StreamHandler doesn't handle non-blocking streams properly, truncating logs #2011

@martin-heralecky

Description

@martin-heralecky

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions