|
1 | 1 | --- |
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." |
4 | 4 | --- |
5 | 5 |
|
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. |
7 | 7 |
|
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. |
9 | 9 |
|
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/). |
11 | 12 |
|
12 | | -Next, we will extend the plugin by: |
| 13 | +## What you'll build |
13 | 14 |
|
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: |
18 | 16 |
|
| 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 |
19 | 20 |
|
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 |
21 | 22 |
|
22 | | -### Prerequisites |
| 23 | +Before starting, make sure you have: |
23 | 24 |
|
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 |
25 | 30 |
|
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 |
29 | 32 |
|
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. |
31 | 34 |
|
| 35 | +Download the latest release from the GitHub repository: |
32 | 36 |
|
33 | | -## Setup |
| 37 | +Visit <https://github.com/wpengine/hwptoolkit/releases/latest/download/wpgraphql-logging.zip> and download the plugin zip file. |
34 | 38 |
|
| 39 | +Install and activate the plugin through your WordPress admin. |
35 | 40 |
|
36 | | -### 1. Install the Plugin |
| 41 | + |
37 | 42 |
|
38 | | -### 2. Setup the configuration |
| 43 | +## Step 2: Configure the logging plugin |
39 | 44 |
|
| 45 | +Now we will configure the logging settings to start capturing queries. |
40 | 46 |
|
41 | | -## Logging & Viewing Data |
| 47 | +In WordPress admin, go to GraphQL Logs > Settings. |
42 | 48 |
|
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: |
46 | 50 |
|
| 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 |
47 | 55 |
|
48 | | -## Extending and adding data to the logs |
| 56 | + |
49 | 57 |
|
50 | | -### Event |
| 58 | +Let's adjust the sampling rate so we see logs more consistently: |
51 | 59 |
|
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" |
53 | 63 |
|
| 64 | +You should see a success message confirming your settings were saved. |
54 | 65 |
|
55 | | -### Add Configuration |
| 66 | +## Step 3: Generate log entries |
56 | 67 |
|
57 | | -### Rule Manager |
| 68 | +Now we will run some GraphQL queries to generate log data. |
58 | 69 |
|
| 70 | +Go back to GraphQL > GraphiQL IDE. |
59 | 71 |
|
60 | | -### Updating the handler |
| 72 | +Run the following query: |
61 | 73 |
|
| 74 | +```graphql |
| 75 | +query GetPosts { |
| 76 | + posts(first: 3) { |
| 77 | + nodes { |
| 78 | + id |
| 79 | + title |
| 80 | + date |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
62 | 85 |
|
| 86 | +Click the "Play" button. You should see your posts in the response. |
63 | 87 |
|
64 | | -## Conclusion |
| 88 | +Now run a different query: |
65 | 89 |
|
| 90 | +```graphql |
| 91 | +query GetPages { |
| 92 | + pages(first: 3) { |
| 93 | + nodes { |
| 94 | + id |
| 95 | + title |
| 96 | + content |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
66 | 101 |
|
| 102 | +Notice how we're running queries with named operations (`GetPosts` and `GetPages`). These names will help us identify queries in the logs. |
67 | 103 |
|
| 104 | +## Step 4: View your logs |
68 | 105 |
|
| 106 | +Let's look at the logged query data. |
69 | 107 |
|
| 108 | +In WordPress admin, go to GraphQL Logs > All Logs. |
70 | 109 |
|
71 | | -## Contributing |
| 110 | +You should see a table displaying your logged queries. Each row represents one GraphQL query execution. |
72 | 111 |
|
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 | + |
| 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 | + |
| 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 | + |
| 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. |
0 commit comments