Skip to content

Commit 883cae7

Browse files
committed
Added request headers to the log. Displayed this in the admin view.
1 parent f242763 commit 883cae7

File tree

5 files changed

+91
-9
lines changed

5 files changed

+91
-9
lines changed

plugins/wpgraphql-logging/src/Admin/View/List/List_Table.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ public function get_columns(): array {
134134
'level_name' => __( 'Level Name', 'wpgraphql-logging' ),
135135
'event' => __( 'Event', 'wpgraphql-logging' ),
136136
'process_id' => __( 'Process ID', 'wpgraphql-logging' ),
137-
'headers' => __( 'Headers', 'wpgraphql-logging' ),
138-
'time' => __( 'Time', 'wpgraphql-logging' ),
137+
'request_headers' => __( 'Headers', 'wpgraphql-logging' ),
139138
'memory_usage' => __( 'Memory Usage', 'wpgraphql-logging' ),
140139
]
141140
);
@@ -175,6 +174,8 @@ public function column_default( $item, $column_name ): mixed {
175174
return $this->get_memory_usage( $item );
176175
case 'wpgraphql_query':
177176
return $this->get_query( $item );
177+
case 'request_headers':
178+
return $this->get_request_headers( $item );
178179
default:
179180
// Users can add their own custom columns and render functionality.
180181
return apply_filters( 'wpgraphql_logging_logs_table_column_value', '', $item, $column_name );
@@ -243,7 +244,18 @@ public function column_query( DatabaseEntity $item ): ?string {
243244
*/
244245
public function get_query(DatabaseEntity $item): string {
245246
$extra = $item->get_extra();
246-
return ! empty( $extra['wpgraphql_query'] ) ? esc_html( $extra['wpgraphql_query'] ) : '';
247+
$query = ! empty( $extra['wpgraphql_query'] ) ? esc_html( $extra['wpgraphql_query'] ) : '';
248+
return '<pre style="overflow-x: auto;
249+
background: #f6f7f7;
250+
padding: 15px;
251+
border: 1px solid #ddd;
252+
border-radius: 4px;
253+
white-space: pre-wrap;
254+
word-break: break-word;
255+
max-width: 100%;
256+
max-height: 300px;
257+
overflow-y: auto;
258+
box-sizing: border-box;">' . esc_html( $query ) . '</pre>';
247259
}
248260

249261
/**
@@ -278,4 +290,23 @@ public function get_memory_usage(DatabaseEntity $item): string {
278290
$extra = $item->get_extra();
279291
return ! empty( $extra['memory_peak_usage'] ) ? esc_html( $extra['memory_peak_usage'] ) : '';
280292
}
293+
294+
/**
295+
* Gets the request headers from extra.
296+
*
297+
* @return string The event
298+
*/
299+
public function get_request_headers(DatabaseEntity $item): string {
300+
$extra = $item->get_extra();
301+
$request_headers = $extra['request_headers'] ?? [];
302+
if ( empty( $request_headers ) || ! is_array( $request_headers ) ) {
303+
return '';
304+
}
305+
306+
$formatted_request_headers = wp_json_encode( $request_headers, JSON_PRETTY_PRINT );
307+
if ( false === $formatted_request_headers ) {
308+
return '';
309+
}
310+
return '<pre style="overflow-x: auto; background: #f4f4f4; padding: 15px; border: 1px solid #ddd; border-radius: 4px;">' . esc_html( $formatted_request_headers ) . '</pre>';
311+
}
281312
}

plugins/wpgraphql-logging/src/Admin/View/List/Templates/wpgraphql-logger-list.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
<form method="post">
2525
<?php
2626
$list_table->prepare_items();
27-
// $list_table->display_bulk_actions_top();
2827
$list_table->display();
29-
// $list_table->display_bulk_actions_bottom();
3028
?>
3129
</form>
3230
</div>

plugins/wpgraphql-logging/src/Logger/Database/DatabaseEntity.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,12 @@ public function get_datetime(): string {
249249
public static function find_logs(int $limit, int $offset, string $orderby = 'id', string $order = 'DESC'): array {
250250
global $wpdb;
251251
$table_name = self::get_table_name();
252+
$order = esc_sql( strtoupper( $order ) );
252253
$orderby = esc_sql( $orderby );
253-
$order = esc_sql( $order );
254254

255+
/** @psalm-suppress PossiblyInvalidCast */
255256
$query = $wpdb->prepare(
256-
"SELECT * FROM {$table_name} ORDER BY %s %s LIMIT %d, %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
257-
$orderby,
258-
$order,
257+
"SELECT * FROM {$table_name} ORDER BY {$orderby} {$order} LIMIT %d, %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
259258
$offset,
260259
$limit
261260
);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Monolog\Processor\ProcessorInterface;
1313
use Monolog\Processor\WebProcessor;
1414
use WPGraphQL\Logging\Logger\Handlers\WordPressDatabaseHandler;
15+
use WPGraphQL\Logging\Logger\Processors\RequestHeadersProcessor;
1516
use WPGraphQL\Logging\Logger\Processors\WPGraphQLQueryProcessor;
1617

1718
/**
@@ -225,6 +226,7 @@ public static function get_default_processors(): array {
225226
new WebProcessor(), // Logs web request data. e.g. IP address, request method, URI, etc.
226227
new ProcessIdProcessor(), // Logs the process ID.
227228
new WPGraphQLQueryProcessor(), // Custom processor to capture GraphQL request data.
229+
new RequestHeadersProcessor(), // Custom processor to capture request headers.
228230
];
229231

230232
// Filter for users to add their own processors.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WPGraphQL\Logging\Logger\Processors;
6+
7+
use Monolog\LogRecord;
8+
use Monolog\Processor\ProcessorInterface;
9+
10+
/**
11+
* This class is responsible for capturing and processing request headers
12+
* for logging purposes.
13+
*
14+
* @package WPGraphQL\Logging
15+
*
16+
* @since 0.0.1
17+
*/
18+
class RequestHeadersProcessor implements ProcessorInterface {
19+
/**
20+
* Retrieves the request headers from the $_SERVER superglobal.
21+
*
22+
* @return array<string, mixed> The request headers.
23+
*/
24+
private function get_headers(): array {
25+
$headers = [];
26+
foreach ( $_SERVER as $key => $value ) {
27+
if ( ! is_string( $value ) || empty( $value ) ) {
28+
continue;
29+
}
30+
$header_key = substr( $key, 5 );
31+
$header_key = str_replace( '_', '-', $header_key );
32+
$header_key = ucwords( strtolower( sanitize_text_field( (string) $header_key ) ), '-' );
33+
$headers[ $header_key ] = sanitize_text_field( $value );
34+
}
35+
36+
return $headers;
37+
}
38+
39+
/**
40+
* This method is called for each log record. It adds the captured
41+
* request headers to the record's 'extra' array.
42+
*
43+
* @param \Monolog\LogRecord $record The log record to process.
44+
*
45+
* @return \Monolog\LogRecord The processed log record.
46+
*/
47+
public function __invoke( LogRecord $record ): LogRecord {
48+
$record->extra['request_headers'] = $this->get_headers();
49+
50+
return $record;
51+
}
52+
}

0 commit comments

Comments
 (0)