[GH-ISSUE #1356] remove replaced context when using psrlogmessageprocessor #570

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

Originally created by @rhrzr on GitHub (Aug 3, 2019).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/1356

as far as i know, the psrlogmessageprocessor-class is designed to replace placeholders in messages like

$logger->debug('{foo}', ['foo' => 'bar']);

after processing the message, the output is

"bar {'foo':'bar'}"

the replaced context still exists, even though the element 'foo' was properly replaced.
why not remove that context, but keep the context which could not be replaced?
i tried do achieve this goal and this is my solution: (class PsrLogMessageProcessor.php)

public function __invoke(array $record)
...
$newContext = array();
foreach ($record['context'] as $key => $val) {
    ...
    if (false === strpos($record['message'], '{'.$key.'}')) {
        $newContext[$key] = $val;
    }
}
$record['context'] = $newContext;
...

if the user wants to keep the context in his message, you could make it an option by introducing a boolean parameter in the constructor.
what do you think about?

Originally created by @rhrzr on GitHub (Aug 3, 2019). Original GitHub issue: https://github.com/Seldaek/monolog/issues/1356 as far as i know, the psrlogmessageprocessor-class is designed to replace placeholders in messages like ```php $logger->debug('{foo}', ['foo' => 'bar']); ``` after processing the message, the output is ```php "bar {'foo':'bar'}" ``` the replaced context still exists, even though the element 'foo' was properly replaced. why not remove that context, but keep the context which could not be replaced? i tried do achieve this goal and this is my solution: (class PsrLogMessageProcessor.php) ```php public function __invoke(array $record) ... $newContext = array(); foreach ($record['context'] as $key => $val) { ... if (false === strpos($record['message'], '{'.$key.'}')) { $newContext[$key] = $val; } } $record['context'] = $newContext; ... ``` if the user wants to keep the context in his message, you could make it an option by introducing a boolean parameter in the constructor. what do you think about?
kerem closed this issue 2026-03-04 02:16:03 +03:00
Author
Owner

@Seldaek commented on GitHub (Aug 15, 2019):

There is already a $removeUsedContextFields option in the PsrLogMessageProcessor in Monolog 2.0 so closing.

<!-- gh-comment-id:521766783 --> @Seldaek commented on GitHub (Aug 15, 2019): There is already a `$removeUsedContextFields` option in the PsrLogMessageProcessor in Monolog 2.0 so closing.
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#570
No description provided.