Skip to content

Remove Psr\Log, add "Logs" Query Monitor subpanel #493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"galbar/jsonpath": "^3.0",
"guzzlehttp/guzzle": "^7.8",
"kevinrob/guzzle-cache-middleware": "^6.0",
"psr/log": "^3.0",
"erusev/parsedown": "^1.7",
"symfony/var-exporter": "^6"
},
Expand Down
58 changes: 4 additions & 54 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 5 additions & 13 deletions docs/ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -669,29 +669,21 @@ if ( defined( 'REMOTE_DATA_BLOCKS__LOADED' ) ) {
}
```

### wpcomvip_log
### remote_data_blocks_log

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

```php
function custom_log( string $namespace, string $level, string $message, array $context ): void {
// Send the log to a custom destination.
}
add_action( 'wpcomvip_log', 'custom_log', 10, 4 );
add_action( 'remote_data_blocks_log', 'custom_log', 10, 4 );
```

## Filters

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.

### wpcomvip_log_to_query_monitor

Filter whether to log a message to Query Monitor (default: `true`).

```php
add_filter( 'wpcomvip_log_to_query_monitor', '__return_false' );
```

### remote_data_blocks_register_example_block

Filter whether to register the included example API block ("Conference Event") (default: `true`).
Expand Down Expand Up @@ -3064,7 +3056,7 @@ function register_aic_block(): void {
'endpoint' => function ( array $input_variables ) use ( $aic_data_source ): string {
$endpoint = $aic_data_source->get_endpoint();
return add_query_arg( [
'limit' => $input_variables['limit'],
'limit' => $input_variables['limit'],
'fields' => 'id,title,image_id,artist_title',
'page' => $input_variables['page'],
], $endpoint );
Expand Down Expand Up @@ -3180,7 +3172,7 @@ function register_aic_block(): void {
register_remote_data_block( [
'title' => 'Art Institute of Chicago Loop',
'icon' => 'art',
'instructions' => 'This block displays a set amount of artworks based on the provided limit.',
'instructions' => 'This block displays a set amount of artworks based on the provided limit.',
'render_query' => [
'query' => $collection_query,
'loop' => true,
Expand Down
14 changes: 3 additions & 11 deletions docs/extending/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,21 @@ if ( defined( 'REMOTE_DATA_BLOCKS__LOADED' ) ) {
}
```

### wpcomvip_log
### remote_data_blocks_log

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

```php
function custom_log( string $namespace, string $level, string $message, array $context ): void {
// Send the log to a custom destination.
}
add_action( 'wpcomvip_log', 'custom_log', 10, 4 );
add_action( 'remote_data_blocks_log', 'custom_log', 10, 4 );
```

## Filters

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.

### wpcomvip_log_to_query_monitor

Filter whether to log a message to Query Monitor (default: `true`).

```php
add_filter( 'wpcomvip_log_to_query_monitor', '__return_false' );
```

### remote_data_blocks_register_example_block

Filter whether to register the included example API block ("Conference Event") (default: `true`).
Expand Down
14 changes: 6 additions & 8 deletions inc/Editor/AdminNotices/AdminNotices.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace RemoteDataBlocks\Editor\AdminNotices;

use Psr\Log\LogLevel;
use RemoteDataBlocks\Logging\LoggerManager;
use RemoteDataBlocks\Logging\Logger;
use RemoteDataBlocks\Logging\LogLevel;

use function add_action;

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

