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

Created on 1 Mar 2019  路  20Comments  路  Source: balderdashy/sails

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

needs documentation resolved

Most helpful comment

Created PR that should fix this. https://github.com/balderdashy/captains-log/pull/25

All 20 comments

@wawanopoulos Thanks for posting, we'll take a look as soon as possible.


For help with questions about Sails, click here. If you’re interested in hiring @sailsbot and her minions in Austin, click here.

@wawanopoulos Thanks for taking the time to provide details and example code for this.
What previous version of Winston Log library was working for you with Sails?
Was it working with the same version of Sails above (v1.1.0) ?
This will further help the community understand what may be causing the incompatibility.

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

Created PR that should fix this. https://github.com/balderdashy/captains-log/pull/25

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

@luislobo thank you for your time in contributing! It looks like that pull request is still waiting for some changes requested by Mike. @wawanopoulos it looks like a patch is for this issue is on the horizon! thanks @Themandunord for offering a quick work around for any others experiencing this issue.

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

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

Thanks @luislobo - will get this to Mike.

@wawanopoulos Hi! It sounds like the example in our docs is not compatible with the newest version of Winston. Is that right?

Now that https://github.com/balderdashy/captains-log/pull/25 has been merged can this be fixed? I'm keen to get the fixes 馃槃

@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'

};

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

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

I upgraded to [email protected] and [email protected] and can use a winston customLogger. Great job everyone 馃憤

I am using [email protected] and [email protected] and config/log.js as https://github.com/balderdashy/sails/issues/4601#issuecomment-529557170,but still getting TypeError: self._addDefaultMeta is not a function. Can anyone please help to resolve the issue.
@kierans @johnabrams7 @luislobo

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

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.

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

Was this page helpful?
0 / 5 - 0 ratings