winston-logzio icon indicating copy to clipboard operation
winston-logzio copied to clipboard

Invalid transport, must be an object with a log method

Open Fl4m3Ph03n1x opened this issue 6 years ago • 15 comments

I have the following code snippet:

const winston = require("winston"); const logzioWinstonTransport = require("winston-logzio");

const winstonFactory = ({ API_TOKEN }) => {
    
    const loggerOptions = {
        token:  API_TOKEN,
        host:   "listener.logz.io",
        type:   "nodejs"
    };
    
    winston.add( logzioWinstonTransport, loggerOptions );

    return winston;
};

module.exports = winstonFactory;

Upon executing winstonFactory(), I get the following error:

Error: Invalid transport, must be an object with a log method. at new LegacyTransportStream (/home/ubuntu/workspace/node_modules/winston-transport/legacy.js:17:11) at DerivedLogger.add (/home/ubuntu/workspace/node_modules/winston/lib/winston/logger.js:277:7) at Object.winston.(anonymous function) [as add] (/home/ubuntu/workspace/node_modules/winston/lib/winston.js:84:34) at winstonFactory (/home/ubuntu/workspace/src/deps/winston.js:12:13) at setupDeps (/home/ubuntu/workspace/src/deps/index.js:4:15) at setupApp (/home/ubuntu/workspace/src/app.js:5:24) at Object. (/home/ubuntu/workspace/server.js:2:13) at Module._compile (module.js:649:30) at Object.Module._extensions..js (module.js:660:10) at Module.load (module.js:561:32)

You can also reproduce the said error by dong this simple tutorial:

https://codeburst.io/setting-up-a-nodejs-monitoring-and-error-alert-system-in-a-jiffy-5241a4ef0561

Am I missing somehting or doing something wrong? Why isn't it working ?


versions:

  "dependencies": {
    "winston": "^3.0.0-rc2",
    "winston-logzio": "^1.0.6"
  }

NODE: 9.8.0

Fl4m3Ph03n1x avatar Mar 13 '18 11:03 Fl4m3Ph03n1x

EDIT: the reason is due to the Winston version. we support v2 of winston. we haven't looked at v3 yet.

Using V2

This snippet of code works for me.

const winston = require("winston");
const logzioWinstonTransport = require("winston-logzio");

const winstonFactory = ({ API_TOKEN }) => {

    const loggerOptions = {
        token:  API_TOKEN,
        host:   "listener.logz.io",
        type:   "nodejs"
    };

    winston.add( logzioWinstonTransport, loggerOptions );

    return winston;
};

var winstonLogger = winstonFactory({API_TOKEN: 'asd'});
winstonLogger.log('wow')

because you haven't shared the full code, im guessing you are trying to override the Winston instance, instead, try to create a new one

const winstonFactory = ({ API_TOKEN }) => {

    const winston = require("winston");
    const logzioWinstonTransport = require("winston-logzio");

    const loggerOptions = {
        token:  API_TOKEN,
        host:   "listener.logz.io",
        type:   "nodejs"
    };

    winston.add( logzioWinstonTransport, loggerOptions );

    return winston;
};

const winstonLogger = winstonFactory({API_TOKEN: 'asd'});
winstonLogger.log('wow');

AlonMiz avatar Mar 15 '18 08:03 AlonMiz

None of the versions work for me and I couldn't say why.

Regardless after trying a couple new examples I am not able to communicate with logzio so I think it's safe to say that the issue was in my code and not with the library.

Closing this ticket and thanks for contesting!

Fl4m3Ph03n1x avatar Mar 20 '18 13:03 Fl4m3Ph03n1x

@Fl4m3Ph03n1x I think you were using Winston@3 +, Make sure you install Winston@2 had the same issue

veken1199 avatar Sep 23 '18 19:09 veken1199

Downgrading to an older version of winston is not a good solution IMO

I believe the solution is related to the number of arguments the "log" function accepts (see this comment in another transport repo)

Would a PR for this be accepted?

EDIT: apologies just saw winston 3.x ticket

lokimckay avatar Oct 16 '18 06:10 lokimckay

Having the same issue with Winston 3.0.0-rc5 and winston-logzio 3.0.3 (node 9.4.0)

Fire-Brand avatar Dec 09 '18 13:12 Fire-Brand

@Fire-Brand Thank you, I'll take a look. Can you attach the debug logs?

idohalevi avatar Dec 10 '18 07:12 idohalevi

@idohalevi From what i've tried, it seems that if i use the example in the README.MD and simply execute that file it does work.

When i tried creating my own module with winston (for custom logging ) and winston-logzio, i've encountered no errors, but logs were not being sent over(couldn't find them on the dashboard).

After playing around a bit it seemed that while my Console transporter was printing the logs in the terminal, the logzioWinstonTransport wasn't being activated, it seems that issue was that my process was exiting too fast for the logs to be sent... After i gave it a small timeout prior to exiting the process, it did got sent an i was able to see my logs.

Fire-Brand avatar Dec 10 '18 10:12 Fire-Brand

@Fire-Brand Thanks for the help. We did implement a finish() function that supposed to be called by the logging framework. I will check why the function wasn't called at the end of your program. Can you try closing Winston logger at the end of the run?

idohalevi avatar Dec 10 '18 11:12 idohalevi

Just to clarify, i was using process.exit(0); for the purpose of that test, so maybe that's reason why finish() wasn't called?

Fire-Brand avatar Dec 10 '18 13:12 Fire-Brand

@Fire-Brand I will run some tests and get back to you. According to their documentation this is how you should close the logger safely.

idohalevi avatar Dec 10 '18 16:12 idohalevi

I had to use : winston.configure({transports: [new winston.transports.File({ filename: 'logfile.log' }) ]}); instead off: winston.add(winston.transports.File, { filename: 'logfile.log' });

CriptoGirl avatar Feb 28 '19 16:02 CriptoGirl

I had to use : winston.configure({transports: [new winston.transports.File({ filename: 'logfile.log' }) ]}); instead off: winston.add(winston.transports.File, { filename: 'logfile.log' });

This worked for me. Thanks.

Katuka avatar Jul 27 '19 18:07 Katuka

This works too:

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

galaczi avatar Jul 31 '19 09:07 galaczi

I had to use : winston.configure({transports: [new winston.transports.File({ filename: 'logfile.log' }) ]}); instead off: winston.add(winston.transports.File, { filename: 'logfile.log' });

this worked

MrCartaaa avatar Mar 23 '20 19:03 MrCartaaa

I had to use : winston.configure({transports: [new winston.transports.File({ filename: 'logfile.log' }) ]}); instead off: winston.add(winston.transports.File, { filename: 'logfile.log' });

worked for me, thanks!

arunsingh49 avatar May 27 '20 07:05 arunsingh49