Skip to content

Commit 0a85d94

Browse files
authored
Remove Psr\Log, add "Logs" Query Monitor subpanel (#493)
* Remove Psr\Log * Warehouse Psr\Log classes * Remove should_log_to_query_monitor filter * warehouse fixup * Move is_log_level_higher to LogLevel * Remove LoggerManager * LogLevel fixup * Update action name * Update QueryMonitor panels to use Logger * Fix sprintf * Fix missing block_info * Fix test that asserts log context
1 parent 90de462 commit 0a85d94

24 files changed

+525
-566
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"galbar/jsonpath": "^3.0",
3333
"guzzlehttp/guzzle": "^7.8",
3434
"kevinrob/guzzle-cache-middleware": "^6.0",
35-
"psr/log": "^3.0",
3635
"erusev/parsedown": "^1.7",
3736
"symfony/var-exporter": "^6"
3837
},

composer.lock

Lines changed: 4 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/ai.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -669,29 +669,21 @@ if ( defined( 'REMOTE_DATA_BLOCKS__LOADED' ) ) {
669669
}
670670
```
671671

672-
### wpcomvip_log
672+
### remote_data_blocks_log
673673

674-
If you want to send debugging information to another source besides [Query Monitor](../troubleshooting.md#query-monitor), use the `wpcomvip_log` action.
674+
If you want to send debugging information to another source besides [Query Monitor](../troubleshooting.md#query-monitor), use the `remote_data_blocks_log` action.
675675

676676
```php
677677
function custom_log( string $namespace, string $level, string $message, array $context ): void {
678678
// Send the log to a custom destination.
679679
}
680-
add_action( 'wpcomvip_log', 'custom_log', 10, 4 );
680+
add_action( 'remote_data_blocks_log', 'custom_log', 10, 4 );
681681
```
682682

683683
## Filters
684684

685685
Filters give you the ability to change data during the execution of the plugin. Callback functions for Filters will accept a variable, modify it, and return it. They are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output.
686686

687-
### wpcomvip_log_to_query_monitor
688-
689-
Filter whether to log a message to Query Monitor (default: `true`).
690-
691-
```php
692-
add_filter( 'wpcomvip_log_to_query_monitor', '__return_false' );
693-
```
694-
695687
### remote_data_blocks_register_example_block
696688

697689
Filter whether to register the included example API block ("Conference Event") (default: `true`).
@@ -3064,7 +3056,7 @@ function register_aic_block(): void {
30643056
'endpoint' => function ( array $input_variables ) use ( $aic_data_source ): string {
30653057
$endpoint = $aic_data_source->get_endpoint();
30663058
return add_query_arg( [
3067-
'limit' => $input_variables['limit'],
3059+
'limit' => $input_variables['limit'],
30683060
'fields' => 'id,title,image_id,artist_title',
30693061
'page' => $input_variables['page'],
30703062
], $endpoint );
@@ -3180,7 +3172,7 @@ function register_aic_block(): void {
31803172
register_remote_data_block( [
31813173
'title' => 'Art Institute of Chicago Loop',
31823174
'icon' => 'art',
3183-
'instructions' => 'This block displays a set amount of artworks based on the provided limit.',
3175+
'instructions' => 'This block displays a set amount of artworks based on the provided limit.',
31843176
'render_query' => [
31853177
'query' => $collection_query,
31863178
'loop' => true,

docs/extending/hooks.md

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,21 @@ if ( defined( 'REMOTE_DATA_BLOCKS__LOADED' ) ) {
2929
}
3030
```
3131

32-
### wpcomvip_log
32+
### remote_data_blocks_log
3333

