Skip to content
Snippets Groups Projects
Verified Commit c8be4966 authored by Bartek Jaskulski's avatar Bartek Jaskulski
Browse files

feat: always use WC logger if available


Remove option, allowing to disable WooCommerce logger. If WC is
available, this log should always be used unconditionally.

Signed-off-by: default avatarBart Jaskulski <bjaskulski@protonmail.com>
parent 78bab06f
No related branches found
No related tags found
1 merge request!29feat/message handling
...@@ -4,7 +4,6 @@ declare( strict_types=1 ); ...@@ -4,7 +4,6 @@ declare( strict_types=1 );
namespace WPDesk\Logger; namespace WPDesk\Logger;
use Monolog\Handler\HandlerInterface; use Monolog\Handler\HandlerInterface;
use Monolog\Handler\NullHandler;
use Monolog\Logger; use Monolog\Logger;
use Monolog\Handler\ErrorLogHandler; use Monolog\Handler\ErrorLogHandler;
use Monolog\Processor\PsrLogMessageProcessor; use Monolog\Processor\PsrLogMessageProcessor;
...@@ -39,16 +38,14 @@ final class SimpleLoggerFactory implements LoggerFactory { ...@@ -39,16 +38,14 @@ final class SimpleLoggerFactory implements LoggerFactory {
wp_timezone() wp_timezone()
); );
if ( $this->options->use_wc_log ) {
if ( \function_exists( 'wc_get_logger' ) && \did_action( 'woocommerce_init' ) ) { if ( \function_exists( 'wc_get_logger' ) && \did_action( 'woocommerce_init' ) ) {
$this->create_wc_handler(); $this->create_wc_handler();
} else { } else {
\add_action( 'woocommerce_init', [ $this, 'create_wc_handler' ] ); \add_action( 'woocommerce_init', [ $this, 'create_wc_handler' ] );
} }
}
// Adding WooCommerce logger may have failed, if so add WP by default. // 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() ); $this->logger->pushHandler( $this->get_wp_handler() );
} }
...@@ -58,24 +55,15 @@ final class SimpleLoggerFactory implements LoggerFactory { ...@@ -58,24 +55,15 @@ final class SimpleLoggerFactory implements LoggerFactory {
/** /**
* @internal * @internal
*/ */
public function create_wc_handler() { public function set_wc_handler(): void {
while ( ! $this->options->use_wp_log && ! empty( $this->logger->getHandlers() ) ) { $this->set_handler(
$this->logger->popHandler(); $this->logger,
} new WooCommerceHandler( \wc_get_logger(), $this->channel )
$this->logger->pushHandler(
new WooCommerceHandler(
\wc_get_logger(),
$this->channel
)
); );
} }
private function get_wp_handler(): HandlerInterface { private function set_handler( Logger $logger, HandlerInterface $handler ): void {
if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { // Purposefully replace all existing handlers.
return new ErrorLogHandler( ErrorLogHandler::OPERATING_SYSTEM, $this->options->level ); $logger->setHandlers( [ $handler ] );
} }
return new NullHandler();
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment