Skip to content

Commit 2a77388

Browse files
committed
Added fixes to EventFilterLogger and added tests.
1 parent 9559fbe commit 2a77388

File tree

5 files changed

+305
-65
lines changed

5 files changed

+305
-65
lines changed

plugins/wpgraphql-logging/src/Events/QueryActionLogger.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use GraphQL\Executor\ExecutionResult;
88
use Monolog\Level;
9-
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
109
use WPGraphQL\Logging\Logger\LoggerService;
1110
use WPGraphQL\Logging\Logger\LoggingHelper;
1211
use WPGraphQL\Request;
@@ -182,25 +181,6 @@ public function log_before_response_returned(
182181
}
183182
}
184183

185-
/**
186-
* Determine if the event should be logged based on the configuration and selected events.
187-
*
188-
* @param string $event The event name.
189-
* @param string|null $query The GraphQL query (optional).
190-
*
191-
* @return bool True if the event should be logged, false otherwise.
192-
*/
193-
public function should_log_event(string $event, ?string $query = null): bool {
194-
if ( ! $this->is_logging_enabled( $this->config, $query ) ) {
195-
return false;
196-
}
197-
$selected_events = $this->config[ BasicConfigurationTab::EVENT_LOG_SELECTION ] ?? [];
198-
if ( ! is_array( $selected_events ) || empty( $selected_events ) ) {
199-
return false;
200-
}
201-
return in_array( $event, $selected_events, true );
202-
}
203-
204184
/**
205185
* Get the context for the response.
206186
*
@@ -224,14 +204,4 @@ protected function get_response_errors( array|ExecutionResult $response ): ?arra
224204

225205
return $errors;
226206
}
227-
228-
/**
229-
* Handles and logs application errors.
230-
*
231-
* @param string $event
232-
* @param \Throwable $exception
233-
*/
234-
protected function process_application_error( string $event, \Throwable $exception ): void {
235-
error_log( 'Error for WPGraphQL Logging - ' . $event . ': ' . $exception->getMessage() . ' in ' . $exception->getFile() . ' on line ' . $exception->getLine() ); //phpcs:ignore
236-
}
237207
}

