3

I am configuring winston with Sequelize. I have the following:

const logger = winston.createLogger({
    level: 'info',
    format: winston.format.json(),
    transports: [
        new winston.transports.File({ filename: path.join('logs', 'error.log'), level: 'error' }),
        new winston.transports.File({ filename: path.join('logs', 'info.log'), level: 'info' }),
        new winston.transports.File({ filename: path.join('logs', 'combined.log') }),
    ],
});

const sequelize = new Sequelize(
    database.database,
    database.user,
    database.password,
    {
        host: database.host,
        dialect: 'mysql',
        logging: (msg) => logger.info(msg),
    }
);

However, the logs files show the message before level:

{"message":"Database connection has been established successfully.","level":"info"}

Besides, timestamp does not appear as shown here.

Any fix?

1 Answers1

4
const logger = winston.createLogger({
  level: 'info',
  format: winston.format.combine(winston.format.timestamp(), winston.format.json()),
  //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  transports: [
    new winston.transports.File({ filename: path.join('logs', 'error.log'), level: 'error', timestamp: true }),
    new winston.transports.File({ filename: path.join('logs', 'info.log'), level: 'info', timestamp: true }),
    new winston.transports.File({ filename: path.join('logs', 'combined.log'), timestamp: true }),
  ],
});

https://stackoverflow.com/a/48573091/11343720

Wang Liang
  • 4,244
  • 6
  • 22
  • 45
  • can you please check my winston related question. https://stackoverflow.com/questions/67385636/how-to-print-logs-from-specific-js-file-in-node-js-based-on-configuration-prese any help will be appreciated – S.R May 05 '21 at 11:27