[GH-ISSUE #321] Remove inline line breaks from line formatter #108

Closed
opened 2026-03-04 02:12:15 +03:00 by kerem · 2 comments
Owner

Originally created by @gunnarlium on GitHub (Feb 14, 2014).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/321

I'm a bit unsure about the best approach for a problem I'm having, and wanted to hear some opionions before making a PR.

When the LineFormatter is used to log exceptions, and the message of the thrown exception has line breaks, the line breaks are inlcuded in the Monolog message, in effect causing the log message to span multiple lines. This breaks many log parsers, as they expected each entry to span exactly one line. I would rather want these line breaks to replaced be replaced by either \n or . I can quite easily create a SingleLineFormatter that extends the LineFormatter:

class SingleLineFormatter extends LineFormatter
{
    public function format(array $record)
    {
        $record = parent::format($record);

        return str_replace(PHP_EOL, ' ', $record) . PHP_EOL;
    }
}

So I guess my question is whether it would make sense to make something like this the default behavior of the line formatter, whether such a forced single line formatter might be relevant for inclusion in Monolog, or if I should just create it myself whenever I need it?

Originally created by @gunnarlium on GitHub (Feb 14, 2014). Original GitHub issue: https://github.com/Seldaek/monolog/issues/321 I'm a bit unsure about the best approach for a problem I'm having, and wanted to hear some opionions before making a PR. When the LineFormatter is used to log exceptions, and the message of the thrown exception has line breaks, the line breaks are inlcuded in the Monolog message, in effect causing the log message to span multiple lines. This breaks many log parsers, as they expected each entry to span exactly one line. I would rather want these line breaks to replaced be replaced by either `\n` or ` `. I can quite easily create a SingleLineFormatter that extends the LineFormatter: ``` PHP class SingleLineFormatter extends LineFormatter { public function format(array $record) { $record = parent::format($record); return str_replace(PHP_EOL, ' ', $record) . PHP_EOL; } } ``` So I guess my question is whether it would make sense to make something like this the default behavior of the line formatter, whether such a forced single line formatter might be relevant for inclusion in Monolog, or if I should just create it myself whenever I need it?
kerem closed this issue 2026-03-04 02:12:15 +03:00
Author
Owner

@Seldaek commented on GitHub (Feb 14, 2014):

I think this can be considered a bugfix, the line formatter really shouldn't produce multiple lines output unless you put line breaks in the format yourself. It could probably be optional in case someone really wants to keep the old behavior so if you want to send a PR with a constructor flag that'd be great.

<!-- gh-comment-id:35073963 --> @Seldaek commented on GitHub (Feb 14, 2014): I think this can be considered a bugfix, the line formatter really shouldn't produce multiple lines output unless you put line breaks in the format yourself. It could probably be optional in case someone really wants to keep the old behavior so if you want to send a PR with a constructor flag that'd be great.
Author
Owner

@gunnarlium commented on GitHub (Feb 14, 2014):

Created a pull request. I'm "readding" a trailing line break if the message has a trailing line break.

When this is enabled, having custom inline line breaks in the format won't work. So if we want to allow such line breaks, but not linebreaks in the actual message, we need to something more complex, I think.

<!-- gh-comment-id:35077421 --> @gunnarlium commented on GitHub (Feb 14, 2014): Created a pull request. I'm "readding" a trailing line break if the message has a trailing line break. When this is enabled, having custom inline line breaks in the format won't work. So if we want to allow such line breaks, but not linebreaks in the actual message, we need to something more complex, I think.
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#108
No description provided.