Skip to content

Commit 3823a78

Browse files
committed
Add buffer handler to reduce database write operations for performance.
Introduces Monolog's BufferHandler for database logging and adds the 'wpgraphql_logging_default_buffer_limit' filter to customize buffer size. Updates tests to flush buffer before asserting log counts and documents the new filter. Also adds an index for 'level_name' in the database entity.
1 parent da046e8 commit 3823a78

File tree

5 files changed

+56
-1
lines changed

5 files changed

+56
-1
lines changed

plugins/wpgraphql-logging/docs/reference/logging.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ add_filter( 'wpgraphql_logging_default_processors', function( array $processors
3939
});
4040
```
4141

42+
#### Filter: `wpgraphql_logging_default_buffer_limit`
43+
Filters the default buffer limit for the BufferHandler.
44+
45+
Parameters:
46+
- `$buffer_limit` (int) Current buffer limit (default: 50)
47+
48+
Returns: int
49+
50+
Example:
51+
```php
52+
53+
add_filter( 'wpgraphql_logging_default_buffer_limit', function( int $buffer_limit ) {
54+
// Increase buffer limit for high-traffic sites
55+
return 100;
56+
});
57+
58+
59+
```
60+
61+
62+
4263
#### Filter: `wpgraphql_logging_default_handlers`
4364
Filters the default handler list.
4465

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ public static function get_schema(): string {
366366
datetime DATETIME NOT NULL,
367367
PRIMARY KEY (id),
368368
INDEX channel_index (channel),
369+
INDEX level_name_index (level_name)
369370
INDEX level_index (level),
370371
INDEX datetime_index (datetime)
371372
) {$charset_collate};

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace WPGraphQL\Logging\Logger;
66

7+
use Monolog\Handler\BufferHandler;
78
use Monolog\Handler\HandlerInterface;
89
use Monolog\Logger;
910
use Monolog\Processor\MemoryPeakUsageProcessor;
@@ -212,6 +213,15 @@ public function log( $level, string $message, array $context = [] ): void {
212213
$this->monolog->log( $level, $message, array_merge( $this->default_context, $context ) );
213214
}
214215

216+
/**
217+
* Gets the Monolog logger instance.
218+
*
219+
* @return \Monolog\Logger The Monolog logger instance.
220+
*/
221+
public function get_monolog(): Logger {
222+
return $this->monolog;
223+
}
224+
215225
/**
216226
* Returns an array of default processors.
217227
*
@@ -241,8 +251,11 @@ public static function get_default_processors(): array {
241251
* @return array<\Monolog\Handler\AbstractProcessingHandler>
242252
*/
243253
public static function get_default_handlers(): array {
254+
255+
$buffer_limit = apply_filters( 'wpgraphql_logging_default_buffer_limit', 50 );
256+
$database_handler = new BufferHandler( new WordPressDatabaseHandler(), $buffer_limit );
244257
$default_handlers = [
245-
new WordPressDatabaseHandler(),
258+
$database_handler,
246259
];
247260

248261
// Filter for users to add their own handlers.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace WPGraphQL\Logging\Tests\Events;
66

77
use WPGraphQL\Logging\Plugin;
8+
use Monolog\Handler\BufferHandler;
89
use lucatume\WPBrowser\TestCase\WPTestCase;
910
use WPGraphQL\Logging\Events\QueryActionLogger;
1011
use WPGraphQL\Logging\Events\Events;
@@ -53,6 +54,15 @@ public function get_log_count(): int {
5354
}
5455

5556
public function assert_log_count(int $expected_count): void {
57+
58+
// Flush the buffer handler to ensure the log count is accurate.
59+
$handlers = $this->logger->get_monolog()->getHandlers();
60+
foreach ($handlers as $handler) {
61+
if ($handler instanceof BufferHandler) {
62+
$handler->flush();
63+
}
64+
}
65+
5666
$actual_count = $this->get_log_count();
5767
$this->assertEquals($expected_count, $actual_count, "Expected log count to be {$expected_count}, but got {$actual_count}.");
5868
}

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

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

77

88
use WPGraphQL\Logging\Plugin;
9+
use Monolog\Handler\BufferHandler;
910
use lucatume\WPBrowser\TestCase\WPTestCase;
1011
use WPGraphQL\Logging\Events\QueryFilterLogger;
1112
use WPGraphQL\Logging\Events\Events;
@@ -55,6 +56,15 @@ public function get_log_count(): int {
5556
}
5657

5758
public function assert_log_count(int $expected_count): void {
59+
60+
// Flush the buffer handler to ensure the log count is accurate.
61+
$handlers = $this->logger->get_monolog()->getHandlers();
62+
foreach ($handlers as $handler) {
63+
if ($handler instanceof BufferHandler) {
64+
$handler->flush();
65+
}
66+
}
67+
5868
$actual_count = $this->get_log_count();
5969
$this->assertEquals($expected_count, $actual_count, "Expected log count to be {$expected_count}, but got {$actual_count}.");
6070
}

0 commit comments

Comments
 (0)