简体   繁体   中英

How to set up Winston loggin with Sequelize properly?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM