|
2 | 2 |
|
3 | 3 | A WPGraphQL logging plugin that provides visibility into request lifecycle to help quickly identify and resolve bottlenecks in your headless WordPress application. |
4 | 4 |
|
5 | | -* [Join the Headless WordPress community on Discord.](https://discord.gg/headless-wordpress-836253505944813629) |
6 | | -* [Documentation](#getting-started) |
7 | | - |
8 | 5 | > [!CAUTION] |
9 | | -> This plugin is currently in alpha state and is not production ready but please feel free to test. |
10 | | -
|
11 | | ------ |
12 | | - |
13 | | -@TODO |
| 6 | +> This plugin is currently in beta state and is not production ready but please feel free to test. |
14 | 7 |
|
15 | 8 | ----- |
16 | 9 |
|
17 | | - |
18 | | - |
19 | | -## Table of Contents |
20 | | - |
21 | | -- [Overview](#overview) |
22 | | -- [Getting Started](#getting-started) |
23 | | -- [Features](#features) |
24 | | -- [Usage](#usage) |
25 | | -- [Configuration](#configuration) |
26 | | -- [Admin & Settings](#admin--settings) |
27 | | -- [Extending the Functionality](#extending-the-functionality) |
28 | | -- [Testing](#testing) |
| 10 | +[](https://github.com/wpengine/hwptoolkit/releases) |
| 11 | +[](https://www.gnu.org/licenses/gpl-2.0.html) |
| 12 | + |
| 13 | + |
| 14 | +[](https://github.com/wpengine/hwptoolkit/actions) |
| 15 | +[](https://github.com/wpengine/hwptoolkit/actions) |
| 16 | +[](https://github.com/wpengine/hwptoolkit/actions) |
29 | 17 |
|
30 | 18 |
|
31 | 19 | ----- |
32 | 20 |
|
33 | 21 | ## Overview |
34 | 22 |
|
35 | 23 |
|
36 | | -WPGraphQL Logging is a plugin that integrates directly with the WPGraphQL Query Lifecycle, capturing detailed information about each GraphQL request. By leveraging the powerful [Monolog](https://github.com/Seldaek/monolog) logging library, it records events and metrics that help you quickly identify performance bottlenecks and other issues in your headless WordPress application. |
37 | | - |
38 | | -Designed with extensibility in mind, developers can easily customize and extend the logging functionality to suit their specific needs, making it a valuable tool for debugging and optimizing WPGraphQL-powered sites. |
39 | | - |
40 | | ---- |
41 | | - |
42 | | - |
43 | | -## Getting Started |
44 | | - |
45 | | -To install, you need to follow our guide here to install the plugin via composer - [https://github.com/wpengine/hwptoolkit/blob/main/docs/how-to/install-toolkit-plugins/index.md] |
46 | | - |
47 | | -Once you have the composer repository setup, please run `composer req wpengine/wpgraphql-logging:*` to install the plugin. |
48 | | - |
49 | | -Plugin should start logging data, once activated. |
50 | | - |
51 | | ---- |
52 | | - |
53 | | -## Uninstallation and Data Cleanup |
54 | | - |
55 | | -By default, WPGraphQL Logging preserves all logged data when the plugin is deactivated to prevent accidental data loss. If you want to completely remove all plugin data (including database tables) when deactivating the plugin, you must explicitly enable this behavior. |
56 | | - |
57 | | -### Enabling Database Cleanup on Deactivation |
| 24 | +WPGraphQL Logging is a plugin that integrates directly with the WPGraphQL Query Lifecycle, capturing detailed information about each GraphQL request. |
58 | 25 |
|
59 | | -To enable automatic database cleanup when the plugin is deactivated, add the following constant to your `wp-config.php` file or in a must-use plugin: |
| 26 | +### Key Features |
| 27 | +* **Granular Control**: Choose which events in the request lifecycle to log, giving you precise control over the data you capture. |
| 28 | +* **Developer-Friendly Extensibility**: Built with developers in mind, it features a pub/sub system that allows you to hook into the logging pipeline, transform event data, and trigger custom actions. |
| 29 | +* **Flexible Log Handling**: Leverages the powerful Monolog logging library, enabling developers to add custom processors and handlers to route logs to various destinations like Slack, files, or external services. |
60 | 30 |
|
61 | | -```php |
62 | | -define( 'WP_GRAPHQL_LOGGING_UNINSTALL_PLUGIN', true ); |
63 | | -``` |
64 | | - |
65 | | -> [!WARNING] |
66 | | -> **Data Loss Warning**: When `WP_GRAPHQL_LOGGING_UNINSTALL_PLUGIN` is defined as `true`, deactivating the plugin will permanently delete all logged data and drop the plugin's database tables. This action is irreversible. |
67 | | -
|
68 | | -### Manual Data Cleanup |
69 | | - |
70 | | -If you prefer to manually clean up data without defining the constant, you can: |
71 | | - |
72 | | -1. Use the plugin's admin interface to clear logs (when available) |
73 | | -2. Manually drop the database table: `{$wpdb->prefix}wpgraphql_logging` |
74 | | -3. Remove plugin options from the WordPress options table |
| 31 | +>[!IMPORTANT] |
| 32 | +>For detailed developer guides and examples, see our [How-To Guides](docs/index.md#how-to-guides). |
75 | 33 |
|
76 | 34 | --- |
77 | 35 |
|
78 | | -@TODO add more info once we have configuration setup. |
79 | | - |
80 | | -@TODO add more info once we have configuration setup. |
81 | | - |
82 | | - |
83 | | ---- |
84 | | - |
85 | | -## Project Structure |
86 | | - |
87 | | -```text |
88 | | -wpgraphql-logging/ |
89 | | -├── docs/ # Docs for extending the plugin. |
90 | | -├── src/ # Main plugin source code |
91 | | -│ ├── Admin/ # Admin settings, menu, and settings page logic |
92 | | -│ ├── Settings/ # Admin settings functionality for displaying and saving data. |
93 | | -│ ├── Events/ # Event logging, pub/sub event manager for extending the logging. |
94 | | -│ ├── Logger/ # Logging logic, logger service, Monolog handlers & processors |
95 | | -│ ├── Rules/ # Rule Management on whether we log a query |
96 | | -│ ├── Scheduler/ # Automated data cleanup and maintenance tasks |
97 | | -│ ├── Plugin.php # Main plugin class (entry point) |
98 | | -│ └── Autoload.php # PSR-4 autoloader |
99 | | -├── tests/ # All test suites |
100 | | -│ ├── wpunit/ # WPBrowser/Codeception unit tests |
101 | | -├── [wpgraphql-logging.php] |
102 | | -├── [activation.php] |
103 | | -├── [composer.json] |
104 | | -├── [deactivation.php] |
105 | | -├── [TESTING.md] |
106 | | -├── [README.md] |
107 | | -``` |
108 | | - |
109 | | -## Features |
110 | | - |
111 | | -- **Query event lifecycle logging** |
112 | | - - **Pre Request** (`do_graphql_request`): captures `query`, `variables`, `operation_name`. |
113 | | - - **Before Execution** (`graphql_before_execute`): includes a snapshot of request `params`. |
114 | | - - **Before Response Returned** (`graphql_return_response`): inspects `response` and automatically upgrades level to Error when GraphQL `errors` are present (adds `errors` to context when found). |
115 | | - |
116 | | -- **Built-in pub/sub event bus** |
117 | | - - In-memory event manager with priorities: `subscribe(event, listener, priority)` and `publish(event, payload)`. |
118 | | - - Transform pipeline: `transform(event, payload)` lets you mutate `context` and `level` before logging/publishing. |
119 | | - - WordPress bridges: actions `wpgraphql_logging_event_{event}` and filters `wpgraphql_logging_filter_{event}` to integrate with standard hooks. |
120 | | - |
121 | | -- **Monolog-powered logging pipeline** |
122 | | - - Default handler: stores logs in a WordPress table (`{$wpdb->prefix}wpgraphql_logging`). |
123 | | - |
124 | | -- **Automated data management** |
125 | | - - **Daily cleanup scheduler**: Automatically removes old logs based on retention settings. |
126 | | - - **Configurable retention period**: Set how many days to keep log data (default: 30 days). |
127 | | - - **Manual cleanup**: Admin interface to trigger immediate cleanup of old logs. |
128 | | - - **Data sanitization**: Remove sensitive fields from logged data for privacy compliance. |
129 | | - |
130 | | -- **Simple developer API** |
131 | | - - `Plugin::on()` to subscribe, `Plugin::emit()` to publish, `Plugin::transform()` to modify payloads. |
132 | | - |
133 | | -- **Safe-by-default listeners/transforms** |
134 | | - - Exceptions in listeners/transforms are caught and logged without breaking the pipeline. |
135 | | - |
136 | | ---- |
137 | | - |
138 | | -## Data Sanitization |
139 | | - |
140 | | -WPGraphQL Logging includes robust data sanitization capabilities to help you protect sensitive information while maintaining useful logs for debugging and monitoring. The sanitization system allows you to automatically clean, anonymize, or remove sensitive fields from log records before they are stored. |
| 36 | +## Requirements |
141 | 37 |
|
142 | | -### Why Data Sanitization Matters |
| 38 | +- WordPress 6.5+ |
| 39 | +- WPGraphQL 2.3+ |
| 40 | +- PHP 8.1.2+ |
143 | 41 |
|
144 | | -When logging GraphQL requests, context data often contains sensitive information such as: |
145 | | -- User authentication tokens |
146 | | -- Personal identification information (PII) |
147 | | -- Password fields |
148 | | -- Session data |
149 | | -- Internal system information |
150 | 42 |
|
151 | | -Data sanitization ensures compliance with privacy regulations (GDPR, CCPA) and security best practices while preserving the debugging value of your logs. |
| 43 | +## Installation |
152 | 44 |
|
153 | | -### Sanitization Methods |
| 45 | +### Option 1: Plugin Zip |
154 | 46 |
|
155 | | -The plugin offers two sanitization approaches: |
| 47 | +You can get the latest release from <https://github.com/wpengine/hwptoolkit/releases?q=wpgraphql-logging&expanded=true>. |
156 | 48 |
|
157 | | -#### 1. Recommended Rules (Default) |
158 | | -Pre-configured rules that automatically sanitize common WordPress and WPGraphQL sensitive fields: |
159 | | -- `request.app_context.viewer.data` - User data object |
160 | | -- `request.app_context.viewer.allcaps` - User capabilities |
161 | | -- `request.app_context.viewer.cap_key` - Capability keys |
162 | | -- `request.app_context.viewer.caps` - User capability array |
| 49 | +### Option 2: Composer |
163 | 50 |
|
164 | | -#### 2. Custom Rules |
165 | | -Define your own sanitization rules using dot notation to target specific fields: |
| 51 | +To install, you need to follow our guide here to install the plugin via composer - <https://github.com/wpengine/hwptoolkit/blob/main/docs/how-to/install-toolkit-plugins/index.md> |
166 | 52 |
|
167 | | -**Field Path Examples:** |
168 | | -``` |
169 | | -variables.password |
170 | | -request.headers.authorization |
171 | | -user.email |
172 | | -variables.input.creditCard |
173 | | -``` |
174 | | - |
175 | | -### Sanitization Actions |
176 | | - |
177 | | -For each field, you can choose from three sanitization actions: |
178 | | - |
179 | | -| Action | Description | Example | |
180 | | -|--------|-------------|---------| |
181 | | -| **Remove** | Completely removes the field from logs | `password: "secret123"` → *field removed* | |
182 | | -| **Anonymize** | Replaces value with `***` | `email: "user@example.com"` → `email: "***"` | |
183 | | -| **Truncate** | Limits string length to 47 characters + `...` | `longText: "Very long text..."` → `longText: "Very long text that gets cut off here and mo..."` | |
184 | | - |
185 | | -### Configuration |
186 | | - |
187 | | -Enable and configure data sanitization through the WordPress admin: |
188 | | - |
189 | | -1. Navigate to **GraphQL Logging → Settings** |
190 | | -2. Click the **Data Management** tab |
191 | | -3. Enable **Data Sanitization** |
192 | | -4. Choose your sanitization method: |
193 | | - - **Recommended**: Uses pre-configured rules for common sensitive fields |
194 | | - - **Custom**: Define your own field-specific rules |
195 | | - |
196 | | -#### Custom Configuration Fields |
197 | | - |
198 | | -When using custom rules, configure the following fields: |
199 | | - |
200 | | -- **Fields to Remove**: Comma-separated list of field paths to completely remove |
201 | | -- **Fields to Anonymize**: Comma-separated list of field paths to replace with `***` |
202 | | -- **Fields to Truncate**: Comma-separated list of field paths to limit length |
203 | | - |
204 | | -**Example Configuration:** |
205 | | -``` |
206 | | -Remove: variables.password, request.headers.authorization |
207 | | -Anonymize: user.email, variables.input.personalInfo |
208 | | -Truncate: query, variables.input.description |
209 | | -``` |
210 | | - |
211 | | -### Developer Hooks |
212 | | - |
213 | | -Customize sanitization behavior using WordPress filters: |
214 | | - |
215 | | -```php |
216 | | -// Enable/disable sanitization programmatically |
217 | | -add_filter( 'wpgraphql_logging_data_sanitization_enabled', function( $enabled ) { |
218 | | - return current_user_can( 'manage_options' ) ? false : $enabled; |
219 | | -}); |
220 | | - |
221 | | -// Modify recommended rules |
222 | | -add_filter( 'wpgraphql_logging_data_sanitization_recommended_rules', function( $rules ) { |
223 | | - $rules['custom.sensitive.field'] = 'remove'; |
224 | | - return $rules; |
225 | | -}); |
226 | | - |
227 | | -// Modify all sanitization rules |
228 | | -add_filter( 'wpgraphql_logging_data_sanitization_rules', function( $rules ) { |
229 | | - // Add additional rules or modify existing ones |
230 | | - $rules['request.custom_header'] = 'anonymize'; |
231 | | - return $rules; |
232 | | -}); |
233 | | - |
234 | | -// Modify the final log record after sanitization |
235 | | -add_filter( 'wpgraphql_logging_data_sanitization_record', function( $record ) { |
236 | | - // Additional processing after sanitization |
237 | | - return $record; |
238 | | -}); |
239 | | -``` |
240 | | - |
241 | | -### Performance Considerations |
242 | | - |
243 | | -- Sanitization runs on every log record when enabled |
244 | | -- Complex nested field paths may impact performance on high-traffic sites |
245 | | -- Consider using recommended rules for optimal performance |
246 | | -- Test custom rules thoroughly to ensure they target the intended fields |
247 | | - |
248 | | -### Security Best Practices |
249 | | - |
250 | | -1. **Review logs regularly** to ensure sanitization is working as expected |
251 | | -2. **Test field paths** in a development environment before applying to production |
252 | | -3. **Use remove over anonymize** for highly sensitive data |
253 | | -4. **Monitor performance impact** when implementing extensive custom rules |
254 | | -5. **Keep rules updated** as your GraphQL schema evolves |
255 | | - |
256 | | ---- |
257 | | - |
258 | | -## Usage |
259 | | - |
260 | | -WPGraphQL Logging Plugin is highly configurable and extendable and built with developers in mind to allow them to modify, change or add data, loggers etc to this plugin. Please read the docs below: |
261 | | - |
262 | | - |
263 | | -The following documentation is available in the `docs/` directory: |
264 | | - |
265 | | -- [Events](docs/Events.md): |
266 | | - Learn about the event system, available events, and how to subscribe, transform, or listen to WPGraphQL Logging events. |
267 | | - |
268 | | -- [Logging](docs/Logging.md): |
269 | | - Learn about the logging system, Monolog integration, handlers, processors, and how to use or extend the logger. |
270 | | - |
271 | | -- [Admin](docs/admin.md): |
272 | | - Learn how the admin settings page works, all available hooks, and how to add tabs/fields via actions and filters. |
273 | | - |
274 | | ---- |
275 | | - |
276 | | - |
277 | | - |
278 | | -## Configuration |
279 | | - |
280 | | -@TODO - When we integrate plugin configuration. |
281 | | - |
282 | | ---- |
283 | | - |
284 | | -### Settings |
| 53 | +Once you have the composer repository setup, please run `composer req wpengine/wpgraphql-logging:*` to install the plugin. |
285 | 54 |
|
286 | | -@TODO - When we integrate plugin configuration. |
| 55 | +Once installed and configured, the plugin should begin to log uncached WPGraphQL logs into the database. |
287 | 56 |
|
288 | 57 | --- |
289 | 58 |
|
290 | | -## Admin & Settings |
291 | | - |
292 | | -See `docs/admin.md` for a full overview of the admin/settings architecture, hooks, and examples for adding tabs and fields. |
293 | | - |
294 | | -## Testing |
| 59 | +## Documentation |
295 | 60 |
|
296 | | -See [Testing.md](TESTING.md) for details on how to test the plugin. |
| 61 | +For detailed usage instructions, developer references, and examples, please visit the [Documentation](docs/index.md) folder included with this plugin. |
297 | 62 |
|
298 | | -## Screenshots |
299 | 63 |
|
300 | | -@TODO - When before BETA release. |
| 64 | +## License |
| 65 | +WP GPL 2 |
0 commit comments