diff --git a/CHANGELOG.md b/CHANGELOG.md index 0be4d4103a6d1bc251fb74f675c641ca0d5f186e..1bf5062826d310d392e32fab4f052031a7cdebab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ ## [Unreleased] ### Fixed -- Fixed no handler actually assigned when using combination of - NullHandler and actual one +- Fixed no handler actually assigned when using combination of NullHandler and actual one +- WooCommerce channel is now taken from logger registered channel ## [1.7.1] - 2022-04-15 ### Fixed diff --git a/src/SimpleLoggerFactory.php b/src/SimpleLoggerFactory.php index 169877cbd1c2c1bdbf7bcd2ca19b53c7b10052be..cfd2c0dff1d4af4e7d04c14150a7d1793171af3b 100644 --- a/src/SimpleLoggerFactory.php +++ b/src/SimpleLoggerFactory.php @@ -36,7 +36,7 @@ final class SimpleLoggerFactory implements LoggerFactory { $logger->pushHandler( new WooCommerceHandler( \wc_get_logger(), - $this->options->levels + $this->channel ) ); } diff --git a/src/WC/WooCommerceHandler.php b/src/WC/WooCommerceHandler.php index 9e6da943195898ab1a655833d86de8e10b980306..960f9df6ce291a4054883ac33f03141df68b857a 100644 --- a/src/WC/WooCommerceHandler.php +++ b/src/WC/WooCommerceHandler.php @@ -9,36 +9,43 @@ use Monolog\Logger; * Class WooCommerceFactory */ class WooCommerceHandler extends AbstractProcessingHandler { - const DEFAULT_WC_SOURCE = 'wpdesk-logger'; + const DEFAULT_WC_SOURCE = 'wpdesk-logger'; /** @var \WC_Logger_Interface */ private $wc_logger; - /** - * Writes the record down to the log of the implementing handler - * - * @param array $record - * @return void - */ - protected function write(array $record) - { - $context = array_merge([ - 'source' => self::DEFAULT_WC_SOURCE - ], $record['extra'], $record['context']); - - $this->wc_logger->log($this->convertMonologLevelToWC($record['level']), $record['message'], $context); + /** @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 ) { + $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) { - return Logger::getLevelName($level); - } + /** + * @param int $level + * @return string + */ + private function convertMonologLevelToWC( $level ) { + return Logger::getLevelName( $level ); + } - public function __construct(\WC_Logger_Interface $originalWcLogger) { + public function __construct( \WC_Logger_Interface $originalWcLogger, string $channel = self::DEFAULT_WC_SOURCE ) { parent::__construct(); $this->wc_logger = $originalWcLogger; + $this->channel = $channel; } }