Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I plug into socket.io's built in logging system to generate my own messages?

socket.io seems to have a basically sensible logging system for all its internals. How do I get at that logging object myself so I can generate my own log messages at appropriate levels? It bugs me that my console.log() messages are un-timestamped, un-leveled, and ugly next to the socket.io messages. I've done a bunch of spelunking in the socket.io code and I'm just not savvy enough about node at this point to understand what the object hierarchies look like to know how to get at the object I want from my code.

Longer term, I'm probably going to want a more robust logging system module (with the ability to log to files, auto-rotate, manage levels on a per-module basis, custom log levels, etc.). Winston looks sensible, but can I get socket.io to use it, too? It'd be nice to have everything in one place.

like image 973
drewww Avatar asked Oct 01 '11 16:10

drewww


2 Answers

While using socket.io, I was able to plug into the existing logger module like so:

var express = require('express'),
    app     = module.exports = express.createServer(), //just creating 'app' for io
    io      = require('socket.io').listen(app),
    logger  = io.log, // access the existing logger setup in socket.io
    util    = require('util');

logger.info(util.format("Express server listening on port %d in %s mode", 8003, app.settings.env));

Configuring the logger is also very simple:

io.configure('production', function(){
  io.set('log level', 1);
}
like image 106
Kato Avatar answered Oct 21 '22 14:10

Kato


Have you looked into using the logger middleware from Connect? It looks like someone already created a library for what you want called socket.IO-connect. I use something similar in my Express program:

var connect = require('connect');

module.exports = connect.createServer(
    connect.logger({ format: ':response-time :method :url' }),
    connect.static(__dirname + '/public)
);
like image 23
EhevuTov Avatar answered Oct 21 '22 13:10

EhevuTov