diff --git a/src/Settings.php b/src/Settings.php index aa648ce8e7e77c40a93b6c6c5300a0a003037ab9..1fa6f1be833aa8b5a0074f92bb1546b73b6a22ea 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -4,6 +4,9 @@ namespace WPDesk\Logger; use Psr\Log\LogLevel; +/** + * @deprecated + */ final class Settings { /** @var string */ @@ -26,4 +29,12 @@ final class Settings { $this->use_wp_log = $use_wp_log; } + public function to_array(): array { + return [ + 'level' => $this->level, + 'use_wc_log' => $this->use_wc_log, + 'use_wp_log' => $this->use_wp_log, + ]; + } + } diff --git a/src/SimpleLoggerFactory.php b/src/SimpleLoggerFactory.php index 432be72df25962f2c9b697f28a5d237e3e9bfa36..13eddc7cd79d4438f5757aa4354b9d88da9a2d4e 100644 --- a/src/SimpleLoggerFactory.php +++ b/src/SimpleLoggerFactory.php @@ -3,15 +3,23 @@ declare( strict_types=1 ); namespace WPDesk\Logger; +use Monolog\Handler\FingersCrossedHandler; use Monolog\Handler\HandlerInterface; -use Monolog\Handler\NullHandler; 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 Settings */ + /** + * @var array{ + * level?: string, + * action_level?: string|null, + * } + */ private $options; /** @var string */ @@ -20,9 +28,25 @@ final class SimpleLoggerFactory implements LoggerFactory { /** @var Logger */ private $logger; - public function __construct( string $channel, Settings $options = null ) { + /** + * 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; - $this->options = $options ?? new Settings(); + + 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 { @@ -30,19 +54,26 @@ final class SimpleLoggerFactory implements LoggerFactory { return $this->logger; } - $this->logger = new Logger( $this->channel ); + $this->logger = new Logger( + $this->channel, + [], + [ new PsrLogMessageProcessor( null, true ), new UidProcessor() ], + wp_timezone() + ); - if ( $this->options->use_wc_log ) { - if ( \function_exists( 'wc_get_logger' ) && \did_action( 'woocommerce_init' ) ) { - $this->create_wc_handler(); - } else { - \add_action( 'woocommerce_init', [ $this, 'create_wc_handler' ] ); - } + if ( \function_exists( 'wc_get_logger' ) && \did_action( 'woocommerce_init' ) ) { + $this->set_wc_handler(); + } else { + \add_action( 'woocommerce_init', [ $this, 'set_wc_handler' ] ); } - // Adding WooCommerce logger may have failed, if so add WP by default. - if ( $this->options->use_wp_log || empty( $this->logger->getHandlers() ) ) { - $this->logger->pushHandler( $this->get_wp_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; @@ -51,24 +82,19 @@ final class SimpleLoggerFactory implements LoggerFactory { /** * @internal */ - public function create_wc_handler() { - while ( ! $this->options->use_wp_log && ! empty( $this->logger->getHandlers() ) ) { - $this->logger->popHandler(); - } - $this->logger->pushHandler( - new WooCommerceHandler( - \wc_get_logger(), - $this->channel - ) + public function set_wc_handler(): void { + $this->set_handler( + $this->logger, + new WooCommerceHandler( \wc_get_logger(), $this->channel ) ); } - private function get_wp_handler(): HandlerInterface { - if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { - return new ErrorLogHandler( ErrorLogHandler::OPERATING_SYSTEM, $this->options->level ); + private function set_handler( Logger $logger, HandlerInterface $handler ): void { + if ( is_string( $this->options['action_level'] ) ) { + $handler = new FingersCrossedHandler( $handler, $this->options['action_level'] ); } - return new NullHandler(); + // Purposefully replace all existing handlers. + $logger->setHandlers( [ $handler ] ); } - }