Golo Roden Golo Roden - 1 year ago 114
Node.js Question

How do I log Socket.io through Winston?

I would like to use Winston as logger for Socket.io. I have seen this issue where it says:

var io = require('socket.io').listen(8080);
io.set('logger', { debug: <log function>, info: … , error: .., warn: .. })

Unfortunately, it is not described what the
log function
should look like.

Some playing around and a look into the Socket.io logger documentation told me that there is no fixed set of parameters: There are log messages with one, two and three parameters. Perhaps there are even more, I don't know.

I think that this is definitely not a good practice to have an undefined number of parameters, especially if this is your interface to external components.

Anyway ... does anybody have some experience with this? Can anyone point out what to watch out for?

Answer Source

This seems to work fine for me

var io = require('socket.io').listen(server, {
    logger: {
        debug: winston.debug, 
        info: winston.info, 
        error: winston.error, 
        warn: winston.warn

As a bonus, by setting the logger in the same call as .listen(), you catch all the log output from Socket.IO. Note that you should be able to just pass winston instead of that object, but it isn't working for me, so that's why I've posted this solution instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download