|
1 | 1 | # mercurius-logging |
2 | | -Log the GraphQL details |
| 2 | + |
| 3 | +[](https://standardjs.com) |
| 4 | +[](https://github.com/Eomm/mercurius-logging/actions/workflows/ci.yml) |
| 5 | + |
| 6 | +This plugin add a Log with all the GraphQL details you need. |
| 7 | + |
| 8 | +## The issue |
| 9 | + |
| 10 | +By default, Fastify logs a simple request that shows always the same `url`: |
| 11 | + |
| 12 | +```json |
| 13 | +{ |
| 14 | + "level": 30, |
| 15 | + "time": 1660395516356, |
| 16 | + "pid": 83316, |
| 17 | + "hostname": "eomm", |
| 18 | + "name": "gateway", |
| 19 | + "reqId": "req-1", |
| 20 | + "req": { |
| 21 | + "method": "POST", |
| 22 | + "url": "/graphql", |
| 23 | + "hostname": "localhost:60767", |
| 24 | + "remoteAddress": "127.0.0.1", |
| 25 | + "remotePort": 60769 |
| 26 | + }, |
| 27 | + "msg": "incoming request" |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +This output does not let you know which queries or mutations are being executed, |
| 32 | +unless you print or inspect the GQL payload. |
| 33 | + |
| 34 | +This plugin adds this log output to your application: |
| 35 | + |
| 36 | +```json |
| 37 | +{ |
| 38 | + "level": 30, |
| 39 | + "time": 1660395516406, |
| 40 | + "pid": 83316, |
| 41 | + "hostname": "eomm", |
| 42 | + "name": "gateway", |
| 43 | + "reqId": "req-1", |
| 44 | + "graphql": { |
| 45 | + "queries": [ |
| 46 | + "myTeam", |
| 47 | + "myTeam" |
| 48 | + ] |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +When the request contains some mutations: |
| 54 | + |
| 55 | +```json |
| 56 | +{ |
| 57 | + "level": 30, |
| 58 | + "time": 1660395516406, |
| 59 | + "pid": 83316, |
| 60 | + "hostname": "eomm", |
| 61 | + "name": "gateway", |
| 62 | + "reqId": "req-1", |
| 63 | + "graphql": { |
| 64 | + "mutations": [ |
| 65 | + "resetCounter" |
| 66 | + ] |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## Install |
| 72 | + |
| 73 | +``` |
| 74 | +npm install mercurius-logging |
| 75 | +``` |
| 76 | + |
| 77 | +## Usage |
| 78 | + |
| 79 | +```js |
| 80 | +const Fastify = require('fastify') |
| 81 | +const mercurius = require('mercurius') |
| 82 | +const mercuriusLogging = require('mercurius-logging') |
| 83 | + |
| 84 | +const app = Fastify({ |
| 85 | + logger: true, |
| 86 | + disableRequestLogging: true |
| 87 | +}) |
| 88 | +t.teardown(app.close.bind(app)) |
| 89 | + |
| 90 | +app.register(mercurius, { |
| 91 | + schema: yourSchema, |
| 92 | + resolvers: yourResolvers |
| 93 | +}) |
| 94 | +app.register(mercuriusLogging) |
| 95 | +``` |
| 96 | + |
| 97 | +## Options |
| 98 | + |
| 99 | +You can customize the output of the plugin by passing an options object: |
| 100 | + |
| 101 | +```js |
| 102 | +app.register(mercuriusLogging, { |
| 103 | + logLevel: 'debug', // default: 'info' |
| 104 | + prependAlias: true, // default: false |
| 105 | +}) |
| 106 | +``` |
| 107 | + |
| 108 | +### logLevel |
| 109 | + |
| 110 | +The log level of the plugin. Note that the `request` logger is used, so you will get the additional |
| 111 | +[request data](https://www.fastify.io/docs/latest/Reference/Logging/#usage) such as the `reqId`. |
| 112 | + |
| 113 | +### prependAlias |
| 114 | + |
| 115 | +Queries and mutations may have an alias. If you want to append the alias to the log, set this option to `true`. |
| 116 | +You will get the following output: |
| 117 | + |
| 118 | +```json |
| 119 | +{ |
| 120 | + "level": 30, |
| 121 | + "graphql": { |
| 122 | + "queries": [ |
| 123 | + "firstQuery:myTeam", |
| 124 | + "secondQuery:myTeam" |
| 125 | + ] |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +## License |
| 131 | + |
| 132 | +Copyright [Manuel Spigolon](https://github.com/Eomm), Licensed under [MIT](./LICENSE). |
0 commit comments