0

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?

Note: The data containing new lines is perfectly fine, wanting to log this information out in its entirety, but the logging library does not output valid json?

https://github.com/Seldaek/monolog/issues/1629

Liam Mitchell
  • 1,001
  • 13
  • 23

1 Answers1

0

Use single quotes and they will be escaped. '\n' doesn't output the newline.

Another option is to json_encode what you want to log if you do not want the newlines to be outputted.

php > print_r(['e' => "a\nb"]);              <- double quotes
Array
(
    [e] => a
b                                            <- newline is outputted
)
php > print_r(['e' => 'a\nb']);              <- single quotes
Array
(
    [e] => a\nb                              <- newline is not outputted
)
php > print_r(json_encode(['e' => "a\nb"])); <- json_encode
{"e":"a\nb"}                                 <- newline is not outputted

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

Log::debug('Testing new lines in log context.', json_encode(['yep' => "This\nhas\nNew Lines."]));
IGP
  • 14,160
  • 4
  • 26
  • 43
  • The data goes to another system, the new lines are used for formatting the data, but want it to be logged in its entirety. When outputting \n to json I'de expect the logging library to escape it to output valid json. Smells like a bug. – Liam Mitchell Jan 25 '22 at 21:38