diff --git a/CHANGELOG.md b/CHANGELOG.md index 69cf19ad399e87f5ee81bccfc1b3c6d5fcbbf921..fa7a08c4073b64a1173fa296fe13e13d5e1cfa1c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [1.10.1] - 2022-10-01 +### Fixed +- WC logger initialisation + ## [1.10.0] - 2022-08-16 ### Added - en_CA, en_GB translators diff --git a/src/Settings.php b/src/Settings.php index 07fa9788b75581684c2f736db2c6da716100f3cb..aa648ce8e7e77c40a93b6c6c5300a0a003037ab9 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -15,4 +15,15 @@ final class Settings { /** @var bool */ public $use_wp_log = true; + /** + * @param string $level + * @param bool $use_wc_log + * @param bool $use_wp_log + */ + public function __construct( string $level = LogLevel::DEBUG, bool $use_wc_log = true, bool $use_wp_log = true ) { + $this->level = $level; + $this->use_wc_log = $use_wc_log; + $this->use_wp_log = $use_wp_log; + } + } diff --git a/src/SimpleLoggerFactory.php b/src/SimpleLoggerFactory.php index cfd2c0dff1d4af4e7d04c14150a7d1793171af3b..cf88a942db65ab9b6cf927da30b79f326c17635b 100644 --- a/src/SimpleLoggerFactory.php +++ b/src/SimpleLoggerFactory.php @@ -30,23 +30,37 @@ final class SimpleLoggerFactory implements LoggerFactory { return $this->logger; } - $logger = new Logger( $this->channel ); - - if ( $this->options->use_wc_log && \function_exists( 'wc_get_logger' ) ) { - $logger->pushHandler( - new WooCommerceHandler( - \wc_get_logger(), - $this->channel - ) - ); + $this->logger = new Logger( $this->channel ); + + if ( $this->options->use_wc_log ) { + if ( \function_exists( 'wc_get_logger' ) ) { + $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( $logger->getHandlers() ) ) { - $logger->pushHandler( $this->get_wp_handler() ); + if ( $this->options->use_wp_log || empty( $this->logger->getHandlers() ) ) { + $this->logger->pushHandler( $this->get_wp_handler() ); } - return $this->logger = $logger; + return $this->logger; + } + + /** + * @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 + ) + ); } private function get_wp_handler(): HandlerInterface {