starleaf1
starleaf1

Reputation: 2872

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?

Upvotes: 14

Views: 18638

Answers (2)

silentsudo
silentsudo

Reputation: 6973

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});
...


Upvotes: 4

Mukesh Sharma
Mukesh Sharma

Reputation: 9022

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

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

Upvotes: 16

Related Questions