public static function store_log( string $namespace, string $log_level, string $message ): void {
if ( LoggerManager::$log_namespace !== $namespace ) {
return;
}

if ( ! LoggerManager::instance()->is_log_level_higher( $log_level, self::$log_level_threshold ) ) {
if ( ! LogLevel::meets_threshold( $log_level, self::$log_level_threshold ) ) {
return;
}

self::$log_store[] = [
'level' => $log_level,
'message' => $message,
'namespace' => $namespace,
];
}

Expand Down
6 changes: 3 additions & 3 deletions inc/Editor/BlockManagement/ConfigRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

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

use RemoteDataBlocks\Logging\LoggerManager;
use Psr\Log\LoggerInterface;
use RemoteDataBlocks\Logging\Logger;
use RemoteDataBlocks\Config\Query\HttpQuery;
use RemoteDataBlocks\Config\Query\QueryInterface;
use RemoteDataBlocks\Editor\BlockPatterns\BlockPatterns;
use RemoteDataBlocks\Logging\LoggerInterface;
use RemoteDataBlocks\Validation\ConfigSchemas;
use RemoteDataBlocks\Validation\Validator;
use WP_Error;
Expand All @@ -27,7 +27,7 @@ class ConfigRegistry {
public const SEARCH_QUERY_KEY = 'search';

public static function init( ?LoggerInterface $logger = null ): void {
self::$logger = $logger ?? LoggerManager::instance();
self::$logger = $logger ?? new Logger();
ConfigStore::init( self::$logger );
}

Expand Down
6 changes: 3 additions & 3 deletions inc/Editor/BlockManagement/ConfigStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
defined( 'ABSPATH' ) || exit();

use RemoteDataBlocks\Config\Query\QueryInterface;
use RemoteDataBlocks\Logging\LoggerManager;
use Psr\Log\LoggerInterface;
use RemoteDataBlocks\Logging\Logger;
use RemoteDataBlocks\Logging\LoggerInterface;

use function sanitize_title_with_dashes;

Expand All @@ -20,7 +20,7 @@ class ConfigStore {

public static function init( ?LoggerInterface $logger = null ): void {
self::$blocks = [];
self::$logger = $logger ?? LoggerManager::instance();
self::$logger = $logger ?? new Logger();
}

/**
Expand Down
40 changes: 30 additions & 10 deletions inc/Editor/DataBinding/BlockBindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

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

use Psr\Log\LogLevel;
use RemoteDataBlocks\Config\BlockAttribute\RemoteDataBlockAttribute;
use RemoteDataBlocks\Editor\BlockManagement\ConfigRegistry;
use RemoteDataBlocks\Editor\BlockManagement\ConfigStore;
use RemoteDataBlocks\Logging\Logger;
use RemoteDataBlocks\Logging\LoggerInterface;
use RemoteDataBlocks\Sanitization\Sanitizer;
use WP_Block;
use WP_Error;
Expand All @@ -21,12 +22,15 @@
class BlockBindings {
public static string $context_name = 'remote-data-blocks/remoteData';
public static string $binding_source = 'remote-data/binding';
public static string $log_action_name = 'remote_data_blocks_log_block_binding_event';

protected static string $hydrated_results_key = 'hydrated_results';
protected static string $prerendered_content_key = 'prerendered_content';

public static function init(): void {
protected static ?LoggerInterface $logger = null;

public static function init( ?LoggerInterface $logger = null ): void {
self::$logger = $logger ?? new Logger();

add_action( 'init', [ __CLASS__, 'register_block_bindings' ], 50, 0 );
add_filter( 'register_block_type_args', [ __CLASS__, 'inject_context_for_synced_patterns' ], 10, 2 );
}
Expand Down Expand Up @@ -266,8 +270,10 @@ public static function get_value( array $source_args, WP_Block|array $block, str

$log_context = [
'block_name' => $bound_block_name,
'block_info' => [
'source_args' => $source_args,
],
'remote_data_block_name' => $block_name,
'source_args' => $source_args,
];

if ( null === $field_name ) {
Expand Down Expand Up @@ -371,6 +377,7 @@ public static function render_remote_data_template_block( array $attributes, str

$log_context = [
'block_name' => $block->name,
'block_info' => [],
'remote_data_block_name' => $block_context['blockName'] ?? 'unknown',
];

Expand Down Expand Up @@ -472,20 +479,33 @@ private static function add_source_args_to_inner_blocks( array $inner_blocks, ar
}

protected static function log_error( array $log_context, WP_Error $error ): void {
if ( null === self::$logger ) {
return;
}

// Remove key "hydrated_results" if it exists.
if ( isset( $log_context['source_args'][ self::$hydrated_results_key ] ) ) {
unset( $log_context['source_args'][ self::$hydrated_results_key ] );
if ( isset( $log_context['block_info']['source_args'][ self::$hydrated_results_key ] ) ) {
unset( $log_context['block_info']['source_args'][ self::$hydrated_results_key ] );
}

do_action( self::$log_action_name, LogLevel::ERROR, $error->get_error_message(), $log_context, $error );
$log_context['error'] = $error;
$log_context['type'] = 'block-binding';

self::$logger->error( $error->get_error_message(), $log_context );
}

protected static function log_success( array $log_context ): void {
if ( null === self::$logger ) {
return;
}

// Remove key "hydrated_results" if it exists.
if ( isset( $log_context['source_args'][ self::$hydrated_results_key ] ) ) {
unset( $log_context['source_args'][ self::$hydrated_results_key ] );
if ( isset( $log_context['block_info']['source_args'][ self::$hydrated_results_key ] ) ) {
unset( $log_context['block_info']['source_args'][ self::$hydrated_results_key ] );
}

do_action( self::$log_action_name, LogLevel::INFO, 'Successfully resolved block binding', $log_context );
$log_context['type'] = 'block-binding';

self::$logger->info( 'Successfully resolved block binding', $log_context );
}
}
Loading
Loading