Skip to content
Snippets Groups Projects
Select Git revision
  • ce355fd67828c5cc80dd2813c7edba21fe323c2e
  • master default protected
  • bugfix/wordpress-review
  • bugfix/prevent-error-notice
  • remove-arrow
  • feature/update-message
  • feature/minimum-plugin-version-check-demo1
  • feature/plugin-name
  • 3.7.1
  • 3.7.0
  • 3.6.3
  • 3.6.2
  • 3.6.1
  • 3.6.0
  • 3.6.0-beta3
  • 3.6.0-beta2
  • 3.6.0-beta1
  • 3.5.2
  • 3.5.1
  • 3.5.0
  • 3.4.0
  • 3.3.0
  • 3.2.8
  • 3.2.7
  • 3.2.6
  • 3.2.5
  • 3.2.4
  • 3.2.3
28 results

Basic_Requirement_Checker.php

Blame
  • SimpleLoggerFactory.php 2.51 KiB
    <?php
    declare( strict_types=1 );
    
    namespace WPDesk\Logger;
    
    use Monolog\Handler\FingersCrossedHandler;
    use Monolog\Handler\HandlerInterface;
    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 array{
    	 *   level?: string,
    	 *   action_level?: string|null,
    	 * }
    	 */
    	private $options;
    
    	/** @var string */
    	private $channel;
    
    	/** @var Logger */
    	private $logger;
    
    	/**
    	 * 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;
    
    		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 {
    		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->set_wc_handler();
    		} else {
    			\add_action( 'woocommerce_init', [ $this, 'set_wc_handler' ] );
    		}
    
    		// In the worst-case scenario, when WC logs are not available (yet, or at all),