|
| 1 | +# Logging and debugging |
| 2 | + |
| 3 | +## Logging |
| 4 | + |
| 5 | +The default logger in Yoga is the [JavaScript console](https://developer.mozilla.org/en-US/docs/Web/API/console) and its respective methods ([debug](https://developer.mozilla.org/en-US/docs/Web/API/console/debug) _when `DEBUG=1` in environment_, [info](https://developer.mozilla.org/en-US/docs/Web/API/console/info), [warn](https://developer.mozilla.org/en-US/docs/Web/API/console/warn) and [error](https://developer.mozilla.org/en-US/docs/Web/API/console/error)) matching the log level. |
| 6 | + |
| 7 | +You can of course provide your own logger for piping to your favourite logging service/utility: |
| 8 | + |
| 9 | +```ts |
| 10 | +import { createYoga } from 'graphql-yoga' |
| 11 | +import { createServer } from 'node:http' |
| 12 | +import { schema } from './my-schema' |
| 13 | +import { logger } from './my-logger' |
| 14 | + |
| 15 | +const yoga = createYoga({ |
| 16 | + schema, |
| 17 | + logging: { |
| 18 | + debug(...args) { |
| 19 | + // will only get triggered if DEBUG=1 in environment |
| 20 | + logger.debug(...args) |
| 21 | + }, |
| 22 | + info(...args) { |
| 23 | + logger.info(...args) |
| 24 | + }, |
| 25 | + warn(...args) { |
| 26 | + logger.warn(...args) |
| 27 | + }, |
| 28 | + error(...args) { |
| 29 | + logger.error(...args) |
| 30 | + }, |
| 31 | + }, |
| 32 | +}) |
| 33 | + |
| 34 | +const server = createServer(yoga) |
| 35 | + |
| 36 | +server.listen(4000, () => { |
| 37 | + console.info('Server is running on http://localhost:4000/graphql') |
| 38 | +}) |
| 39 | +``` |
| 40 | + |
| 41 | +Further hook into the logging system by leveraging [envelop](https://www.envelop.dev/plugins/use-logger)'s [`useLogger`](https://www.envelop.dev/plugins/use-logger) plugin: |
| 42 | + |
| 43 | +```ts |
| 44 | +import { createYoga, useLogger } from 'graphql-yoga' |
| 45 | +import { createServer } from 'node:http' |
| 46 | +import { schema } from './my-schema' |
| 47 | +import { logger } from './my-logger' |
| 48 | + |
| 49 | +const yoga = createYoga({ |
| 50 | + schema, |
| 51 | + plugins: [ |
| 52 | + useLogger({ |
| 53 | + logFn: (eventName, args) => { |
| 54 | + // Event could be execute-start / execute-end / subscribe-start / subscribe-end / etc. |
| 55 | + // args will include the arguments passed to execute/subscribe (in case of "start" event) and additional result in case of "end" event. |
| 56 | + logger.debug(eventName, ...args) |
| 57 | + }, |
| 58 | + }), |
| 59 | + ], |
| 60 | +}) |
| 61 | + |
| 62 | +const server = createServer(yoga) |
| 63 | + |
| 64 | +server.listen(4000, () => { |
| 65 | + console.info('Server is running on http://localhost:4000/graphql') |
| 66 | +}) |
| 67 | +``` |
| 68 | + |
| 69 | +## Debugging |
| 70 | + |
| 71 | +Note that Yoga can run in debug when having `DEBUG=1` in the environment. Doing so will increase verbosity of the logger delivering additional information including: |
| 72 | + |
| 73 | +- Processing of GraphQL parameters |
| 74 | +- Parsing of GraphQL parameters |
| 75 | +- Execution or subscription start |
| 76 | +- Received GraphQL operation variables |
| 77 | +- Execution or subscription end |
| 78 | +- [GraphiQL](https://github.com/graphql/graphiql) rendering |
| 79 | +- Healthcheck and readiness checks |
0 commit comments