• v3 (beta)
  • Features
  • Logging and Debugging

Logging and Debugging

Logging

The default logger in Yoga is the JavaScript console and its respective methods (debug when DEBUG=1 in environment, info, warn and error) matching the log level.

You can of course provide your own logger for piping to your favorite logging service/utility:

import { createYoga } from 'graphql-yoga'
import { createServer } from 'node:http'
import { schema } from './my-schema'
import { logger } from './my-logger'
 
const yoga = createYoga({
  schema,
  logging: {
    debug(...args) {
      // will only get triggered if DEBUG=1 in environment
      logger.debug(...args)
    },
    info(...args) {
      logger.info(...args)
    },
    warn(...args) {
      logger.warn(...args)
    },
    error(...args) {
      logger.error(...args)
    }
  }
})
 
const server = createServer(yoga)
 
server.listen(4000, () => {
  console.info('Server is running on http://localhost:4000/graphql')
})

Further hook into the logging system by leveraging envelop's useLogger plugin:

import { createYoga, useLogger } from 'graphql-yoga'
import { createServer } from 'node:http'
import { schema } from './my-schema'
import { logger } from './my-logger'
 
const yoga = createYoga({
  schema,
  plugins: [
    useLogger({
      logFn: (eventName, args) => {
        // Event could be execute-start / execute-end / subscribe-start / subscribe-end / etc.
        // args will include the arguments passed to execute/subscribe (in case of "start" event) and additional result in case of "end" event.
        logger.debug(eventName, ...args)
      }
    })
  ]
})
 
const server = createServer(yoga)
 
server.listen(4000, () => {
  console.info('Server is running on http://localhost:4000/graphql')
})

Debugging

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:

  • Processing of GraphQL parameters
  • Parsing of GraphQL parameters
  • Execution or subscription start
  • Received GraphQL operation variables
  • Execution or subscription end
  • GraphiQL rendering
  • Health checks
Last updated on October 2, 2022