Skip to content
Snippets Groups Projects
Select Git revision
  • 8b7a09c089aca3cc43249e4f44bad54a5cfd6c59
  • master default protected
  • feat/message-handling
  • feat/request-id
  • 1.13.2
  • 1.13.1
  • 1.13.0
  • 1.12.1
  • 1.12.0
  • 1.11.0
  • 1.11.0-beta2
  • 1.11.0-beta1
  • 1.10.2
  • 1.10.1
  • 1.10.0
  • 1.9.0
  • 1.8.0
  • 1.7.4
  • 1.7.3
  • 1.7.2
  • 1.7.1
  • 1.7.0
  • 1.6.2
  • 1.6.2-beta2
24 results

WooCommerceHandler.php

Blame
  • WooCommerceHandler.php 1.10 KiB
    <?php
    
    namespace WPDesk\Logger\WC;
    
    use Monolog\Handler\AbstractProcessingHandler;
    use Monolog\Logger;
    
    /**
     * Class WooCommerceFactory
     */
    class WooCommerceHandler extends AbstractProcessingHandler {
    	const DEFAULT_WC_SOURCE = 'wpdesk-logger';
    
    	/** @var \WC_Logger_Interface */
    	private $wc_logger;
    
    	/** @var string */
    	private $channel;
    
    	/**
    	 * Writes the record down to the log of the implementing handler
    	 *
    	 * @param  array $record
    	 * @return void
    	 */
    	protected function write( array $record ): void {
    		$context = array_merge(
    			[
    				'source' => $this->channel,
    			],
    			$record['extra'],
    			$record['context']
    		);
    
    		$this->wc_logger->log( $this->convertMonologLevelToWC( $record['level'] ), $record['message'], $context );
    	}
    
    	/**
    	 * @param int $level
    	 * @return string
    	 */
    	private function convertMonologLevelToWC( $level ): string {
    		return Logger::getLevelName( $level );
    	}
    
    	public function __construct( \WC_Logger_Interface $originalWcLogger, string $channel = self::DEFAULT_WC_SOURCE ) {
    		parent::__construct();
    		$this->wc_logger = $originalWcLogger;
    		$this->channel   = $channel;
    	}
    }