[GH-ISSUE #1069] Filter out some log messages based on runtime config? #434

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

Originally created by @howardjones on GitHub (Oct 29, 2017).
Original GitHub issue: https://github.com/Seldaek/monolog/issues/1069

In my app, I use error codes on warnings, and allow users to mute certain warnings (e.g. "--no-warn WARN22"). I plan to use monolog (1.23 for now, until I stop supporting php 5.6) and use the context data to tag the warning with the error code if it has one.

But, how can I implement the muting/filtering behaviour in monolog? They'll all be the same log level. Can a processor return null or an empty event, to indicate that the event shouldn't be logged? Internally I already have a list of currently-muted error codes to check against.

Originally created by @howardjones on GitHub (Oct 29, 2017). Original GitHub issue: https://github.com/Seldaek/monolog/issues/1069 In my app, I use error codes on warnings, and allow users to mute certain warnings (e.g. "--no-warn WARN22"). I plan to use monolog (1.23 for now, until I stop supporting php 5.6) and use the context data to tag the warning with the error code if it has one. But, how can I implement the muting/filtering behaviour in monolog? They'll all be the same log level. Can a processor return null or an empty event, to indicate that the event shouldn't be logged? Internally I already have a list of currently-muted error codes to check against.
kerem 2026-03-04 02:15:00 +03:00
  • closed this issue
  • added the
    Support
    label
Author
Owner

@howardjones commented on GitHub (Jan 2, 2018):

Answering my own issue - you can't return null, but you can set the level to something non-existent...

class SuppressProcessor
{
    private $suppressed = array();

    public function addSuppressedCode($code)
    {
        $this->suppressed[$code] = true;
    }

    public function removeSuppressedCode($code)
    {
        unset($this->suppressed[$code]);
    }

    public function __invoke(array $record)
    {
        if (isset($record['context']['code']) && isset($this->suppressed[$record['context']['code']])) {
            $record['level'] = "SUPPRESS";
        }

        return $record;
    }
}
<!-- gh-comment-id:354686724 --> @howardjones commented on GitHub (Jan 2, 2018): Answering my own issue - you can't return null, but you can set the level to something non-existent... ``` class SuppressProcessor { private $suppressed = array(); public function addSuppressedCode($code) { $this->suppressed[$code] = true; } public function removeSuppressedCode($code) { unset($this->suppressed[$code]); } public function __invoke(array $record) { if (isset($record['context']['code']) && isset($this->suppressed[$record['context']['code']])) { $record['level'] = "SUPPRESS"; } return $record; } } ```
Author
Owner

@Seldaek commented on GitHub (Jun 8, 2018):

You could also use a wrapping handler instead of a processor, to filter what gets passed down to the other handlers. That'd be the more "monolog" way to do this.

<!-- gh-comment-id:395846388 --> @Seldaek commented on GitHub (Jun 8, 2018): You could also use a wrapping handler instead of a processor, to filter what gets passed down to the other handlers. That'd be the more "monolog" way to do this.
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#434
No description provided.