Winston logging object

前端 未结 5 2110
轻奢々
轻奢々 2021-02-13 12:23

I use Winston for my backend logging I cannot log the object without using JSON.stringify which is annoying

logger.debug(`Register ${JSON.stringify(         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-13 13:10

    My solution was to use this kind of formatter:

    const { format } = winston
    const consoleFormat = format.combine(
      format.prettyPrint(),
      format.splat(),
      format.printf((info) => {
        if (typeof info.message === 'object') {
          info.message = JSON.stringify(info.message, null, 3)
        }
    
        return info.message
      })
    )
    

    now all those options works as expected:

    logger.info('plain text')
    logger.info('plain text with object %o', { a:1, b: 2} )
    logger.info({ a:1, b: 2 })
    

提交回复
热议问题