[GH-ISSUE #1185] Maybe LineFormatter shouldn't replace line breaks in the context values #487

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

Originally created by @nesk on GitHub (Aug 6, 2018).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/1185

I need to log some ASCII tables, like this one:

┌─────────┬─────┬─────┐
│ (index) │  a  │  b  │
├─────────┼─────┼─────┤
│    0    │  1  │ 'Y' │
│    1    │ 'Z' │  2  │
└─────────┴─────┴─────┘

The default configuration of the LineFormatter is not really suitable to easily read these tables, so I've allowed inline line breaks, here's an example:

define('ALLOW_INLINE_LINE_BREAKS', true);

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\LineFormatter;
use Monolog\Processor\PsrLogMessageProcessor;

$formatter = new LineFormatter(null, null, ALLOW_INLINE_LINE_BREAKS);

$handler = new StreamHandler('php://stdout');
$handler->setFormatter($formatter);

$logger = new Logger('test');
$logger->pushHandler($handler);
$logger->pushProcessor(new PsrLogMessageProcessor);

$data = "
┌─────────┬─────┬─────┐
│ (index) │  a  │  b  │
├─────────┼─────┼─────┤
│    0    │  1  │ 'Y' │
│    1    │ 'Z' │  2  │
└─────────┴─────┴─────┘
";

$logger->debug("Here's the data: {data}", ['data' => $data]);

If you run this code, you will get the following output:

[2018-08-06 16:16:10] test.DEBUG: Here's the data: 
┌─────────┬─────┬─────┐
│ (index) │  a  │  b  │
├─────────┼─────┼─────┤
│    0    │  1  │ 'Y' │
│    1    │ 'Z' │  2  │
└─────────┴─────┴─────┘
 {"data":"
┌─────────┬─────┬─────┐
│ (index) │  a  │  b  │
├─────────┼─────┼─────┤
│    0    │  1  │ 'Y' │
│    1    │ 'Z' │  2  │
└─────────┴─────┴─────┘
"} []

However, I don't think it's relevant to replace \n and \r occurences in the log string with real line breaks because it is no longer a valid JSON object (and it's hard to distinguish the message from the context), here's what I was expecting instead:

[2018-08-06 16:16:10] test.DEBUG: Here's the data: 
┌─────────┬─────┬─────┐
│ (index) │  a  │  b  │
├─────────┼─────┼─────┤
│    0    │  1  │ 'Y' │
│    1    │ 'Z' │  2  │
└─────────┴─────┴─────┘
{"data":"\n┌─────────┬─────┬─────┐\n│ (index) │  a  │  b  │\n├─────────┼─────┼─────┤\n│    0    │  1  │ 'Y' │\n│    1    │ 'Z' │  2  │\n└─────────┴─────┴─────┘\n"} []

This condition seems to be responsible of the additional line breaks:

github.com/Seldaek/monolog@334b8d8783/src/Monolog/Formatter/LineFormatter.php (L159-L161)

What do you think? Is this the wanted behavior?

Originally created by @nesk on GitHub (Aug 6, 2018). Original GitHub issue: https://github.com/Seldaek/monolog/issues/1185 I need to log some ASCII tables, like this one: ``` ┌─────────┬─────┬─────┐ │ (index) │ a │ b │ ├─────────┼─────┼─────┤ │ 0 │ 1 │ 'Y' │ │ 1 │ 'Z' │ 2 │ └─────────┴─────┴─────┘ ``` The default configuration of the `LineFormatter` is not really suitable to easily read these tables, so I've allowed inline line breaks, here's an example: ```php define('ALLOW_INLINE_LINE_BREAKS', true); use Monolog\Logger; use Monolog\Handler\StreamHandler; use Monolog\Formatter\LineFormatter; use Monolog\Processor\PsrLogMessageProcessor; $formatter = new LineFormatter(null, null, ALLOW_INLINE_LINE_BREAKS); $handler = new StreamHandler('php://stdout'); $handler->setFormatter($formatter); $logger = new Logger('test'); $logger->pushHandler($handler); $logger->pushProcessor(new PsrLogMessageProcessor); $data = " ┌─────────┬─────┬─────┐ │ (index) │ a │ b │ ├─────────┼─────┼─────┤ │ 0 │ 1 │ 'Y' │ │ 1 │ 'Z' │ 2 │ └─────────┴─────┴─────┘ "; $logger->debug("Here's the data: {data}", ['data' => $data]); ``` If you run this code, you will get the following output: ``` [2018-08-06 16:16:10] test.DEBUG: Here's the data: ┌─────────┬─────┬─────┐ │ (index) │ a │ b │ ├─────────┼─────┼─────┤ │ 0 │ 1 │ 'Y' │ │ 1 │ 'Z' │ 2 │ └─────────┴─────┴─────┘ {"data":" ┌─────────┬─────┬─────┐ │ (index) │ a │ b │ ├─────────┼─────┼─────┤ │ 0 │ 1 │ 'Y' │ │ 1 │ 'Z' │ 2 │ └─────────┴─────┴─────┘ "} [] ``` However, I don't think it's relevant to replace `\n` and `\r` occurences in the log string with real line breaks because _it is no longer a valid JSON object_ (and it's hard to distinguish the message from the context), here's what I was expecting instead: ``` [2018-08-06 16:16:10] test.DEBUG: Here's the data: ┌─────────┬─────┬─────┐ │ (index) │ a │ b │ ├─────────┼─────┼─────┤ │ 0 │ 1 │ 'Y' │ │ 1 │ 'Z' │ 2 │ └─────────┴─────┴─────┘ {"data":"\n┌─────────┬─────┬─────┐\n│ (index) │ a │ b │\n├─────────┼─────┼─────┤\n│ 0 │ 1 │ 'Y' │\n│ 1 │ 'Z' │ 2 │\n└─────────┴─────┴─────┘\n"} [] ``` This condition seems to be responsible of the additional line breaks: https://github.com/Seldaek/monolog/blob/334b8d8783a1262c3b8311d6599889d82e9cc58c/src/Monolog/Formatter/LineFormatter.php#L159-L161 What do you think? Is this the wanted behavior?
kerem 2026-03-04 02:15:25 +03:00
  • closed this issue
  • added the
    Feature
    label
Author
Owner

@nesk commented on GitHub (Aug 10, 2018):

Note that I'm just asking if this would be something accepted in a PR. I'm willing to write the PR by myself. 🙂

<!-- gh-comment-id:412028129 --> @nesk commented on GitHub (Aug 10, 2018): Note that I'm just asking if this would be something accepted in a PR. I'm willing to write the PR by myself. 🙂
Author
Owner

@Seldaek commented on GitHub (Nov 4, 2018):

I think maybe we could add another option in monolog 2 so that you can allow real line breaks, or optionally expand line breaks that have been inlined in json.

The way I use this, I never need the json to be actual json, and I don't wanna bother putting placeholders to get the expanded value in the message.. I just want to get whatever is in the context including line breaks.

<!-- gh-comment-id:435682057 --> @Seldaek commented on GitHub (Nov 4, 2018): I think maybe we could add another option in monolog 2 so that you can allow real line breaks, or optionally expand line breaks that have been inlined in json. The way I use this, I never need the json to be actual json, and I don't wanna bother putting placeholders to get the expanded value in the message.. I just want to get whatever is in the context including line breaks.
Author
Owner

@HenkPoley commented on GitHub (May 13, 2019):

This also bites when parts of the path on Windows starts with an 'n' or an 'r' and you throw an exception. See the linked issue above.

<!-- gh-comment-id:491851967 --> @HenkPoley commented on GitHub (May 13, 2019): This also bites when parts of the path on Windows starts with an 'n' or an 'r' and you throw an exception. See the linked issue above.
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#487
No description provided.