added socket.io logging which can be dynamically enabled/disabled via the /admin/logger panel

v1.18.x
Andrew Darqui 11 years ago
parent 55d84d0f9b
commit 9007f9de9e

@ -17,7 +17,22 @@
<br/>
<label>Path to log file</label>
<input class="form-control" type="text" placeholder="/path/to/log/file.log" data-field="loggerPath" /><br />
<input class="form-control" type="text" placeholder="/path/to/log/file.log" data-field="loggerPath" />
<br />
<br/>
<br/>
<label>
<input type="checkbox" data-field="loggerIOStatus"> <strong>Enable socket.io logging</strong>
</label>
<br/>
<label>Path to socket.io log file</label>
<input class="form-control" type="text" placeholder="/path/to/log/socket.io.file.log" data-field="loggerIOPath" />
<br/>
<br/>
</form>
</div>

@ -81,11 +81,17 @@ var express = require('express'),
app.use(express.compress());
if(nconf.get("express:logger") == true || meta.config.loggerStatus > 0) {
/*
* HTTP request logging is enabled
*/
var loggerObj = {};
if(meta.config.loggerPath) {
loggerObj.stream = fs.createWriteStream(meta.config.loggerPath, {flags: 'a'});
meta.config.loggerStream = loggerObj.stream
}
else {
loggerObj.stream = process.stdout;
}
meta.config.loggerStream = loggerObj.stream;
app.use(express.logger(loggerObj));
}

@ -16,6 +16,8 @@ var cookie = require('cookie'),
async = require('async'),
RedisStoreLib = require('connect-redis')(express),
RDB = require('./redis'),
util = require('util'),
fs = require('fs')
RedisStore = new RedisStoreLib({
client: RDB,
ttl: 60 * 60 * 24 * 14
@ -54,6 +56,26 @@ module.exports.init = function(io) {
var hs = socket.handshake,
sessionID, uid;
/*
(function() {
var emit = socket.emit;
socket.emit = function() {
console.log('***','emit', Array.prototype.slice.call(arguments));
emit.apply(socket, arguments);
};
var $emit = socket.$emit;
socket.$emit = function() {
console.log('***','on',Array.prototype.slice.call(arguments));
$emit.apply(socket, arguments);
};
})();
*/
// Validate the session, if present
socketCookieParser(hs, {}, function(err) {
sessionID = socket.handshake.signedCookies["express.sid"];
@ -64,6 +86,49 @@ module.exports.init = function(io) {
userSockets[uid] = userSockets[uid] || [];
userSockets[uid].push(socket);
if(meta.config.loggerIOStatus > 0) {
var _prepare_loggerIO_string = function(_type, _uid, _args) {
try {
return 'io: '+_uid+' '+_type+' '+util.inspect(Array.prototype.slice.call(_args))+'\n';
} catch(err) {
winston.err(err)
}
}
/*
* socket.io emit/on logging is enabled
*/
if(!meta.config.loggerIOStream) {
var loggerObj = {};
if(meta.config.loggerIOPath) {
loggerObj.stream = fs.createWriteStream(meta.config.loggerIOPath, {flags: 'a'});
}
else {
loggerObj.stream = process.stdout;
}
meta.config.loggerIOStream = loggerObj.stream;
}
(function() {
// courtesy of: http://stackoverflow.com/a/9674248
var user = uid
if(!user) user = "?"
var emit = socket.emit;
socket.emit = function() {
meta.config.loggerIOStream.write(_prepare_loggerIO_string("emit",uid,arguments));
emit.apply(socket, arguments);
};
var $emit = socket.$emit;
socket.$emit = function() {
meta.config.loggerIOStream.write(_prepare_loggerIO_string("on",uid,arguments));
$emit.apply(socket, arguments);
};
})();
}
if (uid) {
RDB.zadd('users:online', Date.now(), uid, function(err, data) {

Loading…
Cancel
Save