I'm trying to set up an access log and an error log for my express server using Winston, but I seem to be doing something wrong.
Here is my attempt at a config file:
const winston = require('winston'),
fs = require('fs');
const tsFormat = () => (new Date()).toLocaleTimeString();
winston.loggers.add('errorLog', {
file: {
filename: '<path>/errors.log', //<path> is replaced by the
timestamp: tsFormat, //absolute path of the log
level: 'info'
}
});
winston.loggers.add('accessLog', {
file: {
filename: '<path>/access.log', //same as before
timestamp: tsFormat,
level: 'info'
}
});
And this is how I'm including it in my other files:
var winston = require('winston'),
accessLog = winston.loggers.get('accessLog'),
errorLog = winston.loggers.get('errorLog');
This seems to me like it follows the documentation (https://github.com/winstonjs/winston/tree/2.4.0#working-with-multiple-loggers-in-winston) but I'm getting this error when I try to log to it:
[winston] Attempt to write logs with no transports {"message":"pls","level":"info"}
[winston] Attempt to write logs with no transports {"message":"Bad request: undefined","level":"warn"}
Any help would be greatly appreciated, I've been pretty stumped for a couple days now.
1 Logger + console logging for development purpose:
logger.js
app.js
I'd try something like this, put all the logger related stuff into a module logger.js:
logger.js
and then test in index.js:
index.js
You should see log lines like:
errors.log:
access.log:
EDIT
Since you were creating the
winston
variable in the original code asconst winston
it wasn't able to add transports and other options in the following lines. The proposed solution moves the similar code into a module but assignswinston
variable intovar winston
.