Select Git revision
SimpleLoggerFactory.php
-
Bartek Jaskulski authored
In situation, when we don't collect verbose logs, but we still may be interested in any errors occurring on client's website and being able to investigate those after gaining access, we may make use of `action_level` option in `SimpleLoggerFactory`. With this option set to some level, we configure `FingersCrossedHandler` which will ensure, when action level is hit, all log stack is persisted. Signed-off-by:
Bart Jaskulski <bjaskulski@protonmail.com>
Bartek Jaskulski authoredIn situation, when we don't collect verbose logs, but we still may be interested in any errors occurring on client's website and being able to investigate those after gaining access, we may make use of `action_level` option in `SimpleLoggerFactory`. With this option set to some level, we configure `FingersCrossedHandler` which will ensure, when action level is hit, all log stack is persisted. Signed-off-by:
Bart Jaskulski <bjaskulski@protonmail.com>
SimpleLoggerFactory.php 2.51 KiB
<?php
declare( strict_types=1 );
namespace WPDesk\Logger;
use Monolog\Handler\FingersCrossedHandler;
use Monolog\Handler\HandlerInterface;
use Monolog\Logger;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Processor\PsrLogMessageProcessor;
use Monolog\Processor\UidProcessor;
use Psr\Log\LogLevel;
use WPDesk\Logger\WC\WooCommerceHandler;
final class SimpleLoggerFactory implements LoggerFactory {
/**
* @var array{
* level?: string,
* action_level?: string|null,
* }
*/
private $options;
/** @var string */
private $channel;
/** @var Logger */
private $logger;
/**
* Valid options are:
* * level (default debug): Default logging level
* * action_level: If value is set, it will act as the minimum level at which logger will be triggered using FingersCrossedHandler {@see https://seldaek.github.io/monolog/doc/02-handlers-formatters-processors.html#wrappers--special-handlers}
*/
public function __construct( string $channel, $options = null ) {
$this->channel = $channel;
if ( $options instanceof Settings ) {
$options = $options->to_array();
}
$this->options = array_merge(
[
'level' => LogLevel::DEBUG,
'action_level' => null,
],
$options
);
}
public function getLogger( $name = null ): Logger {
if ( $this->logger ) {
return $this->logger;
}
$this->logger = new Logger(
$this->channel,
[],
[ new PsrLogMessageProcessor( null, true ), new UidProcessor() ],
wp_timezone()
);
if ( \function_exists( 'wc_get_logger' ) && \did_action( 'woocommerce_init' ) ) {
$this->set_wc_handler();
} else {
\add_action( 'woocommerce_init', [ $this, 'set_wc_handler' ] );
}
// In the worst-case scenario, when WC logs are not available (yet, or at all),
// fallback to WP logs, but only when enabled.
if ( empty( $this->logger->getHandlers() ) && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
$this->set_handler(
$this->logger,
new ErrorLogHandler( ErrorLogHandler::OPERATING_SYSTEM, $this->options['level'] );
);
}
return $this->logger;
}
/**
* @internal
*/
public function set_wc_handler(): void {
$this->set_handler(
$this->logger,
new WooCommerceHandler( \wc_get_logger(), $this->channel )
);
}
private function set_handler( Logger $logger, HandlerInterface $handler ): void {
if ( is_string( $this->options['action_level'] ) ) {
$handler = new FingersCrossedHandler( $handler, $this->options['action_level'] );
}
// Purposefully replace all existing handlers.
$logger->setHandlers( [ $handler ] );
}
}