34-
If you want to send debugging information to another source besides [Query Monitor](../troubleshooting.md#query-monitor), use the `wpcomvip_log` action.
34+
If you want to send debugging information to another source besides [Query Monitor](../troubleshooting.md#query-monitor), use the `remote_data_blocks_log` action.
3535

3636
```php
3737
function custom_log( string $namespace, string $level, string $message, array $context ): void {
3838
// Send the log to a custom destination.
3939
}
40-
add_action( 'wpcomvip_log', 'custom_log', 10, 4 );
40+
add_action( 'remote_data_blocks_log', 'custom_log', 10, 4 );
4141
```
4242

4343
## Filters
4444

4545
Filters give you the ability to change data during the execution of the plugin. Callback functions for Filters will accept a variable, modify it, and return it. They are meant to work in an isolated manner, and should never have side effects such as affecting global variables and output.
4646

47-
### wpcomvip_log_to_query_monitor
48-
49-
Filter whether to log a message to Query Monitor (default: `true`).
50-
51-
```php
52-
add_filter( 'wpcomvip_log_to_query_monitor', '__return_false' );
53-
```
54-
5547
### remote_data_blocks_register_example_block
5648

5749
Filter whether to register the included example API block ("Conference Event") (default: `true`).

inc/Editor/AdminNotices/AdminNotices.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace RemoteDataBlocks\Editor\AdminNotices;
44

5-
use Psr\Log\LogLevel;
6-
use RemoteDataBlocks\Logging\LoggerManager;
5+
use RemoteDataBlocks\Logging\Logger;
6+
use RemoteDataBlocks\Logging\LogLevel;
7+
78
use function add_action;
89

910
defined( 'ABSPATH' ) || exit();
@@ -30,22 +31,19 @@ class AdminNotices {
3031
* Initialize the logger and provide WordPress hooks.
3132
*/
3233
public static function init(): void {
33-
add_action( 'wpcomvip_log', [ __CLASS__, 'store_log' ], 10, 3 );
34+
add_action( Logger::ACTION_NAME, [ __CLASS__, 'store_log' ], 10, 3 );
3435
add_action( 'admin_notices', [ __CLASS__, 'admin_notices' ], 10, 0 );
3536
}
3637

3738
public static function store_log( string $namespace, string $log_level, string $message ): void {
38-
if ( LoggerManager::$log_namespace !== $namespace ) {
39-
return;
40-
}
41-
42-
if ( ! LoggerManager::instance()->is_log_level_higher( $log_level, self::$log_level_threshold ) ) {
39+
if ( ! LogLevel::meets_threshold( $log_level, self::$log_level_threshold ) ) {
4340
return;
4441
}
4542

4643
self::$log_store[] = [
4744
'level' => $log_level,
4845
'message' => $message,
46+
'namespace' => $namespace,
4947
];
5048
}
5149

inc/Editor/BlockManagement/ConfigRegistry.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
defined( 'ABSPATH' ) || exit();
66

7-
use RemoteDataBlocks\Logging\LoggerManager;
8-
use Psr\Log\LoggerInterface;
7+
use RemoteDataBlocks\Logging\Logger;
98
use RemoteDataBlocks\Config\Query\HttpQuery;
109
use RemoteDataBlocks\Config\Query\QueryInterface;
1110
use RemoteDataBlocks\Editor\BlockPatterns\BlockPatterns;
11+
use RemoteDataBlocks\Logging\LoggerInterface;
1212
use RemoteDataBlocks\Validation\ConfigSchemas;
1313
use RemoteDataBlocks\Validation\Validator;
1414
use WP_Error;
@@ -27,7 +27,7 @@ class ConfigRegistry {
2727
public const SEARCH_QUERY_KEY = 'search';
2828

2929
public static function init( ?LoggerInterface $logger = null ): void {
30-
self::$logger = $logger ?? LoggerManager::instance();
30+
self::$logger = $logger ?? new Logger();
3131
ConfigStore::init( self::$logger );
3232
}
3333

inc/Editor/BlockManagement/ConfigStore.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
defined( 'ABSPATH' ) || exit();
66

77
use RemoteDataBlocks\Config\Query\QueryInterface;
8-
use RemoteDataBlocks\Logging\LoggerManager;
9-
use Psr\Log\LoggerInterface;
8+
use RemoteDataBlocks\Logging\Logger;
9+
use RemoteDataBlocks\Logging\LoggerInterface;
1010

1111
use function sanitize_title_with_dashes;
1212

@@ -20,7 +20,7 @@ class ConfigStore {
2020

2121
public static function init( ?LoggerInterface $logger = null ): void {
2222
self::$blocks = [];
23-
self::$logger = $logger ?? LoggerManager::instance();
23+
self::$logger = $logger ?? new Logger();
2424
}
2525

2626
/**

inc/Editor/DataBinding/BlockBindings.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
defined( 'ABSPATH' ) || exit();
66

7-
use Psr\Log\LogLevel;
87
use RemoteDataBlocks\Config\BlockAttribute\RemoteDataBlockAttribute;
98
use RemoteDataBlocks\Editor\BlockManagement\ConfigRegistry;
109
use RemoteDataBlocks\Editor\BlockManagement\ConfigStore;
10+
use RemoteDataBlocks\Logging\Logger;
11+
use RemoteDataBlocks\Logging\LoggerInterface;
1112
use RemoteDataBlocks\Sanitization\Sanitizer;
1213
use WP_Block;
1314
use WP_Error;
@@ -21,12 +22,15 @@
2122
class BlockBindings {
2223
public static string $context_name = 'remote-data-blocks/remoteData';
2324
public static string $binding_source = 'remote-data/binding';
24-
public static string $log_action_name = 'remote_data_blocks_log_block_binding_event';
2525

2626
protected static string $hydrated_results_key = 'hydrated_results';
2727
protected static string $prerendered_content_key = 'prerendered_content';
2828

29-
public static function init(): void {
29+
protected static ?LoggerInterface $logger = null;
30+
31+
public static function init( ?LoggerInterface $logger = null ): void {
32+
self::$logger = $logger ?? new Logger();
33+
3034
add_action( 'init', [ __CLASS__, 'register_block_bindings' ], 50, 0 );
3135
add_filter( 'register_block_type_args', [ __CLASS__, 'inject_context_for_synced_patterns' ], 10, 2 );
3236
}
@@ -282,8 +286,10 @@ public static function get_value( array $source_args, WP_Block|array $block, str
282286

283287
$log_context = [
284288
'block_name' => $bound_block_name,
289+
'block_info' => [
290+
'source_args' => $source_args,
291+
],
285292
'remote_data_block_name' => $block_name,
286-
'source_args' => $source_args,
287293
];
288294

289295
if ( null === $field_name ) {
@@ -387,6 +393,7 @@ public static function render_remote_data_template_block( array $attributes, str
387393

388394
$log_context = [
389395
'block_name' => $block->name,
396+
'block_info' => [],
390397
'remote_data_block_name' => $block_context['blockName'] ?? 'unknown',
391398
];
392399

@@ -488,20 +495,33 @@ private static function add_source_args_to_inner_blocks( array $inner_blocks, ar
488495
}
489496

490497
protected static function log_error( array $log_context, WP_Error $error ): void {
498+
if ( null === self::$logger ) {
499+
return;
500+
}
501+
491502
// Remove key "hydrated_results" if it exists.
492-
if ( isset( $log_context['source_args'][ self::$hydrated_results_key ] ) ) {
493-
unset( $log_context['source_args'][ self::$hydrated_results_key ] );
503+
if ( isset( $log_context['block_info']['source_args'][ self::$hydrated_results_key ] ) ) {
504+
unset( $log_context['block_info']['source_args'][ self::$hydrated_results_key ] );
494505
}
495506

496-
do_action( self::$log_action_name, LogLevel::ERROR, $error->get_error_message(), $log_context, $error );
507+
$log_context['error'] = $error;
508+
$log_context['type'] = 'block-binding';
509+
510+
self::$logger->error( $error->get_error_message(), $log_context );
497511
}
498512

499513
protected static function log_success( array $log_context ): void {
514+
if ( null === self::$logger ) {
515+
return;
516+
}
517+
500518
// Remove key "hydrated_results" if it exists.
501-
if ( isset( $log_context['source_args'][ self::$hydrated_results_key ] ) ) {
502-
unset( $log_context['source_args'][ self::$hydrated_results_key ] );
519+
if ( isset( $log_context['block_info']['source_args'][ self::$hydrated_results_key ] ) ) {
520+
unset( $log_context['block_info']['source_args'][ self::$hydrated_results_key ] );
503521
}
504522

505-
do_action( self::$log_action_name, LogLevel::INFO, 'Successfully resolved block binding', $log_context );
523+
$log_context['type'] = 'block-binding';
524+
525+
self::$logger->info( 'Successfully resolved block binding', $log_context );
506526
}
507527
}

0 commit comments

Comments
 (0)