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.
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);
}
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)
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With