Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
6 changes: 6 additions & 0 deletions class-hcqg-mutex-guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions hypercart-query-guard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/DbDropinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );
}
Expand Down
16 changes: 16 additions & 0 deletions tests/LoggingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down