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

Merge branch 'master' into feat/request-id

parents 51a716e4 4a3ef36d
No related branches found
No related tags found
1 merge request!25feat: add request id to logs
Pipeline #133396 passed with stages
in 52 seconds
## [1.11.0] - 2022-11-28
### Added
- Inserted request id in log entries
## [1.10.2] - 2022-11-15
### Fixed
- Deprecated error in PHP 8.X
## [1.10.1] - 2022-10-01
### Fixed
- WC logger initialisation
## [1.10.0] - 2022-08-16
### Added
- en_CA, en_GB translators
......
......@@ -15,4 +15,15 @@ final class Settings {
/** @var bool */
public $use_wp_log = true;
/**
* @param string $level
* @param bool $use_wc_log
* @param bool $use_wp_log
*/
public function __construct( string $level = LogLevel::DEBUG, bool $use_wc_log = true, bool $use_wp_log = true ) {
$this->level = $level;
$this->use_wc_log = $use_wc_log;
$this->use_wp_log = $use_wp_log;
}
}
......@@ -21,9 +21,13 @@ final class SimpleLoggerFactory implements LoggerFactory {
/** @var Logger */
private $logger;
/** @var string */
private $request_id;
public function __construct( string $channel, Settings $options = null ) {
$this->channel = $channel;
$this->options = $options ?? new Settings();
$this->channel = $channel;
$this->options = $options ?? new Settings();
$this->request_id = uniqid('', true);
}
public function getLogger( $name = null ): Logger {
......@@ -31,25 +35,40 @@ final class SimpleLoggerFactory implements LoggerFactory {
return $this->logger;
}
$logger = new Logger( $this->channel );
$this->logger = new Logger( $this->channel );
if ( $this->options->use_wc_log && \function_exists( 'wc_get_logger' ) ) {
$handler = new WooCommerceHandler( \wc_get_logger(), $this->channel );
if ( $this->options->use_wc_log ) {
if ( \function_exists( 'wc_get_logger' ) ) {
$this->create_wc_handler();
} else {
\add_action( 'woocommerce_init', [ $this, 'create_wc_handler' ] );
}
}
// Adding WooCommerce logger may have failed, if so add WP by default.
if ( $this->options->use_wp_log || empty( $logger->getHandlers() ) ) {
if ( $this->options->use_wp_log || empty( $this->logger->getHandlers() ) ) {
$handler = $this->get_wp_handler();
$handler->setFormatter( $this->get_formatter() );
$this->logger->pushHandler( $handler );
}
$request_id = uniqid('', true);
$handler->setFormatter(
new LineFormatter( "[%datetime%] %channel%.%level_name%: %message% %context% %extra%, requestId: $request_id" )
);
return $this->logger;
}
$logger->pushHandler( $handler );
/**
* @internal
*/
public function create_wc_handler() {
while ( ! $this->options->use_wp_log && ! empty( $this->logger->getHandlers() ) ) {
$this->logger->popHandler();
}
$handler = new WooCommerceHandler( \wc_get_logger(), $this->channel );
$handler->setFromatter( $this->get_formatter() );
$this->logger->pushHandler( $handler );
}
return $this->logger = $logger;
private function get_formatter(): LineFormatter {
return new LineFormatter( "[%datetime%] %channel%.%level_name%: %message% %context% %extra%, requestId: $request_id" );
}
private function get_wp_handler(): HandlerInterface {
......
......@@ -42,7 +42,7 @@ if ( ! class_exists( 'WPDesk_Logger_Factory' ) ) {
*
* @see http://php.net/manual/en/function.debug-backtrace.php
*/
public static function log_wp_error( WP_Error $e, array $backtrace ) {
public static function log_wp_error( WP_Error $e, array $backtrace = array() ) {
$message = 'Error: ' . get_class( $e ) . ' Code: ' . $e->get_error_code() . ' Message: ' . $e->get_error_message();
self::log_message_backtrace( $message, WPDesk_Logger::ERROR, $backtrace );
......@@ -79,7 +79,7 @@ if ( ! class_exists( 'WPDesk_Logger_Factory' ) ) {
* @param string $level Level of error.
* @param array $backtrace Backtrace information with snapshot of error env.
*/
public static function log_message_backtrace( $message, $level = WPDesk_Logger::DEBUG, array $backtrace ) {
public static function log_message_backtrace( $message, $level = WPDesk_Logger::DEBUG, array $backtrace = array() ) {
$message .= ' Backtrace: ' . json_encode( $backtrace );
if (isset($backtrace[ self::BACKTRACE_FILENAME_KEY ])) {
$filename = $backtrace[ self::BACKTRACE_FILENAME_KEY ];
......
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