1

In pre 1.0 versions of Socket.io it seems you could set the logger with the logger option upon instantiation, but i cant seem to find any possibilities to do it after 1.0. Am i right they have removed this option, and are there any good ways i can bypass this by hooking up/listening to errors from the debug-module that Socket.io seems to use now, without modifying the existing module. I want to log my socket messages with Bunyan.

hbentlov
  • 544
  • 2
  • 12

2 Answers2

1

To log with new socket.io, you can use the following :

DEBUG=* // all
DEBUG=socket.io:* // all in socket.io module
DEBUG=socket.io:server // only server debugger
cvng
  • 1,822
  • 17
  • 23
  • I think the OP wants to be able to direct his logs via a logger that allows stream configuration. – Guy Oct 22 '14 at 23:39
0

After some research I've created a simple wrapper:

var wrapper = function (http, opts) {
    var logCB = function (str) {
        fs.writeFileSync("a.txt", "SOCKET.IO " + str + "\n", {flag: 'a'});
    };
    var io = require('socket.io')(http, opts);
    var ret = {
        'use': function (f) {
            io.use(f);
        },
        'sockets': {
            'on': function (evt, fn) {
                logCB("ON " + evt);
                io.sockets.on(evt, function (socket) {
                    var newSocket = Object.create(socket);
                    newSocket.on = function (ev, cb) {
                        socket.on(ev, function (data, ok) {
                            logCB("ON " + ev + " DATA " + JSON.stringify(data));
                            cb(data, ok);
                        });
                    };
                    newSocket.emit = function (ev, data) {
                        logCB("EMIT " + ev + " DATA " + JSON.stringify(data));
                        socket.emit(ev, data);
                    };
                    fn(newSocket);
                });
            }
        }
    };
    return ret;
};

use it instead of traditional request:

httpServer.listen(80, ip);
var io = wrapper(httpServer, sockOpts);

please improve it, if you wish.

Stepan Yakovenko
  • 8,670
  • 28
  • 113
  • 206