winston-logzio
winston-logzio copied to clipboard
Invalid transport, must be an object with a log method
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
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');
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 I think you were using Winston@3 +, Make sure you install Winston@2 had the same issue
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
Having the same issue with Winston 3.0.0-rc5
and winston-logzio 3.0.3
(node 9.4.0)
@Fire-Brand Thank you, I'll take a look. Can you attach the debug logs?
@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 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?
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 I will run some tests and get back to you. According to their documentation this is how you should close the logger safely.
I had to use : winston.configure({transports: [new winston.transports.File({ filename: 'logfile.log' }) ]}); instead off: winston.add(winston.transports.File, { filename: 'logfile.log' });
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.
This works too:
winston.add(new winston.transports.File({ filename: 'logfile.log' }));
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
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!