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

feat: integrate FingersCrossedHandler


In situation, when we don't collect verbose logs, but we still may be
interested in any errors occurring on client's website and being able to
investigate those after gaining access, we may make use of
`action_level` option in `SimpleLoggerFactory`.

With this option set to some level, we configure `FingersCrossedHandler`
which will ensure, when action level is hit, all log stack is persisted.

Signed-off-by: default avatarBart Jaskulski <bjaskulski@protonmail.com>
parent 8c87a648
No related branches found
No related tags found
1 merge request!29feat/message handling
Pipeline #393175 passed with warnings with stages
in 31 seconds
...@@ -3,6 +3,7 @@ declare( strict_types=1 ); ...@@ -3,6 +3,7 @@ declare( strict_types=1 );
namespace WPDesk\Logger; namespace WPDesk\Logger;
use Monolog\Handler\FingersCrossedHandler;
use Monolog\Handler\HandlerInterface; use Monolog\Handler\HandlerInterface;
use Monolog\Logger; use Monolog\Logger;
use Monolog\Handler\ErrorLogHandler; use Monolog\Handler\ErrorLogHandler;
...@@ -16,6 +17,7 @@ final class SimpleLoggerFactory implements LoggerFactory { ...@@ -16,6 +17,7 @@ final class SimpleLoggerFactory implements LoggerFactory {
/** /**
* @var array{ * @var array{
* level?: string, * level?: string,
* action_level?: string|null,
* } * }
*/ */
private $options; private $options;
...@@ -29,6 +31,7 @@ final class SimpleLoggerFactory implements LoggerFactory { ...@@ -29,6 +31,7 @@ final class SimpleLoggerFactory implements LoggerFactory {
/** /**
* Valid options are: * Valid options are:
* * level (default debug): Default logging level * * 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 ) { public function __construct( string $channel, $options = null ) {
$this->channel = $channel; $this->channel = $channel;
...@@ -40,6 +43,7 @@ final class SimpleLoggerFactory implements LoggerFactory { ...@@ -40,6 +43,7 @@ final class SimpleLoggerFactory implements LoggerFactory {
$this->options = array_merge( $this->options = array_merge(
[ [
'level' => LogLevel::DEBUG, 'level' => LogLevel::DEBUG,
'action_level' => null,
], ],
$options $options
); );
...@@ -58,9 +62,9 @@ final class SimpleLoggerFactory implements LoggerFactory { ...@@ -58,9 +62,9 @@ final class SimpleLoggerFactory implements LoggerFactory {
); );
if ( \function_exists( 'wc_get_logger' ) && \did_action( 'woocommerce_init' ) ) { if ( \function_exists( 'wc_get_logger' ) && \did_action( 'woocommerce_init' ) ) {
$this->create_wc_handler(); $this->set_wc_handler();
} else { } else {
\add_action( 'woocommerce_init', [ $this, 'create_wc_handler' ] ); \add_action( 'woocommerce_init', [ $this, 'set_wc_handler' ] );
} }
// In the worst-case scenario, when WC logs are not available (yet, or at all), // In the worst-case scenario, when WC logs are not available (yet, or at all),
...@@ -86,6 +90,10 @@ final class SimpleLoggerFactory implements LoggerFactory { ...@@ -86,6 +90,10 @@ final class SimpleLoggerFactory implements LoggerFactory {
} }
private function set_handler( Logger $logger, HandlerInterface $handler ): void { private function set_handler( Logger $logger, HandlerInterface $handler ): void {
if ( is_string( $this->options['action_level'] ) ) {
$handler = new FingersCrossedHandler( $handler, $this->options['action_level'] );
}
// Purposefully replace all existing handlers. // Purposefully replace all existing handlers.
$logger->setHandlers( [ $handler ] ); $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