logger jshint

v1.18.x
barisusakli 11 years ago
parent 37e8856861
commit 19c2a85bf1

@ -1,3 +1,5 @@
'use strict';
/* /*
* Logger module: ability to dynamically turn on/off logging for http requests & socket.io events * Logger module: ability to dynamically turn on/off logging for http requests & socket.io events
*/ */
@ -34,25 +36,22 @@ var opts = {
opts.express.app = app; opts.express.app = app;
/* Open log file stream & initialize express logging if meta.config.logger* variables are set */ /* Open log file stream & initialize express logging if meta.config.logger* variables are set */
Logger.setup(); Logger.setup();
} };
Logger.setup = function() { Logger.setup = function() {
Logger.setup_one('loggerPath', meta.config.loggerPath); Logger.setup_one('loggerPath', meta.config.loggerPath);
} };
Logger.setup_one = function(key,value) { Logger.setup_one = function(key, value) {
/* /*
* 1. Open the logger stream: stdout or file * 1. Open the logger stream: stdout or file
* 2. Re-initialize the express logger hijack * 2. Re-initialize the express logger hijack
*/ */
switch(key) { if (key === 'loggerPath') {
case 'loggerPath': { Logger.setup_one_log(value);
Logger.setup_one_log(value); Logger.express_open();
Logger.express_open();
}
default: return;
} }
} };
Logger.setup_one_log = function(value) { Logger.setup_one_log = function(value) {
/* /*
@ -61,20 +60,23 @@ var opts = {
*/ */
if(meta.config.loggerStatus > 0 || meta.config.loggerIOStatus) { if(meta.config.loggerStatus > 0 || meta.config.loggerIOStatus) {
var stream = Logger.open(value); var stream = Logger.open(value);
if(stream) opts.streams.log.f = stream; if(stream) {
else opts.streams.log.f = process.stdout; opts.streams.log.f = stream;
} else {
opts.streams.log.f = process.stdout;
}
} }
else { else {
Logger.close(opts.streams.log); Logger.close(opts.streams.log);
} }
} };
Logger.open = function(value) { Logger.open = function(value) {
/* Open the streams to log to: either a path or stdout */ /* Open the streams to log to: either a path or stdout */
var stream; var stream;
if(value) { if(value) {
if(fs.existsSync(value)) { if(fs.existsSync(value)) {
stats = fs.statSync(value); var stats = fs.statSync(value);
if(stats) { if(stats) {
if(stats.isDirectory()) { if(stats.isDirectory()) {
stream = fs.createWriteStream(path.join(value, 'nodebb.log'), {flags: 'a'}); stream = fs.createWriteStream(path.join(value, 'nodebb.log'), {flags: 'a'});
@ -96,12 +98,14 @@ var opts = {
stream = process.stdout; stream = process.stdout;
} }
return stream; return stream;
} };
Logger.close = function(stream) { Logger.close = function(stream) {
if(stream.f != process.stdout && stream.f != null) stream.end(); if(stream.f !== process.stdout && stream.f) {
stream.end();
}
stream.f = null; stream.f = null;
} };
Logger.monitorConfig = function(socket, data) { Logger.monitorConfig = function(socket, data) {
/* /*
@ -110,10 +114,10 @@ var opts = {
Logger.setup_one(data.key,data.value); Logger.setup_one(data.key,data.value);
Logger.io_close(socket); Logger.io_close(socket);
Logger.io(socket); Logger.io(socket);
} };
Logger.express_open = function() { Logger.express_open = function() {
if(opts.express.set != 1) { if(opts.express.set !== 1) {
opts.express.set = 1; opts.express.set = 1;
opts.express.app.use(Logger.expressLogger); opts.express.app.use(Logger.expressLogger);
} }
@ -121,7 +125,7 @@ var opts = {
* Always initialize "ofn" (original function) with the original logger function * Always initialize "ofn" (original function) with the original logger function
*/ */
opts.express.ofn = express.logger({stream : opts.streams.log.f}); opts.express.ofn = express.logger({stream : opts.streams.log.f});
} };
Logger.expressLogger = function(req,res,next) { Logger.expressLogger = function(req,res,next) {
/* /*
@ -134,7 +138,7 @@ var opts = {
} else { } else {
return next(); return next();
} }
} };
Logger.prepare_io_string = function(_type, _uid, _args) { Logger.prepare_io_string = function(_type, _uid, _args) {
/* /*
@ -145,82 +149,74 @@ var opts = {
try { try {
return 'io: '+_uid+' '+_type+' '+util.inspect(Array.prototype.slice.call(_args))+'\n'; return 'io: '+_uid+' '+_type+' '+util.inspect(Array.prototype.slice.call(_args))+'\n';
} catch(err) { } catch(err) {
winston.info("Logger.prepare_io_string: Failed",err) winston.info("Logger.prepare_io_string: Failed", err);
return "error"; return "error";
} }
} };
Logger.io_close = function(socket) { Logger.io_close = function(socket) {
/* /*
* Restore all hijacked sockets to their original emit/on functions * Restore all hijacked sockets to their original emit/on functions
*/ */
var clients = socket.io.sockets.clients(); var clients = socket.io.sockets.clients();
for(var v in clients) { clients.forEach(function(client) {
var client = clients[v]; if(client.oEmit && client.oEmit !== client.emit) {
if(client.oEmit != undefined && client.oEmit != client.emit) {
client.emit = client.oEmit; client.emit = client.oEmit;
} }
if(client.$oEmit != undefined && client.$oEmit != client.$emit) { if(client.$oEmit && client.$oEmit !== client.$emit) {
client.$emit = client.$oEmit; client.$emit = client.$oEmit;
} }
} });
} };
Logger.io = function(socket) { Logger.io = function(socket) {
/* /*
* Go through all of the currently established sockets & hook their .emit/.on * Go through all of the currently established sockets & hook their .emit/.on
*/ */
if(socket == undefined && socket.io.sockets == undefined) { if(!socket && !socket.io.sockets) {
return; return;
} }
var clients = socket.io.sockets.clients(); var clients = socket.io.sockets.clients();
for(var v in clients) {
var client = clients[v];
Logger.io_one(client,client.state.user.uid);
}
}
Logger.io_one = function(socket,uid) { clients.forEach(function(client) {
Logger.io_one(client, client.uid);
});
};
Logger.io_one = function(socket, uid) {
/* /*
* This function replaces a socket's .emit/.on functions in order to intercept events * This function replaces a socket's .emit/.on functions in order to intercept events
*/ */
if(socket != undefined && meta.config.loggerIOStatus > 0) { if(socket && meta.config.loggerIOStatus > 0) {
(function() { (function() {
function override(method, name, errorMsg) {
return function() {
if(opts.streams.log.f) {
opts.streams.log.f.write(Logger.prepare_io_string(name, uid, arguments));
}
try {
method.apply(socket, arguments);
} catch(err) {
winston.info(errorMsg, err);
}
};
}
// courtesy of: http://stackoverflow.com/a/9674248 // courtesy of: http://stackoverflow.com/a/9674248
var user = uid
if(!user) user = "?"
socket.oEmit = socket.emit; socket.oEmit = socket.emit;
var emit = socket.emit; var emit = socket.emit;
socket.emit = function() { socket.emit = override(emit, 'emit', 'Logger.io_one: emit.apply: Failed');
if(opts.streams.log.f != null) {
opts.streams.log.f.write(Logger.prepare_io_string("emit", uid, arguments));
}
try {
emit.apply(socket, arguments);
} catch(err) {
winston.info("Logger.io_one: emit.apply: Failed", err);
}
};
socket.$oEmit = socket.$emit; socket.$oEmit = socket.$emit;
var $emit = socket.$emit; var $emit = socket.$emit;
socket.$emit = function() { socket.$emit = override($emit, 'on', 'Logger.io_one: $emit.apply: Failed');
if(opts.streams.log.f != null) {
opts.streams.log.f.write(Logger.prepare_io_string("on",uid,arguments));
}
try {
$emit.apply(socket, arguments);
} catch(err) {
winston.info("Logger.io_one: $emit.apply: Failed", err);
}
};
})(); })();
} }
} };
}(exports)); }(exports));

Loading…
Cancel
Save