Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 716
Golo Roden Avatar asked Aug 09 '13 07:08

Golo Roden


People also ask

How do I set up a Winston logger?

Here's a guide that will help you understand NPM in detail. Install the Winston package using the command npm install winston . Logging with Winston is simple, with just four steps, as shown in the example below, and you have your log recorded. Add the Winston module with a require() function.

How do I connect to a Socket.IO server?

listen(port); // Create a Socket.IO instance, passing it our server var socket = io. listen(server); // Add a connect listener socket. on('connection', function(client){ console. log('Connection to client established'); // Success!


1 Answers

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.

like image 118
Brad Avatar answered Sep 20 '22 14:09

Brad