Skip to content
Snippets Groups Projects
Select Git revision
  • 8f22d9a79b503bc611b34f65d724fca95baa1926
  • master default protected
  • bugfix/wordpress-review
  • fix/duplicate
  • bugfix/get_current_screen_fail
  • feature/dismiss-nonce
  • replace-dodgy-path
  • bugfix/notice-not-show
  • devel
  • 3.3.0
  • 3.2.5
  • 3.2.4
  • 3.2.3
  • 3.2.2
  • 3.2.1
  • 3.2.0
  • 3.2.0-beta7
  • 3.2.0-beta6
  • 3.2.0-beta5
  • 3.2.0-beta4
  • 3.2.0-beta3
  • 3.2.0-beta2
  • 3.2.0-beta1
  • 3.1.4
  • 3.1.4-beta1
  • 3.1.3
  • 3.1.1
  • 3.1
  • 3.0
29 results

notice.js

Blame
  • SimpleLoggerFactory.php 1.78 KiB
    <?php
    declare( strict_types=1 );
    
    namespace WPDesk\Logger;
    
    use Monolog\Handler\HandlerInterface;
    use Monolog\Logger;
    use Monolog\Handler\ErrorLogHandler;
    use Monolog\Processor\PsrLogMessageProcessor;
    use Monolog\Processor\UidProcessor;
    use WPDesk\Logger\WC\WooCommerceHandler;
    
    final class SimpleLoggerFactory implements LoggerFactory {
    
    	/** @var Settings */
    	private $options;
    
    	/** @var string */
    	private $channel;
    
    	/** @var Logger */
    	private $logger;
    
    	public function __construct( string $channel, Settings $options = null ) {
    		$this->channel = $channel;
    		$this->options = $options ?? new Settings();
    	}
    
    	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->create_wc_handler();
    		} else {
    			\add_action( 'woocommerce_init', [ $this, 'create_wc_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;
    	}
    
    	/**
    	 * @internal
    	 */
    	public function set_wc_handler(): void {
    		$this->set_handler(
    			$this->logger,
    			new WooCommerceHandler( \wc_get_logger(), $this->channel )
    		);
    	}
    
    	private function set_handler( Logger $logger, HandlerInterface $handler ): void {
    		// Purposefully replace all existing handlers.
    		$logger->setHandlers( [ $handler ] );
    	}
    }