Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: best practice to lower $errorLogLevel in HttpApiListener #83

Open
1 task done
frugan-dev opened this issue Aug 6, 2024 · 2 comments
Open
1 task done

Comments

@frugan-dev
Copy link

frugan-dev commented Aug 6, 2024

Description of the bug

Using version 2.x I would need to lower $errorLogLevel in HttpApiListener listener from LogLevel::ERROR to LogLevel::WARNING.
The procedure I followed works using PHP-DI (see below), I would like to know if it is the correct one or if there is a better approach.

use Inpsyde\Wonolog\LogLevel;
use Inpsyde\Wonolog\HookListener\HttpApiListener;

return [
    HttpApiListener::class => DI\create()
        ->constructor(DI\value(LogLevel::WARNING)),
];
use Inpsyde\Wonolog\HookListener\HttpApiListener;

add_action(
    'wonolog.setup',
    function (Inpsyde\Wonolog\Configurator $config) use ($container) {
        $config->disableDefaultHookListeners(
                HttpApiListener::class
            )
            ->addActionListener(
                $container->get(HttpApiListener::class)
            )
        ;
    }
);

Initially I also tried to create a custom HttpApiListener class that extended Inpsyde\Wonolog\HookListener\HttpApiListener by overriding the __construct(int $errorLogLevel = LogLevel::WARNING), but then I realized that the Inpsyde\Wonolog\HookListener\HttpApiListener class is defined as final.

Reproduction instructions

.

Expected behavior

.

Environment info

  • Wonolog 2.x
  • php-di 7.x
  • Docker w/ Bitnami Apache latest, Bitnami Mariadb latest and Bitnami PHP-FPM 8.2
  • Bedrock
  • WP 6.6
  • WONOLOG_DISABLE=false
  • WONOLOG_DEFAULT_MIN_LEVEL={empty}
  • WP_DEBUG=true
  • WP_DEBUG_LOG=/my-custom-path/debug.log
  • WP_DEBUG_DISPLAY=true
  • WP_ENV=development

Relevant log output

No response

Additional context

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@frugan-dev frugan-dev added the bug label Aug 6, 2024
@gmazzap
Copy link
Contributor

gmazzap commented Aug 6, 2024

The default listener classes are final because Wonolog instantiates them using new $listener() so if classes would be extended, the constructor could be extended in a non-compatible way (e.g. requiring mandatory parameters), PHP would not complain, but then a fatal error would happen.

The approach you use looks fine to me. In the end, it is like you want to use a custom listener, but that happens to use the same class as the default handler.

Maybe it would be nice to have a filter to change the log level, so that the constructor could look like:

$errorLogLevel = apply_filters('wonolog.http-error-listener-level', null);
$this->errorLogLevel = LogLevel::normalizeLevel($errorLogLevel) ?? LogLevel::ERROR;

And then you would need to add the filter:

add_filter('wonolog.http-error-listener-level', fn () => LogLevel::WARNING)

But I'm not sure this is actually better than what you're doing.

@frugan-dev
Copy link
Author

frugan-dev commented Aug 7, 2024

Hi @gmazzap, and thanks for your help!
I think it could be a useful thing to have filters to alter the LogLevels of the default listeners.
The alternative routes (e.g. dependecy injector) are not always easily accessible to everyone, or not everyone knows how to do it, so I would find it useful to provide more options available.

@gmazzap gmazzap added enhancement and removed bug labels Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants