[GH-ISSUE #1788] LogRecord->with does not transfers "formatted" value #757

Open
opened 2026-03-04 02:17:43 +03:00 by kerem · 4 comments
Owner

Originally created by @Justinas-Jurciukonis on GitHub (Jan 17, 2023).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/1788

Monolog version 3.2.0

When PsrLogMessageProcessor processes message, it returns new instance of LogRecord using code

return $record->with(message: strtr($record->message, $replacements), context: $context);

and inside LogRecord->with there is missing formatted key to be copied over, so if any previous processor set any information inside LogRecord->formatted it will be lost.

Seems like same issue with toArray

Originally created by @Justinas-Jurciukonis on GitHub (Jan 17, 2023). Original GitHub issue: https://github.com/Seldaek/monolog/issues/1788 Monolog version 3.2.0 When `PsrLogMessageProcessor` processes message, it returns new instance of `LogRecord` using code ```php return $record->with(message: strtr($record->message, $replacements), context: $context); ``` and inside [`LogRecord->with`](https://github.com/Seldaek/monolog/blob/main/src/Monolog/LogRecord.php#L118) there is missing `formatted` key to be copied over, so if any previous processor set any information inside `LogRecord->formatted` it will be lost. Seems like same issue with `toArray`
Author
Owner

@Seldaek commented on GitHub (Feb 4, 2023):

Normally formatting happens after processing, so this should never be a problem. Have you experienced an actual issue?

<!-- gh-comment-id:1416847844 --> @Seldaek commented on GitHub (Feb 4, 2023): Normally formatting happens after processing, so this should never be a problem. Have you experienced an actual issue?
Author
Owner

@Justinas-Jurciukonis commented on GitHub (Feb 6, 2023):

@Seldaek Yes, since we use custom logic to format all records.

Basically logic was as follows:

  1. Create new structure to formatted property
  2. Every processor adds it's own information to this formatted to the place where it needs to be
  3. Formatter converts log record to JSON or Plain text log, but keeps structure from formatted. Means formatters only responsible for final output, but not structure of log itself

With this approach formatters does not care about structure, just how format LogRecord to string. Processors are responsible for it's own parts of LogRecord->formatted processing.

E.g. Adding information about current user can come from 3 different processors and it merges under key LogRecord->formatted->user

<!-- gh-comment-id:1419090779 --> @Justinas-Jurciukonis commented on GitHub (Feb 6, 2023): @Seldaek Yes, since we use custom logic to format all records. Basically logic was as follows: 1. Create new structure to `formatted` property 2. Every processor adds it's own information to this `formatted` to the place where it needs to be 3. Formatter converts log record to JSON or Plain text log, but keeps structure from `formatted`. Means formatters only responsible for final output, but not structure of log itself With this approach formatters does not care about structure, just how format `LogRecord` to string. Processors are responsible for it's own parts of `LogRecord->formatted` processing. E.g. Adding information about current user can come from 3 different processors and it merges under key `LogRecord->formatted->user`
Author
Owner

@stof commented on GitHub (Feb 6, 2023):

This looks like the use case for LogRecord->extra instead

<!-- gh-comment-id:1419167854 --> @stof commented on GitHub (Feb 6, 2023): This looks like the use case for `LogRecord->extra` instead
Author
Owner

@Seldaek commented on GitHub (Feb 7, 2023):

Right, exactly my thinking @stof - sounds to me like these processors should all record stuff in extra as they are meant to, and then the final formatter can use the info in context/extra to generate the final formatted record.

<!-- gh-comment-id:1420375833 --> @Seldaek commented on GitHub (Feb 7, 2023): Right, exactly my thinking @stof - sounds to me like these processors should all record stuff in extra as they are meant to, and then the final formatter can use the info in context/extra to generate the final formatted record.
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#757
No description provided.