'Invalid transport, must be an object with a log method winston mongodb logging

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]


Solution 1:[1]

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!

Solution 2:[2]

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!

Solution 3:[3]

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

Solution 4:[4]

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

Solution 5:[5]

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

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

Solution 6:[6]

I am also learning nodeJS and this winston logging issue happened. so the solution proposed before was reconfirmed and worked well.

The changes I did was from

winston.add(
    winston.transports.File,
    { filename: "logfile.log" }
);

To:

winston.add(new winston.transports.File({ filename: 'logfile.log' }));

My packages versions are as follow:

{
    "@hapi/joi": "^17.1.1",
    "bcrypt": "^5.0.1",
    "config": "^3.3.7",
    "express": "^4.17.3",
    "express-async-errors": "^3.1.1",
    "fawn": "^2.1.5",
    "font": "^0.0.4",
    "joi-objectid": "^4.0.2",
    "jsonwebtoken": "^8.5.1",
    "lodash": "^4.17.21",
    "mongoose": "^6.2.8",
    "pug": "^3.0.2",
    "winston": "^3.6.0"
}

Solution 7:[7]

it worked for me if I replace :

<br>
   winston.add(azureApplicationInsightsLogger, {....<br>
with :<br>
   winston.add(new azureApplicationInsightsLogger(), {...<br>
and <br>
   winston.add(winston.transports.Console, {...<br>
with : <br>
   winston.add(new winston.transports.Console(), {...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 kapil
Solution 4 adiga
Solution 5 Oybek Toshmatov
Solution 6 Tyler2P
Solution 7 Lajos Arpad