diff --git a/src/WpdbLogger.php b/src/WpdbLogger.php index b0fedbdb0e52a302063bc563644e9ee75374fd0d..47119c0d76e99f3d4defc5f144ef208e9fb9156c 100644 --- a/src/WpdbLogger.php +++ b/src/WpdbLogger.php @@ -11,20 +11,25 @@ class WpdbLogger implements \Psr\Log\LoggerInterface { private const MAX_LOG_SIZE = 30; /** @var string[] */ - private $log; + private $log = []; /** @var string */ private $log_name; public function __construct( string $log_name ) { $this->log_name = $log_name; - $this->log = json_decode( get_option( $this->log_name, '[]' ) ); - if ( ! \is_array( $this->log ) ) { - $this->log = []; - } } public function log( $level, $message, array $context = [] ) { + if ( empty( $this->log ) ) { + $this->log = json_decode( + get_option( + $this->log_name, + '[]' + ) + ); + } + $this->log[] = date( 'Y-m-d G:i:s' ) . sprintf( ': %s', $message ); if ( \count( $this->log ) > self::MAX_LOG_SIZE ) { array_shift( $this->log );