Home > Software design >  I can't change the default `{ level: info }` in the Pino settings for Fastify
I can't change the default `{ level: info }` in the Pino settings for Fastify

Time:11-26

I can't change the default option - { level: info } in the Pino settings for Fastify.
The other options work as they should.

I have a project with the structure:

  • project/
    • node_modules/
    • plugins/
    • pino/
      • config/
        • config.js
        • dev.js
      • logs/
        • dev.log
    • routes/
      • home.js
    • app.js
    • package-lock.json
    • package.json

app.js

import fastify from 'fastify'
const { default: pino } = await import('./pino/config/config.js')

const app = fastify({ logger: pino.dev })
await app.register(import('./plugins/env/plugin.js'))
await app.register(import('./routes/home.js'))
await app.listen({ port: process.env.PORT || 5500 })

pino/config/config.js

const { default: dev } = await import('./dev.js')
// const { default: prod } = await import('./prod.js')
// const { default: test } = await import('./test.js')

export default {
  dev,
  // prod,
  // test,
}

pino/config/dev.js

const targets = [
  {
    target: 'pino-pretty',
    options: {
      name: 'dev-terminal',
      level: 'error', // It should be 'error', but it works as 'info' (default value)

      // setting pino-pretty
      colorize: true,
      levelFirst: true,
      include: 'level,time,',
      translateTime: 'yyyy-mm-dd HH:MM:ss Z',
    },
  },
  {
    target: 'pino/file',
    options: {
      name: 'dev-local-file',
      level: 'fatal', // It should be 'fatal', but it works as 'info' (default value)
      destination: './pino/logs/dev.log',
      mkdir: true,
    },
  },
]

export default {
  transport: {
    targets,
  },
}

Can I do without disableRequestLogging?

CodePudding user response:

The option are in the wrong order, here a working example:

const pino = require('pino')
console.log(pino.version)

const log = pino({
  level: 'debug', // main log level, must be lower than the transport level
  transport: {
    targets: [
      {
        target: 'pino-pretty',
        level: 'error',
        options: {
          name: 'dev-terminal',
          colorize: true,
          levelFirst: true,
          include: 'level,time,',
          translateTime: 'yyyy-mm-dd HH:MM:ss Z'
        }
      },
      {
        target: 'pino/file',
        level: 'fatal',
        options: {
          name: 'dev-local-file',
          destination: 'dev.log',
          mkdir: true
        }
      }
    ]
  }
})

log.debug('debug')
log.info('info')
log.error('error')
log.fatal('fatal')

  • Related