diff --git a/README.md b/README.md index fcb65a6..cfc5a17 100644 --- a/README.md +++ b/README.md @@ -208,7 +208,7 @@ try { ## Logging -If `Hypercart_Logger` (from the Hypercart Performance Monitor plugin) is present, the plugin keeps the structured payload for array-capable logger methods and JSON-serializes it only for string-only logger signatures. Otherwise the plugin falls back to `error_log()` with single-line JSON for `grep`-ability: +If `Hypercart_Logger` (from the Hypercart Helper plugin) is present, the plugin keeps the structured payload for array-capable logger methods and JSON-serializes it only for string-only logger signatures. Otherwise the plugin falls back to `error_log()` with single-line JSON for `grep`-ability: ``` [hypercart_query_guard][error] {"event":"query_killed","context":"admin_ajax","limit_ms":20000,"last_query":"SELECT * FROM wp_nf_transactions WHERE meta_key = '_nofraud_transaction_status_workaround'","uri":"/wp-admin/admin-ajax.php","user_id":0,"time":1745875234} diff --git a/class-hcqg-mutex-guard.php b/class-hcqg-mutex-guard.php index b5849a6..17bd223 100644 --- a/class-hcqg-mutex-guard.php +++ b/class-hcqg-mutex-guard.php @@ -414,6 +414,12 @@ private static function log( $level, array $payload ) { Hypercart_Logger::info( 'query_guard', self::get_logger_payload_for_method( 'Hypercart_Logger', 'info', $payload, $encoded ) ); return; } + // The Hypercart Helper logger exposes warning(); older/string-only + // loggers may expose warn(). Prefer warning(), fall back to warn(). + if ( method_exists( 'Hypercart_Logger', 'warning' ) ) { + Hypercart_Logger::warning( 'query_guard', self::get_logger_payload_for_method( 'Hypercart_Logger', 'warning', $payload, $encoded ) ); + return; + } if ( method_exists( 'Hypercart_Logger', 'warn' ) ) { Hypercart_Logger::warn( 'query_guard', self::get_logger_payload_for_method( 'Hypercart_Logger', 'warn', $payload, $encoded ) ); return; diff --git a/hypercart-query-guard.php b/hypercart-query-guard.php index f4feadd..4855984 100644 --- a/hypercart-query-guard.php +++ b/hypercart-query-guard.php @@ -1107,8 +1107,8 @@ public static function log_slow_queries() { } /** - * Centralized log emitter. Prefers Hypercart_Logger if present (your - * existing file-based logger from the Performance Monitor plugin), + * Centralized log emitter. Prefers Hypercart_Logger if present (the + * file-based logger provided by the Hypercart Helper plugin), * falls back to error_log so this MU-plugin works standalone. * * @param string $level 'info' | 'warn' | 'error' @@ -1126,6 +1126,12 @@ private static function log( $level, array $payload ) { Hypercart_Logger::info( 'query_guard', self::get_logger_payload_for_method( 'Hypercart_Logger', 'info', $payload, $message ) ); return; } + // The Hypercart Helper logger exposes warning(); older/string-only + // loggers may expose warn(). Prefer warning(), fall back to warn(). + if ( method_exists( 'Hypercart_Logger', 'warning' ) ) { + Hypercart_Logger::warning( 'query_guard', self::get_logger_payload_for_method( 'Hypercart_Logger', 'warning', $payload, $message ) ); + return; + } if ( method_exists( 'Hypercart_Logger', 'warn' ) ) { Hypercart_Logger::warn( 'query_guard', self::get_logger_payload_for_method( 'Hypercart_Logger', 'warn', $payload, $message ) ); return; diff --git a/tests/DbDropinTest.php b/tests/DbDropinTest.php index 806612e..9612774 100644 --- a/tests/DbDropinTest.php +++ b/tests/DbDropinTest.php @@ -63,7 +63,7 @@ public function test_log_slow_queries_reads_hcqg_slow_queries_when_dropin_active Hypercart_Query_Guard::log_slow_queries(); $this->assertCount( 1, Hypercart_Logger::$calls ); - $this->assertSame( 'warn', Hypercart_Logger::$calls[0]['level'] ); + $this->assertSame( 'warning', Hypercart_Logger::$calls[0]['level'] ); $this->assertSame( 'slow_query', Hypercart_Logger::$calls[0]['payload']['event'] ); $this->assertSame( 6500, Hypercart_Logger::$calls[0]['payload']['duration_ms'] ); } diff --git a/tests/LoggingTest.php b/tests/LoggingTest.php index 778b6d8..a32f5d5 100644 --- a/tests/LoggingTest.php +++ b/tests/LoggingTest.php @@ -62,6 +62,22 @@ public function test_log_serializes_payload_for_hypercart_logger(): void { $this->assertSame( $payload, Hypercart_Logger::$calls[0]['payload'] ); } + public function test_warn_level_routes_to_hypercart_logger_warning(): void { + // Regression: slow_query events are emitted at 'warn' level. The Helper + // plugin's logger exposes warning(), not warn(); the bridge must map to it. + $payload = array( + 'event' => 'slow_query', + 'duration_ms' => 14777, + ); + + $this->call( 'log', 'warn', $payload ); + + $this->assertCount( 1, Hypercart_Logger::$calls ); + $this->assertSame( 'query_guard', Hypercart_Logger::$calls[0]['channel'] ); + $this->assertSame( 'warning', Hypercart_Logger::$calls[0]['level'] ); + $this->assertSame( $payload, Hypercart_Logger::$calls[0]['payload'] ); + } + public function test_string_only_logger_methods_receive_json_payload(): void { $payload = array( 'event' => 'as_throttle_observed', diff --git a/tests/bootstrap.php b/tests/bootstrap.php index eab0810..85d3ffc 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -314,7 +314,7 @@ class Hypercart_Logger { public static function reset(): void { self::$calls = array(); } public static function error( $channel, $payload ) { self::$calls[] = array( 'level' => 'error', 'channel' => $channel, 'payload' => $payload ); } public static function info( $channel, $payload ) { self::$calls[] = array( 'level' => 'info', 'channel' => $channel, 'payload' => $payload ); } - public static function warn( $channel, $payload ) { self::$calls[] = array( 'level' => 'warn', 'channel' => $channel, 'payload' => $payload ); } + public static function warning( $channel, $payload ) { self::$calls[] = array( 'level' => 'warning', 'channel' => $channel, 'payload' => $payload ); } } $GLOBALS['wpdb'] = new WP_Stub_DB();