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?
You aren't exporting the logger properly in modules.js
.
exports.logger = require('./logger.js').logger;
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});
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With