winston: Attempt to write logs with no transports - using default logger

ekntrtmz picture ekntrtmz · Aug 25, 2018 · Viewed 15.8k times · Source

I followed a tutorial to set up winston (2.x) default logger in my express app. When updating to the current version of winston (3.0.0) I have a problem with adding the transports. I have followed the latest docs but still I get the notice in console and no log files are created at all:

[winston] Attempt to write logs with no transports

logging.js

const winston = require('winston');

module.exports = function () {

  const files = new winston.transports.File({ filename: 'logfile.log' });
  const myconsole = new winston.transports.Console();

  winston.add(myconsole);
  winston.add(files);

}

index.js

const winston = require('winston');
...

require('./logging');
winston.info("Give some info");

[winston] Attempt to write logs with no transports {"message":"Give some info","level":"info"}

What am I doing wrong?

Answer

davewy picture davewy · Aug 31, 2018

In winston 3 you need to create a logger object, then add transports to it.

Winston 3 has many examples, but to adapt from the readme, do something like this:

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({ filename: 'logfile.log' })
  ]
});

logger.info('it works!!');