Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]
like image 902
saurabh ujjainwal Avatar asked Aug 06 '18 07:08

saurabh ujjainwal


3 Answers

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" })
);
like image 133
kapil Avatar answered Nov 01 '22 23:11

kapil


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!

like image 23
itsHarshad Avatar answered Nov 02 '22 01:11

itsHarshad


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!

like image 6
Harshal Y. Avatar answered Nov 01 '22 23:11

Harshal Y.