66
77use GraphQL \Executor \ExecutionResult ;
88use Monolog \Level ;
9- use WPGraphQL \Logging \Admin \Settings \Fields \Tab \BasicConfigurationTab ;
109use WPGraphQL \Logging \Logger \LoggerService ;
1110use WPGraphQL \Logging \Logger \LoggingHelper ;
1211use 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}
0 commit comments