10
votes

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]
5

5 Answers

38
votes

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!

6
votes

Updated Answer:

You need to add mongo transport in winston initialization.

Try this code:

const logger = winston.createLogger({
    transports: [
        new winston.transports.MongoDB({
            db: 'mongodb://localhost:27017/test',
            collection: 'log',
            level: 'info',
            storeHost: true,
            capped: true,
        })
    ]
});

Check the log collection in test DB.

Make sure you have:

logger.info("Test log!")

Hope this solves your query!

3
votes

I had the similar problem so what I did is alter the package.json with the line from "winston": "^3.0.0" to "winston": "2.*" then entered this command npm install -save winston in the command line

2
votes

This is for that latest version as of now.

"mongoose": "^5.11.10", "winston": "^3.3.3", "winston-mongodb": "^5.0.5"

I faced the same issue. This is what fixed my issue

winston.add(
  new winston.transports.File({ filename: "logfile.log", level: "error" })
);
winston.add(
  new winston.transports.MongoDB({ db: "mongodb://localhost/vidly" })
);
0
votes

winston.configure({transports: [new winston.transports.File({ filename: 'logfile.log' }) ]});

This worked for me. Hope it helps for you as well.