1

I am trying to use winston to write logs to my logfiles. I followed the solution mentioned in : Winston logging object

But I am getting a weird issue.

When I use the above solution (in the link) by Pierre C , on my node emailer transporter like so :

transporter.sendMail(mailOptions, function(err, data) {
      if (err) {
       //not checked
      } else {
        logger.info(`Looks good`,{...data});
      }
    });

I works great as expected , but when I use the same format in a try catch block

try {
} catch(err) {
 logger.error(`Looks bad`,{...err});
}

My metadata block comes as empty , my logger.js looks like this

const winston = require ('winston');
const path = require('path')
//const{mainModule} = require('process')
const {combine ,timestamp ,json,label,printf,metadata,colorize,splat} = winston.format;
 
const logFormat = winston.format.printf(info => `${info.timestamp}:level: ${info.level} [${info.label}]: ${info.message}`)

//creating custom logs to feed data in log files
const emaillogger = winston.createLogger({
    levels:winston.config.syslog.levels,
   // level: process.env.LOG_LEVEL || 'info',

    format : combine(
        label({label: path.basename(require.main.filename)}),
        timestamp({format : 'MM-DD-YYYY hh:mm:ss A'}),
        // Format the metadata object
        metadata({ fillExcept: ['timestamp','message', 'level', 'label'] })),
    transports:[
        //new winston.transports.Console({format: combine(colorize())}),
        new winston.transports.File({filename:'logs/combined.log',format:combine(json())}),
        new winston.transports.File({filename:'logs/errorlogs.log',level : 'error',format:combine(json())})
    ],
    exitOnError: false
});

if (process.env.NODE_ENV !== 'production') {
    emaillogger.add(new winston.transports.Console({
      format: combine(colorize(),logFormat)
    }));
  }

module.exports = { emailLogger: emaillogger };
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • it seems there's a problem with winston logging Error objects, as their properties are not enumerable. https://github.com/winstonjs/winston/issues/1243 – Juan Perez Jul 12 '23 at 19:46

0 Answers0