Winston: Cannot create property 'Symbol(level) for logger.log (sample from Readme)

Created on 8 Dec 2017  路  5Comments  路  Source: winstonjs/winston

I've set up a logger from the example given in README.md:

import winston from 'winston';

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  transports: [
    // - Write to all logs with level `info` and below to `combined.log`
    // - Write all logs error (and below) to `error.log`.
    //
    new winston.transports.File({ filename: 'error.log', level: 'error' }),
    new winston.transports.File({ filename: 'combined.log' })
  ]
});

// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
//
if (process.env.NODE_ENV !== 'production') {
  logger.add(
    new winston.transports.Console({
      format: winston.format.simple()
    })
  );
}

export default logger;

I've got some trouble with it:

  • logger.info() appends an empty {} to simple strings:

    logger.info(`Server listening on port ${config('port')}`) will result in

    info: Server listening on port 3000 {}

  • logger.log() doesn't exist at all, it seems:

    level[LEVEL] = level.level;
    ^
    TypeError: Cannot create property 'Symbol(level)' on string 'Server listening on port 3000'

Most helpful comment

Leaving this here for others who come to this thread. The _winstonlogger_ object _does_ have a .log() method. However, unlike console.log(), it requires two arguments, not just one. If you forget the first argument, the log message category, you will get the Symbol(level) error.

For example, this gets the error:

winstonlogger.log('The transaction failed.');

This does _not_:

winstonlogger.log('error', 'The transaction failed.');

This is why this happens to many of us when we convert from console logging to winstonlogger logging. We forget the first argument to the call.

All 5 comments

@doque I just got same error TypeError: Cannot create property 'Symbol(level)' on string, did you find a solution? Thanks!

Ok, the problem is that there is no log(), should be debug() or something else.
I guess that reason is that I switched from console to winston.
It was console.log(), just replace console to logger will trigger this error.

Leaving this here for others who come to this thread. The _winstonlogger_ object _does_ have a .log() method. However, unlike console.log(), it requires two arguments, not just one. If you forget the first argument, the log message category, you will get the Symbol(level) error.

For example, this gets the error:

winstonlogger.log('The transaction failed.');

This does _not_:

winstonlogger.log('error', 'The transaction failed.');

This is why this happens to many of us when we convert from console logging to winstonlogger logging. We forget the first argument to the call.

Seems like a .log method should be implemented as it's pretty trivial that it'll exist, for a logger.

I used the following workaround for this problem:

'use strict';
var winston = require('winston');

module.exports = function () {

const customLogger = winston.createLogger({
    format: winston.format.json(),
    transports: [
        new winston.transports.File({
            filename: 'logs/error.log',
            level: 'error'
        }),
        new winston.transports.File({
            filename: 'logs/warn.log',
            level: 'warn'
        }),
        new winston.transports.File({
            filename: 'logs/info.log',
            level: 'info'
        }),
        new winston.transports.File({
            filename: 'logs/debug.log',
            level: 'debug'
        })
    ]
});

//extending log method of logger to suppport single argument in log function.
function log() {
    if (arguments.length > 1) {
        customLogger.log(...arguments);
    } else
        customLogger.info(arguments[0]);
}

return {
    ...customLogger,
    log
}

}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Buzut picture Buzut  路  3Comments

Nepoxx picture Nepoxx  路  4Comments

kjin picture kjin  路  3Comments

pocesar picture pocesar  路  3Comments

bertolo1988 picture bertolo1988  路  3Comments