Search code examples
node.jsloggingtypeerrorwinston

Winston Logger: Path must be a string


I'm using Winston in my Node.js project for logging. I want to build several loggers with similar settings, so to be DRY I'm trying to build the transports with a function:

function makeTransport(dir, logLevel) {
  return new winston.transports.DailyRotateFile({
    filename: `${LOG_DIR}/${dir}/${logLevel}-%DATE%.log`,
    datePattern: 'YYYY-MM-DD-HH',
    level: logLevel,
    format: myFormat
  });
}

But when I run this, it throws an error

TypeError: Path must be a string. Received undefined

I've checked over all my syntax. The error is definitely something with the transport constructor. Is there a way I can do what I'm trying to do without copypasting logger definitions all over my code?


Solution

  • Update: I found a workaround that is working

    function makeTransport(dir, logLevel) {
      let def = {
        filename: `${LOG_DIR}/${dir}/${logLevel}-%DATE%.log`,
        datePattern: 'YYYY-MM-DD-HH',
        level: logLevel,
        format: myFormat
      };
      return new winston.transports.DailyRotateFile(def);
    }
    

    I'm still not sure why the original way doesn't work, but this is about the same amount of code.