sails: Winston logger incompatibilities: 'self._addDefaultMeta is not a function'

Sails version: 1.1.0 Node version: v11.4.0 NPM version: 6.8.0 DB adapter name: sails-mongo DB adapter version: 1.0.1 Operating system: MacOSX


I currently experiencing some problem with integration of Winston Log library with Sails JS. It seems that since winston 3.2.1 has been released, it is no more possible to use a Winston Logger in a sailsJS application.

log.js:

var winston = require('winston');
require('winston-daily-rotate-file');

// Creating a custom logger based on Winston library
const customLogger = winston.createLogger({
    level: 'info',
    format: winston.format.simple(),
    transports: [
        new winston.transports.DailyRotateFile({
            level: 'silly',
            dirname: 'storage/logs',
            filename: 'ctc-%DATE%.log',
            datePattern: 'YYYY-MM-DD',
            zippedArchive: true,
            maxSize: '20m',
            maxFiles: '14d'
        })
    ]
});

// If we're not in production then log to the `console` too.
if (process.env.NODE_ENV !== 'production') {
    customLogger.add(new winston.transports.Console({
        format: winston.format.simple(),
        level: 'silly'
    }));
}

module.exports.log = {
  // Pass in our custom logger, and pass all log levels through.
  custom: customLogger,
  // Disable captain's log so it doesn't prefix or stringify our meta data.
  inspect: false
};

Error that I get on sails lift:

/Users/test/Documents/repositories/aze/node_modules/winston/lib/winston/create-logger.js:80
        self._addDefaultMeta(info);
             ^

TypeError: self._addDefaultMeta is not a function
    at Function.DerivedLogger.(anonymous function) (/Users/test/Documents/repositories/aze/node_modules/winston/lib/winston/create-logger.js:80:14)
    at Function._writeLogToConsole [as info] (/Users/test/Documents/repositories/aze/node_modules/captains-log/lib/write.js:47:20)
    at async.auto._buildOntology (/Users/test/Documents/repositories/aze/node_modules/sails-hook-orm/lib/initialize.js:445:19)

Reference By : https://github.com/winstonjs/winston/issues/1577#issuecomment-458963380

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 20 (13 by maintainers)

Most upvoted comments

A sample project on how I use Winston Logger is here: https://github.com/luislobo/sails-winston-logger-example

Until 3.2.0 everything was working fine. Seems to be an incompatibility to use custom logger un sails since Winston version 3.2.1

@johnabrams7 To your question about the docs not working anymore with the latest winston version: yes, that example no longer works.

One configuration that I find appealing and nice, is the one I included in the example project mentioned above, that has /config/log.js as:

// config/log.js

const { version } = require('../package');

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, colorize, label, printf, align } = format;
const { SPLAT } = require('triple-beam');
const { isObject } = require('lodash');

function formatObject(param) {
  if (isObject(param)) {
    return JSON.stringify(param);
  }
  return param;
}

// Ignore log messages if they have { private: true }
const all = format((info) => {
  const splat = info[SPLAT] || [];
  const message = formatObject(info.message);
  const rest = splat.map(formatObject).join(' ');
  info.message = `${message} ${rest}`;
  return info;
});

const customLogger = createLogger({
  format: combine(
    all(),
    label({ label: version }),
    timestamp(),
    colorize(),
    align(),
    printf(info => `${info.timestamp} [${info.label}] ${info.level}: ${formatObject(info.message)}`)
  ),
  transports: [new transports.Console()]
});

module.exports.log = {
  custom: customLogger,
  inspect: false

  /***************************************************************************
   *                                                                          *
   * Valid `level` configs: i.e. the minimum log level to capture with        *
   * sails.log.*()                                                            *
   *                                                                          *
   * The order of precedence for log levels from lowest to highest is:        *
   * silly, verbose, info, debug, warn, error                                 *
   *                                                                          *
   * You may also set the level to "silent" to suppress all logs.             *
   *                                                                          *
   ***************************************************************************/

  // level: 'info'

};

With the version 3.2.0 I have the error too. I need to go into the version 3.1.0 to avoid this error.

I have sails 1.1.0

@johnabrams7 I’ve updated the PR for the docs too.

Thanks @johnabrams7 , doing rm -rf node_modules && npm install didn’t solve the problem but upgrading the captains-log explicitly to v2.0.3 solved the problem.

@abilash1104 Have you tried rebuilding the node_modules folder in sails@1.2.3 with: rm -rf node_modules && npm install to verify the latest captains-log 2.0.3 is being used?

Thanks @luislobo ! Everyone can apply this update by rebuilding their node_modules folder with: rm -rf node_modules && npm install (run in the project dir)

@kierans @Themandunord @wawanopoulos

@rachaelshaw @johnabrams7 a new version of captains-log should be published in npm for this to be fully fixed

@raqem I’ve just submitted another commit in that PR based on @mikermcneil comments. Should be good to go