diff --git a/src/SimpleLoggerFactory.php b/src/SimpleLoggerFactory.php index 8d4088138a3c21497008790c2d40a924ae46c51d..ddd46bbf09a42ffca3330647efa8046e7dac1b20 100644 --- a/src/SimpleLoggerFactory.php +++ b/src/SimpleLoggerFactory.php @@ -4,7 +4,6 @@ declare( strict_types=1 ); namespace WPDesk\Logger; use Monolog\Handler\HandlerInterface; -use Monolog\Handler\NullHandler; use Monolog\Logger; use Monolog\Handler\ErrorLogHandler; use Monolog\Processor\PsrLogMessageProcessor; @@ -39,16 +38,14 @@ final class SimpleLoggerFactory implements LoggerFactory { 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->create_wc_handler(); + } else { + \add_action( 'woocommerce_init', [ $this, 'create_wc_handler' ] ); } // Adding WooCommerce logger may have failed, if so add WP by default. - if ( $this->options->use_wp_log || empty( $this->logger->getHandlers() ) ) { + if ( empty( $this->logger->getHandlers() ) ) { $this->logger->pushHandler( $this->get_wp_handler() ); } @@ -58,24 +55,15 @@ 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 ); - } - - return new NullHandler(); + private function set_handler( Logger $logger, HandlerInterface $handler ): void { + // Purposefully replace all existing handlers. + $logger->setHandlers( [ $handler ] ); } - }