RW V7.x.x API fastify logging

Hello,

The logging appears to have changed a bit in V7.x.x with respect to the fastify server.

We are getting all the following logged on our production site (quite overwhelming and blowing up our logs):

INFO incoming request
INFO request completed

We’d like to have the fastify server logging at ‘warn’, but the rest of the app logging set to ‘info’. Looking at the latest createServer function in our server.ts, it has:

import { createServer } from '@redwoodjs/api-server'

import { logger } from 'src/lib/logger'

async function main() {
  const server = await createServer({
    logger,
  })

  await server.start()
}

main()

As it is passing in the main logger instance for the api side, does this mean we would need to create a separate logger instance to have different settings for the fastify server? Or am I missing something?

Thanks!

Hey hey @cjReimer again! :wave:

It seems if you’re using the server file, you could pass in two instances of logger.

In the example above, you’re re-using the same logger. Have you tried creating a different logger and passing to either createServer and createGraphqlHandler?

I’m extrapolating from the source in RW here, it should work in theory :slight_smile:

Thanks @danny!

That’s a simple enough solution, and I assume Logger is a fairly lightweight instance. We’ve implemented the code, and so far it appears to be behaving as expected.

Thanks!

1 Like

@cjReimer you could also try a “child logger” that is set to use a different level.

See pino/docs/child-loggers.md at main · pinojs/pino · GitHub

I believe it’ might be lighter weight what a second instance.

Thanks @dthyresson !

Yes, that’s a great idea. It seems to work (Just need to role out to production yet).

For others who might want to do this as well, the revised code looks something like:

import { isProduction } from '@redwoodjs/api/logger'
import { createServer } from '@redwoodjs/api-server'
import { logger as parentLogger } from 'src/lib/logger'

const logger = parentLogger.child({})
logger.level = isProduction ? 'warn' : 'trace'

async function main() {
  const server = await createServer({
    logger,
  })

  await server.start()
}

main()
1 Like