Select Git revision
Basic_Requirement_Checker.php
-
Grzegorz Rola authoredGrzegorz Rola authored
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),