[GH-ISSUE #1629] New lines in context data do not output valid json. (Laravel) #693

Closed
opened 2026-03-04 02:17:05 +03:00 by kerem · 1 comment
Owner

Originally created by @LiamKarlMitchell on GitHub (Jan 24, 2022).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/1629

Monolog version 2

Noticed when using in a Laravel project and trying to ingest logs into ELK.

Something like this:

Log::debug('Testing new lines in log context.', ['yep' => "This\nhas\nNew Lines."]);

Outputs

[2022-01-25 09:53:22] local.DEBUG: Testing new lines in log context. {"yep":"This
has
New Lines."}

Expecting

[2022-01-25 09:53:22] local.DEBUG: Testing new lines in log context. {"yep":"This\\nhas\\nNew Lines."}

Just wonder if there is a setting I can alter to make it escape the new line characters as its invalid json.

Maybe related: https://github.com/Seldaek/monolog/issues/1185
https://stackoverflow.com/questions/70840461/new-lines-in-context-data-do-not-output-valid-json

Originally created by @LiamKarlMitchell on GitHub (Jan 24, 2022). Original GitHub issue: https://github.com/Seldaek/monolog/issues/1629 Monolog version 2 Noticed when using in a Laravel project and trying to ingest logs into ELK. Something like this: ``` Log::debug('Testing new lines in log context.', ['yep' => "This\nhas\nNew Lines."]); ``` Outputs ``` [2022-01-25 09:53:22] local.DEBUG: Testing new lines in log context. {"yep":"This has New Lines."} ``` Expecting ``` [2022-01-25 09:53:22] local.DEBUG: Testing new lines in log context. {"yep":"This\\nhas\\nNew Lines."} ``` Just wonder if there is a setting I can alter to make it escape the new line characters as its invalid json. Maybe related: https://github.com/Seldaek/monolog/issues/1185 https://stackoverflow.com/questions/70840461/new-lines-in-context-data-do-not-output-valid-json
kerem 2026-03-04 02:17:05 +03:00
  • closed this issue
  • added the
    Support
    label
Author
Owner

@Seldaek commented on GitHub (Mar 13, 2022):

If you need your log lines to be json, the best way is to use a JsonFormatter, then it's guaranteed json one line per log record.

If you're using a LineFormatter, by default it should have allowInlineLineBreaks set to false, which should also prevent that problem. See github.com/Seldaek/monolog@6f233d2e2f/src/Monolog/Formatter/LineFormatter.php (L43)

Maybe check your config.

<!-- gh-comment-id:1066168143 --> @Seldaek commented on GitHub (Mar 13, 2022): If you need your log lines to be json, the best way is to use a JsonFormatter, then it's guaranteed json one line per log record. If you're using a LineFormatter, by default it should have allowInlineLineBreaks set to false, which should also prevent that problem. See https://github.com/Seldaek/monolog/blob/6f233d2e2fc38378670db62640f630fdd7c8321f/src/Monolog/Formatter/LineFormatter.php#L43 Maybe check your config.
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#693
No description provided.