5

I am trying to use pino for logging in to my node app Server and I do have some large logs coming, so rotating the files every day would be more practical.

So I used pino.multistream() and require('file-stream-rotator')

My code works, but for performance reasons, I would not like to use the streams in the main thread.

according to the doc, I should use pino.transport():

[pino.multistream()] differs from pino.transport() as all the streams will be executed within the main thread, i.e. the one that created the pino instance. https://github.com/pinojs/pino/releases?page=2

However, I can't manage to combine pino.transport() and file-stream-rotator.

my code that does not work completely -> logs the first entries, but is not exportable because it blocks the script with the error

throw new Error('the worker has exited')

Main file

const pino = require('pino')

const transport = pino.transport({
  target: './custom-transport.js'
})

const logger = pino(transport)
logger.level = 'info'

logger.info('Pino: Start Service Logging...')

module.exports = {
  logger
}

custom-transport.js file

const { once } = require('events')
const fileStreamRotator = require('file-stream-rotator')

const customTransport = async () => {
  const stream = fileStreamRotator.getStream({ filename: 'myfolder/custom-logger.log', frequency: 'daily' })
  await once(stream, 'open')
  return stream
}

module.exports = customTransport
Noweh
  • 583
  • 5
  • 16

0 Answers0