Migrate to GraphQL Yoga v5
Logging and Debugging
Logging
Yoga uses 4 log levels debug
, info
, warn
and error
. By default, Yoga will only log info
,
warn
and error
messages.
Log Level Overview
error
- Only log unexpected errors
warn
- All prior log levels
- deprecation notices
info
- All prior log levels
- Information about the current state of the system
debug
- All prior log levels
- Processing of GraphQL parameters
- Parsing of GraphQL parameters
- Execution or subscription start
- Received GraphQL operation variables
- Execution or subscription end
- GraphiQL rendering
- Health checks
Enabling Debug Logging
Instead, on JavaScript environments that support environment variables via process.env
(e.g.
Node.js), you can also conditionally enable debug
logging by setting the DEBUG
environment
variable to 1
.
import { createServer } from 'node:http'
import { createYoga } from 'graphql-yoga'
import { schema } from './my-schema.js'
const yoga = createYoga({
schema,
logging: 'debug'
})
Change log level
You can customize the log lovel by passing on of the 4 levels to the logging
option:
We recommend not changing the default log level and only recommend changing it if you know what you are doing.
import { createServer } from 'node:http'
import { createYoga } from 'graphql-yoga'
import { schema } from './my-schema.js'
const yoga = createYoga({
schema,
logging: 'warn'
})
Specifying the log level of warn
will only log warn
and error
messages being logged.
Custom logger
You can of course provide your own logger for piping to your favorite logging service/utility:
import { createServer } from 'node:http'
import { createYoga } from 'graphql-yoga'
import { logger } from './my-logger.js'
import { schema } from './my-schema.js'
const yoga = createYoga({
schema,
logging: {
debug(...args) {
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 { createServer } from 'node:http'
import { createYoga, useLogger } from 'graphql-yoga'
import { logger } from './my-logger'
import { schema } from './my-schema'
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')
})