inpsyde / WP-Stash

Bridge between WordPress and StashPHP, providing a PSR6-compliant caching system for WordPress
https://inpsyde.com/en/wordpress-caching-system-wpstash/
GNU General Public License v2.0
51 stars 12 forks source link

Adding PSR-Logger to Pool #11

Closed Chrico closed 5 years ago

Chrico commented 5 years ago

Currently there is no way to debug the mu-plugin itself. Stash allows to add some logging to every Stash\Pool [1]

This can be added when creating instances of the Pool via Stash\Pool::setLogger().

I would like to see something like a ActionLogger which just triggers a WordPress action:

<?php declare(strict_types=1); # -*- coding: utf-8 -*-

namespace Inpsyde\WpStash\Debug;

use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

class ActionLogger implements LoggerInterface
{
    const ACTION = 'wp-stash';

    public function log($level, $message, array $context = [])
    {
        do_action(self::ACTION . strtolower($level), $message, $context);
    }

    public function emergency($message, array $context = [])
    {
        $this->log(LogLevel::EMERGENCY, $message, $context);
    }

    public function alert($message, array $context = [])
    {
        $this->log(LogLevel::ALERT, $message, $context);
    }

    public function critical($message, array $context = [])
    {
        $this->log(LogLevel::CRITICAL, $message, $context);
    }

    public function error($message, array $context = [])
    {
        $this->log(LogLevel::ERROR, $message, $context);
    }

    public function warning($message, array $context = [])
    {
        $this->log(LogLevel::WARNING, $message, $context);
    }

    public function notice($message, array $context = [])
    {
        $this->log(LogLevel::NOTICE, $message, $context);
    }

    public function info($message, array $context = [])
    {
        $this->log(LogLevel::INFO, $message, $context);
    }

    public function debug($message, array $context = [])
    {
        $this->log(LogLevel::DEBUG, $message, $context);
    }
}

[1] http://www.stashphp.com/Integration.html#logging

Chrico commented 5 years ago

@Biont added init implementation. Would be nice to have some review/opinion. :)

Maybe needs also some documentation about the hook.

Biont commented 5 years ago

@Chrico Can we put that behind WP_DEBUG and/or otherwise make this optional?

Chrico commented 5 years ago

Since it just fires an actions on exceptions, it will not hurt to add this logger. Maybe we could use the NullLogger if no WP_DEBUG, but e.G. packages like Wonolog are still working, even if WP_DEBUG is disabled. :)

Biont commented 5 years ago

I might run a few performance tests when I find the time. Yes, this logger is probably the most lightweight way of handling it imaginable, so I doubt it will cause any slowdown, but I would rather be sure about it. An exception via config is easily added later, so I'm in no hurry, though

Biont commented 5 years ago

Released in version 3.1.0