Skip to content

Conversation

aivis
Copy link
Contributor

@aivis aivis commented Apr 12, 2022

Log filter

The Understand service provider utilises the event dispatcher (MessageLogged event) to capture and deliver logs. This makes it impossible to filter specific log types at the application level even if a custom Laravel log driver is created (the event dispatcher still fires the event).

A good use case example for this is to filter out PHP8 deprecation warnings.

Example filter class

<?php
// config (understand-laravel.php)
'log_filter' => \App\Logging\UnderstandLogFilter::class,

// a hypothetical filter class
<?php

declare(strict_types=1);

namespace App\Logging;

use Illuminate\Support\Str;

class UnderstandLogFilter
{
    public function __invoke($level, $message, $context): bool
    {
        if ($level === 'warning' && Str::contains(strtolower($message), 'deprecated')) {
            return true;
        }

        return false;
    }
}

Configuration details

The configuration value (filter) must be a callable type:

The suggested way would be to create an invokable class since it's hard to serialise anonymous functions (Laravel config cache):

The log (callable) filter interface is as follows: $callable($level, $message, $context).

The result of the filter must be a boolean value:

  • TRUE, the log should be ignored and NOT delivered to Understand.io
  • FALSE, the log should be delivered to Understand.io

The ignored_logs config value has higher precedence than log_filter.

@mauricius mauricius merged commit 473bd64 into understand:master Apr 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants