Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winston logger.info is not a function

I've set up Winston with transports to MySQL and console and put it in a module called logger. Like so...

// modules/logger.js

/* require statements */

exports.logger = new (winston.Logger)({
    transports: [
        new winstonMysql(winstonMysqlConfig),
        new (winston.transports.Console)
    ]
});

And then in /modules

// modules/index.js

/* grab other modules */

exports.logger = require('./logger.js');

When I do console.log(modules.logger), I get this

{ logger: 
    EventEmitter {
        ...
        error: [Function],
        warn: [Function],
        info: [Function],
        verbose: [Function],
        debug: [Function],
        silly: [Function],
        ...
    }
 }

But when I call modules.logger.info() it throws modules.logger.info is not a function error. What's wrong?

like image 352
starleaf1 Avatar asked Apr 03 '17 03:04

starleaf1


2 Answers

You aren't exporting the logger properly in modules.js.

exports.logger = require('./logger.js').logger;
like image 145
Mukesh Sharma Avatar answered Nov 06 '22 18:11

Mukesh Sharma


I have also faced similar issues, but my directory structure is somewhat different I have a logger file and import are a little different here is how I have fixed using the above reference

const winston = require('winston');

const logConfiguration = {
    format: winston.format.json(),
    'transports': [
        new winston.transports.Console(),
        new winston.transports.File({
            filename: 'sample.log'
        })
    ]
};


const logger = winston.createLogger(logConfiguration);

exports.logger = logger;

Import

...

const logger = require('../logger/app-logger').logger
logger.info({type: 'login', user: user.name});
...


like image 28
silentsudo Avatar answered Nov 06 '22 16:11

silentsudo