I want to store my error logs in mongoDB collection. I am using winston & winston -mongoDB.
Getting the error:
throw new Error('Invalid transport, must be an object with a log method.'); Error: Invalid transport, must be an object with a log method.
Here is the code in logger file. Here is my code: import appRoot from 'app-root-path'; import { createLogger, transports, format, } from 'winston';
import * as winston from 'winston';
require('winston-mongodb');
const options = {
fileInfo: {
level: 'info',
filename: `${appRoot}/logs/info.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
timestamp: true,
},
mongoDB: {
db: 'mongodb://127.0.0.1:27017/test',
collection: 'log',
level: 'info',
storeHost: true,
capped: true,
},
};
winston.add(winston.transports.MongoDB, options.mongoDB);
const logger = createLogger({
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss',
}),
format.json()
),
transports: [
new transports.File(options.fileInfo)
],
});
logger.stream = {
write: (message, encoding) => {
logger.info(message);
},
};
export default logger;
Versions:
"mongoose": "^5.2.6",
"morgan": "^1.9.0",
"winston": "^3.0.0",
"winston-mongodb": "^4.0.3",
[email protected]
I had the same issue, what I did was to replace this statement:
winston.add(winston.transports.File, { filename: 'logfile.log' });
to this:
winston.add(new winston.transports.File({ filename: 'logfile.log' }));
This happens in the latest major update of winston i.e 3.x.x and above.
Hope this helps!