plugins/wpgraphql-logging/src/Events/QueryFilterLogger.php

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use GraphQL\Executor\ExecutionResult;
88
use Monolog\Level;
9-
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
109
use WPGraphQL\Logging\Logger\LoggerService;
1110
use WPGraphQL\Logging\Logger\LoggingHelper;
1211
use WPGraphQL\Logging\Logger\Rules\EnabledRule;
@@ -63,16 +62,8 @@ public function __construct( LoggerService $logger, array $config ) {
6362
*/
6463
public function log_graphql_request_data( array $query_data ): array {
6564
try {
66-
$selected_events = $this->config[ BasicConfigurationTab::EVENT_LOG_SELECTION ] ?? [];
67-
if ( ! is_array( $selected_events ) || empty( $selected_events ) ) {
68-
return $query_data;
69-
}
70-
if ( ! in_array( Events::REQUEST_DATA, $selected_events, true ) ) {
71-
return $query_data;
72-
}
73-
7465
$query_string = $query_data['query'] ?? null;
75-
if ( ! $this->is_logging_enabled( $this->config, $query_string ) ) {
66+
if ( ! $this->should_log_event( Events::REQUEST_DATA, $query_string ) ) {
7667
return $query_data;
7768
}
7869

@@ -82,8 +73,11 @@ public function log_graphql_request_data( array $query_data ): array {
8273
'operation_name' => $query_data['operationName'] ?? null,
8374
];
8475

85-
$payload = EventManager::transform( Events::REQUEST_DATA, [ 'context' => $context ] );
86-
$this->logger->log( Level::Info, 'WPGraphQL Request Data', $payload['context'] );
76+
$payload = EventManager::transform( Events::REQUEST_DATA, [
77+
'context' => $context,
78+
'level' => Level::Info,
79+
] );
80+
$this->logger->log( $payload['level'], 'WPGraphQL Request Data', $payload['context'] );
8781
EventManager::publish( Events::REQUEST_DATA, [ 'context' => $payload['context'] ] );
8882
} catch ( \Throwable $e ) {
8983
$this->process_application_error( Events::REQUEST_DATA, $e );
@@ -117,15 +111,7 @@ public function log_graphql_request_results(
117111
?string $query_id
118112
): array|ExecutionResult {
119113
try {
120-
if ( ! $this->is_logging_enabled( $this->config, $query ) ) {
121-
return $response;
122-
}
123-
124-
$selected_events = $this->config[ BasicConfigurationTab::EVENT_LOG_SELECTION ] ?? [];
125-
if ( ! is_array( $selected_events ) || empty( $selected_events ) ) {
126-
return $response;
127-
}
128-
if ( ! in_array( Events::REQUEST_RESULTS, $selected_events, true ) ) {
114+
if ( ! $this->should_log_event( Events::REQUEST_RESULTS, $query ) ) {
129115
return $response;
130116
}
131117

@@ -134,9 +120,9 @@ public function log_graphql_request_results(
134120
$encoded_request = wp_json_encode( $request );
135121
$context = [
136122
'response' => $response,
137-
'operation_name' => $params->operation,
138-
'query' => $params->query,
139-
'variables' => $params->variables,
123+
'operation_name' => $params->operation ?? null,
124+
'query' => $params->query ?? null,
125+
'variables' => $params->variables ?? null,
140126
'request' => false !== $encoded_request ? json_decode( $encoded_request, true ) : null,
141127
'query_id' => $query_id,
142128
];
@@ -185,14 +171,4 @@ public function add_logging_headers( array $headers ): array {
185171

186172
return $headers;
187173
}
188-
189-
/**
190-
* Handles and logs application errors.
191-
*
192-
* @param string $event The name of the event where the error occurred.
193-
* @param \Throwable $exception The exception that was caught.
194-
*/
195-
protected function process_application_error( string $event, \Throwable $exception ): void {
196-
error_log( 'Error for WPGraphQL Logging - ' . $event . ': ' . $exception->getMessage() . ' in ' . $exception->getFile() . ' on line ' . $exception->getLine() ); //phpcs:ignore
197-
}
198174
}

plugins/wpgraphql-logging/src/Logger/LoggingHelper.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace WPGraphQL\Logging\Logger;
66

7+
use WPGraphQL\Logging\Admin\Settings\Fields\Tab\BasicConfigurationTab;
78
use WPGraphQL\Logging\Logger\Rules\AdminUserRule;
89
use WPGraphQL\Logging\Logger\Rules\EnabledRule;
910
use WPGraphQL\Logging\Logger\Rules\ExcludeQueryRule;
@@ -40,6 +41,25 @@ public function should_log_response(array $config): bool {
4041
return $rule->passes( $config );
4142
}
4243

44+
/**
45+
* Determine if the event should be logged based on the configuration and selected events.
46+
*
47+
* @param string $event The event name.
48+
* @param string|null $query The GraphQL query (optional).
49+
*
50+
* @return bool True if the event should be logged, false otherwise.
51+
*/
52+
public function should_log_event(string $event, ?string $query = null): bool {
53+
if ( ! $this->is_logging_enabled( $this->config, $query ) ) {
54+
return false;
55+
}
56+
$selected_events = $this->config[ BasicConfigurationTab::EVENT_LOG_SELECTION ] ?? [];
57+
if ( ! is_array( $selected_events ) || empty( $selected_events ) ) {
58+
return false;
59+
}
60+
return in_array( $event, $selected_events, true );
61+
}
62+
4363
/**
4464
* Get the rule manager, initializing it if necessary.
4565
*/
@@ -75,4 +95,14 @@ protected function is_logging_enabled( array $config, ?string $query_string = nu
7595
*/
7696
return apply_filters( 'wpgraphql_logging_is_enabled', $is_enabled, $config );
7797
}
98+
99+
/**
100+
* Handles and logs application errors.
101+
*
102+
* @param string $event
103+
* @param \Throwable $exception
104+
*/
105+
protected function process_application_error( string $event, \Throwable $exception ): void {
106+
error_log( 'Error for WPGraphQL Logging - ' . $event . ': ' . $exception->getMessage() . ' in ' . $exception->getFile() . ' on line ' . $exception->getLine() ); //phpcs:ignore
107+
}
78108
}

plugins/wpgraphql-logging/tests/wpunit/Events/QueryActionLoggerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use WPGraphQL\Request;
2222

2323
/**
24-
* Class QueryActionLoggerTest
24+
* Tests for the QueryActionLogger class.
2525
*
2626
* @package WPGraphQL\Logging\Tests\Events
2727
*
@@ -419,6 +419,7 @@ public function test_log_before_response_returned_log_data_with_errors_array():
419419

420420
$this->assertEquals(Level::Error->value, $log->get_level());
421421
$this->assertArrayHasKey('errors', $log->get_context());
422+
$this->assertEquals('WPGraphQL Response with Errors', $log->get_message());
422423
}
423424

424425

0 commit comments

Comments
 (0)