Skip to content

Commit 015d22d

Browse files
authored
Merge pull request #534 from wpengine/add-logging-plugin-example
docs(logging): Add a basic usage example and tutorial
2 parents 154a98d + cb49b06 commit 015d22d

File tree

31 files changed

+4059
-33
lines changed

31 files changed

+4059
-33
lines changed
Lines changed: 162 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,202 @@
11
---
2-
title: "Tutorial Learn WPGRaphQL Logging Plugin"
3-
description: "Learn how to install and configure the WPGraphQL Logging Plugin, then customize it using its extensible architecture."
2+
title: "Get Started with WPGraphQL Logging"
3+
description: "Learn how to install and configure the WPGraphQL Logging plugin, view logged data, and extend it with custom context data."
44
---
55

6-
@TODO
6+
In this tutorial, you will install and configure the WPGraphQL Logging plugin to track GraphQL queries in your WordPress site. By the end, you will have a working logging system that captures query data, and you will know how to extend it with custom context data.
77

8-
## Introduction
8+
We will use WPGraphQL IDE to run queries, view logs in the WordPress admin, and write PHP code to extend the plugin's functionality.
99

10-
This tutorial will guide you through the core features of the WPGraphQL Logging Plugin. You will install and set up the plugin, and then you will see log data appear instantly.
10+
> [!TIP]
11+
> This tutorial assumes you're working with a local WordPress development environment. If you don't have one, you can set one up using [Local](https://localwp.com/).
1112
12-
Next, we will extend the plugin by:
13+
## What you'll build
1314

14-
- Subscribing to an event to add more context data
15-
- Displaying this new data on the admin grid
16-
- Adding a rule to the list of rules for the rule manager
17-
- Writing data to a new file handler
15+
By following this tutorial, you will create:
1816

17+
* A working WPGraphQL Logging setup that tracks GraphQL queries
18+
* Custom context data added to logs using the event system
19+
* A configured admin interface to view and filter logged queries
1920

20-
By the end of this tutorial, you will be able to navigate and configure the plugin, and you will have the confidence to extend its functionality to suit your specific needs.
21+
## Prerequisites
2122

22-
### Prerequisites
23+
Before starting, make sure you have:
2324

24-
In order to complete this tutorial, you should:
25+
* WordPress 6.5 or higher installed
26+
* PHP 8.1.2 or higher
27+
* WPGraphQL 2.3 or higher installed and activated
28+
* Basic familiarity with WordPress, WPGraphQL, and PHP
29+
* The ability to add PHP code to a custom plugin or theme's functions.php
2530

26-
- Be familiar with WordPress, WPGraphQL & PHP
27-
- Be familiar with installing a plugin for WordPress
28-
- Be comfortable to be able to add PHP code to a custom plugin
31+
## Step 1: Install the WPGraphQL Logging plugin
2932

30-
Before you get started you need the following
33+
First, we will install the WPGraphQL Logging plugin. Since this plugin extends WPGraphQL, make sure you have WPGraphQL installed and activated before proceeding.
3134

35+
Download the latest release from the GitHub repository:
3236

33-
## Setup
37+
Visit <https://github.com/wpengine/hwptoolkit/releases/latest/download/wpgraphql-logging.zip> and download the plugin zip file.
3438

39+
Install and activate the plugin through your WordPress admin.
3540

36-
### 1. Install the Plugin
41+
![WordPress plugins page showing WPGraphQL Logging plugin activated with a blue "Deactivate" link, alongside the WPGraphQL plugin also showing as active](../screenshots/logging-plugin-installed.png)
3742

38-
### 2. Setup the configuration
43+
## Step 2: Configure the logging plugin
3944

45+
Now we will configure the logging settings to start capturing queries.
4046

41-
## Logging & Viewing Data
47+
In WordPress admin, go to GraphQL Logs > Settings.
4248

43-
- Using WPGraphQL IDE
44-
- Logging a query
45-
- Show the user
49+
You should see the Basic Configuration tab with several settings. Notice the default configuration:
4650

51+
* **Enabled**: Checked (logging is on)
52+
* **Exclude Queries**: `__schema,GetSeedNode` (to skip introspection queries)
53+
* **Data Sampling Rate**: 10% (only logs 10% of queries by default)
54+
* **Log Points**: All events selected
4755

48-
## Extending and adding data to the logs
56+
![GraphQL Logs Settings page showing the Basic Configuration tab with default settings including Enabled checkbox checked, Exclude Queries field populated with "__schema,GetSeedNode", and Data Sampling Rate dropdown showing "10% (Every 10th request)"](../screenshots/logging-plugin-settings.png)
4957

50-
### Event
58+
Let's adjust the sampling rate so we see logs more consistently:
5159

52-
### Admin Grid
60+
1. Find the "Data Sampling Rate" dropdown
61+
2. Change it to "100% (All requests)"
62+
3. Scroll down and click "Save Changes"
5363

64+
You should see a success message confirming your settings were saved.
5465

55-
### Add Configuration
66+
## Step 3: Generate log entries
5667

57-
### Rule Manager
68+
Now we will run some GraphQL queries to generate log data.
5869

70+
Go back to GraphQL > GraphiQL IDE.
5971

60-
### Updating the handler
72+
Run the following query:
6173

74+
```graphql
75+
query GetPosts {
76+
posts(first: 3) {
77+
nodes {
78+
id
79+
title
80+
date
81+
}
82+
}
83+
}
84+
```
6285

86+
Click the "Play" button. You should see your posts in the response.
6387

64-
## Conclusion
88+
Now run a different query:
6589

90+
```graphql
91+
query GetPages {
92+
pages(first: 3) {
93+
nodes {
94+
id
95+
title
96+
content
97+
}
98+
}
99+
}
100+
```
66101

102+
Notice how we're running queries with named operations (`GetPosts` and `GetPages`). These names will help us identify queries in the logs.
67103

104+
## Step 4: View your logs
68105

106+
Let's look at the logged query data.
69107

108+
In WordPress admin, go to GraphQL Logs > All Logs.
70109

71-
## Contributing
110+
You should see a table displaying your logged queries. Each row represents one GraphQL query execution.
72111

73-
We welcome and appreciate contributions from the community. If you'd like to help improve this documentation, please check out our [Contributing Guide](https://github.com/wpengine/hwptoolkit/blob/main/CONTRIBUTING.md) for more details on how to get started.
112+
![All Logs page showing a table with logged GraphQL query entries](../screenshots/logging-plugin-all-logs.png)
113+
114+
Notice the information captured for each log entry:
115+
116+
* **Date**: When the query was executed
117+
* **Query**: The query itself (GetPosts, GetPages)
118+
* **Level**: The log level (usually INFO for successful queries)
119+
* **Event**: Which point in the request lifecycle was logged
120+
121+
Click on any log entry to see more details. You should see a detail view with the full query text, variables (if any), and additional context data.
122+
123+
![Log entry details page showing full query text, variables, and context data](../screenshots/logging-plugin-details.png)
124+
125+
## Step 5: Add custom context data to logs
126+
127+
Now we will extend the logging system by adding custom context data to each log entry.
128+
129+
The plugin uses an event system that allows you to transform log payloads before they're saved. We will subscribe to an event and add custom data.
130+
131+
> [!TIP]
132+
> For more detailed information about the event system and available events, see the [How To: Add Data to an Event](../../how-to/event-add-context/index.md) guide.
133+
134+
Create a custom plugin file or add to your theme's `functions.php`:
135+
136+
```php
137+
<?php
138+
use WPGraphQL\Logging\Plugin;
139+
use WPGraphQL\Logging\Events\Events;
140+
use Monolog\Level;
141+
142+
// Add custom context to the PRE_REQUEST event
143+
add_action( 'init', function() {
144+
Plugin::transform( Events::PRE_REQUEST, function( array $payload ): array {
145+
$payload['context']['environment'] = wp_get_environment_type();
146+
$payload['context']['app_id'] = 'my-headless-app';
147+
148+
// Optional: change the log level for this specific event
149+
$payload['level'] = Level::Error;
150+
151+
return $payload;
152+
}, 10 );
153+
} );
154+
```
155+
156+
This code subscribes to the `PRE_REQUEST` event and adds custom context data to the log:
157+
* `environment`: The current WordPress environment type (production, staging, development, or local)
158+
* `app_id`: A custom identifier for your application (useful when logging from multiple apps)
159+
* `level`: Changes the log level to Error for this event (optional demonstration)
160+
161+
The transform function runs during the `init` action, modifying the payload before it's logged. Notice how we use the `Plugin::transform()` helper method and the `Events::PRE_REQUEST` constant to target a specific lifecycle event.
162+
163+
Save your file and refresh your WordPress site to ensure the code is loaded.
164+
165+
## Step 6: Test the custom context data
166+
167+
Let's verify that our custom context data is being added to logs.
168+
169+
Go back to GraphQL > GraphiQL IDE and run another query:
170+
171+
```graphql
172+
query TestCustomContext {
173+
posts(first: 1) {
174+
nodes {
175+
title
176+
}
177+
}
178+
}
179+
```
180+
181+
Now go to GraphQL Logs > All Logs.
182+
183+
You should see your new `TestCustomContext` queries. Find the one with "WPGraphQL Pre Request" event and click on it to view the details.
184+
185+
In the context data section, you should now see your custom fields:
186+
187+
![Log entry detail showing custom context fields including environment, app_id, and the modified log level (Error) alongside standard context data](../screenshots/logging-plugin-extra-context.png)
188+
189+
Notice how your custom data appears alongside the default context data like memory usage and request information. Also note that the log level changed to "Error" because we modified it in the transform function. This demonstrates how easily you can extend the logging system to capture application-specific data and even modify logging behavior dynamically.
190+
191+
## Next steps
192+
193+
Now that you have a working WPGraphQL Logging setup and understand how to extend it, you can:
194+
195+
* Add more sophisticated custom context data based on your application's needs - see [How To: Add Data to an Event](../../how-to/event-add-context/index.md)
196+
* Create custom rules that check configuration values or query patterns - see [How To: Add or Remove a Rule](../../how-to/logger-add-remove-rules/index.md)
197+
* Export logs to CSV for offline analysis
198+
* Configure data retention policies in the Data Management tab
199+
* Add custom processors to transform log data - see [How To: Add a Processor](../../how-to/logger-add-processor/index.md)
200+
* Implement custom handlers to send logs to external services - see [How To: Add a Handler](../../how-to/logger-add-handler/index.md)
201+
202+
For more details about extending the plugin, see the [How-To Guides](https://github.com/wpengine/hwptoolkit/tree/main/docs/plugins/wpgraphql-logging) which include guides for adding custom processors, handlers, and admin interface customizations.
250 KB
Loading
154 KB
Loading
158 KB
Loading
175 KB
Loading
210 KB
Loading

plugins/wpgraphql-logging/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ tests/_data/
5656

5757
# Playwright outputs
5858
artifacts
59+
60+
# Keep example SQL files
61+
!examples/**/*.sql
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"phpVersion": "8.1",
3+
"plugins": [
4+
"https://github.com/wp-graphql/wp-graphql/releases/latest/download/wp-graphql.zip",
5+
"https://github.com/wpengine/hwptoolkit/releases/latest/download/wpgraphql-logging.zip"
6+
],
7+
"config": {
8+
"WP_DEBUG": true,
9+
"SCRIPT_DEBUG": false,
10+
"GRAPHQL_DEBUG": true,
11+
"WP_DEBUG_LOG": true,
12+
"WP_DEBUG_DISPLAY": false,
13+
"SAVEQUERIES": false
14+
},
15+
"mappings": {
16+
"db": "./wp-env/db",
17+
"wp-content/uploads": "./wp-env/uploads",
18+
".htaccess": "./wp-env/setup/.htaccess"
19+
},
20+
"lifecycleScripts": {
21+
"afterStart": "wp-env run cli -- wp rewrite structure '/%postname%/' && wp-env run cli -- wp rewrite flush"
22+
}
23+
}

0 commit comments

Comments
 (0)