| title | How To Guide: Add a new Processor |
|---|---|
| description | Learn how to add custom Monolog processors to the WPGraphQL Logging plugin to transform log records. |
In this guide, you will learn how to extend the logging capabilities of WPGraphQL Logging by adding a new Monolog processor.
Specifically, we will demonstrate how to add the current WordPress environment variable to the extra data, which can be useful for distinguishing between development, staging, and production environments in your logs.
Processors in Monolog add or transform data on each log record before handlers write it. They can modify the context or extra arrays on a record. See Monolog documentation.
Create a PHP class that implements Monolog\Processor\ProcessorInterface and returns the updated LogRecord.
<?php
namespace MyPlugin\Logging;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
class EnvironmentProcessor implements ProcessorInterface {
public function __invoke( LogRecord $record ): LogRecord {
$record->extra['environment'] = wp_get_environment_type();
return $record;
}
}Use the wpgraphql_logging_default_processors filter to add your processor to all logger instances.
<?php
add_filter( 'wpgraphql_logging_default_processors', function( array $processors ) {
$processors[] = new \MyPlugin\Logging\EnvironmentProcessor();
return $processors;
});You should see environment in the log record's extra data (e.g. in the Logs admin UI or your chosen handler output).
Note
The LoggerService allows you to specify your own list of default handlers and processors if you ever prefer to re-use it or change the list of default handlers and processors e.g.
LoggerService::get_instance($channel, $handlers, $processors, $default_context);We welcome and appreciate contributions from the community. If you'd like to help improve this documentation, please check out our Contributing Guide for more details on how to get started.
