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

Merge branch 'feat/message-handling'

parents a98f738c b2644b4b
No related branches found
No related tags found
1 merge request!29feat/message handling
...@@ -4,6 +4,9 @@ namespace WPDesk\Logger; ...@@ -4,6 +4,9 @@ namespace WPDesk\Logger;
use Psr\Log\LogLevel; use Psr\Log\LogLevel;
/**
* @deprecated
*/
final class Settings { final class Settings {
/** @var string */ /** @var string */
...@@ -26,4 +29,12 @@ final class Settings { ...@@ -26,4 +29,12 @@ final class Settings {
$this->use_wp_log = $use_wp_log; $this->use_wp_log = $use_wp_log;
} }
public function to_array(): array {
return [
'level' => $this->level,
'use_wc_log' => $this->use_wc_log,
'use_wp_log' => $this->use_wp_log,
];
}
} }
...@@ -3,15 +3,23 @@ declare( strict_types=1 ); ...@@ -3,15 +3,23 @@ declare( strict_types=1 );
namespace WPDesk\Logger; namespace WPDesk\Logger;
use Monolog\Handler\FingersCrossedHandler;
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\UidProcessor;
use Psr\Log\LogLevel;
use WPDesk\Logger\WC\WooCommerceHandler; use WPDesk\Logger\WC\WooCommerceHandler;
final class SimpleLoggerFactory implements LoggerFactory { final class SimpleLoggerFactory implements LoggerFactory {
/** @var Settings */ /**
* @var array{
* level?: string,
* action_level?: string|null,
* }
*/
private $options; private $options;
/** @var string */ /** @var string */
...@@ -20,9 +28,25 @@ final class SimpleLoggerFactory implements LoggerFactory { ...@@ -20,9 +28,25 @@ final class SimpleLoggerFactory implements LoggerFactory {
/** @var Logger */ /** @var Logger */
private $logger; private $logger;
public function __construct( string $channel, Settings $options = null ) { /**
* 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; $this->channel = $channel;
$this->options = $options ?? new Settings();
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 { public function getLogger( $name = null ): Logger {
...@@ -30,19 +54,26 @@ final class SimpleLoggerFactory implements LoggerFactory { ...@@ -30,19 +54,26 @@ final class SimpleLoggerFactory implements LoggerFactory {
return $this->logger; return $this->logger;
} }
$this->logger = new Logger( $this->channel ); $this->logger = new Logger(
$this->channel,
[],
[ new PsrLogMessageProcessor( null, true ), new UidProcessor() ],
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->set_wc_handler();
$this->create_wc_handler(); } else {
} else { \add_action( 'woocommerce_init', [ $this, 'set_wc_handler' ] );
\add_action( 'woocommerce_init', [ $this, 'create_wc_handler' ] );
}
} }
// Adding WooCommerce logger may have failed, if so add WP by default. // In the worst-case scenario, when WC logs are not available (yet, or at all),
if ( $this->options->use_wp_log || empty( $this->logger->getHandlers() ) ) { // fallback to WP logs, but only when enabled.
$this->logger->pushHandler( $this->get_wp_handler() ); 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; return $this->logger;
...@@ -51,24 +82,19 @@ final class SimpleLoggerFactory implements LoggerFactory { ...@@ -51,24 +82,19 @@ 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 ) { if ( is_string( $this->options['action_level'] ) ) {
return new ErrorLogHandler( ErrorLogHandler::OPERATING_SYSTEM, $this->options->level ); $handler = new FingersCrossedHandler( $handler, $this->options['action_level'] );
} }
return new NullHandler(); // Purposefully replace all existing handlers.
$logger->setHandlers( [ $handler ] );
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment