Permalink
Browse files

Adding options to fail silently and a log level

  • Loading branch information...
1 parent 9fc0291 commit 51eade281699c04e48935ef96983397664c8a09d @saikat committed Sep 2, 2010
Showing with 51 additions and 43 deletions.
  1. +3 −1 example/server.js
  2. +48 −42 lib/socket-logger.js
View
@@ -1,10 +1,12 @@
var http = require('http'),
-log = require('../lib/socket-logger'),
+log = require('../lib/socket-logger').defaultLogger,
io = require('./socket.io'),
server = http.createServer(function(req, res){
});
+log.logLevel = 1;
+
server.listen(8080);
log.authToken = 'my_secret_token_for_the_dashboard_client';
var socket = io.listen(server);
View
@@ -1,51 +1,57 @@
-function makeLogger(options) {
- var logger = {};
- logger.options = options || {};
- logger.socketLogger = null;
- logger.authToken = null;
- // This whole method is hacky, but I'm not entirely sure what I want
- // the API to be
- logger.listen = function(event, obj) {
- obj.on(event, function() {
- try {
- var currentTime = new Date(),
- timestamp = currentTime.getFullYear() + "/" + (currentTime.getMonth() + 1) + "/" + currentTime.getDate() + ":" + currentTime.getHours() + ":" + currentTime.getMinutes() + ":" + currentTime.getSeconds();
- var theMsg = {'time' : timestamp, 'action' : event};
- // Socket event, the argument was a Client
- if (arguments && arguments[0] && arguments[0].hasOwnProperty('sessionId')) {
- theMsg.client = arguments[0].sessionId;
- // Possible message event
- }
- else if (arguments && arguments[0]) {
- try {
- var parsedMsg = JSON.parse(arguments[0]);
- } catch (ex) {
+function makeLogger() {
+ var logger = {
+ stream : process.stdout,
+ socketLogger : null,
+ authToken : null,
+ logLevel : 0,
+ failSilently : true,
+ // This whole method is hacky, but I'm not entirely sure what I want
+ // the API to be
+ listen : function(event, obj) {
+ obj.on(event, function() {
+ try {
+ var currentTime = new Date(),
+ timestamp = currentTime.getFullYear() + "/" + (currentTime.getMonth() + 1) + "/" + currentTime.getDate() + ":" + currentTime.getHours() + ":" + currentTime.getMinutes() + ":" + currentTime.getSeconds();
+ var theMsg = {'time' : timestamp, 'action' : event};
+ // Socket event, the argument was a Client
+ if (arguments && arguments[0] && arguments[0].hasOwnProperty('sessionId')) {
+ theMsg.client = arguments[0].sessionId;
+ // Possible message event
+ }
+ else if (arguments && arguments[0]) {
+ try { var parsedMsg = JSON.parse(arguments[0]); } catch (x) {}
+ if (parsedMsg && logger.logLevel > 0)
+ theMsg.body = parsedMsg;
+ if (obj.hasOwnProperty('sessionId'))
+ theMsg.client = obj.sessionId;
+
+ if (parsedMsg && parsedMsg.authToken === logger.authToken)
+ logger.socketLogger = obj;
}
- if (parsedMsg)
- theMsg.body = parsedMsg;
- if (obj.hasOwnProperty('sessionId'))
- theMsg.client = obj.sessionId;
-
- if (parsedMsg && parsedMsg.authToken === logger.authToken)
- logger.socketLogger = obj;
+ logger.log(theMsg);
+ } catch (ex) {
+ if (!logger.failSilently)
+ throw ex;
}
- logger.log(theMsg);
- } catch (ex) { }
- });
- }
+ });
+ },
- logger.log = function(msg, logOptions) {
- try {
- var serializedMsg = JSON.stringify(msg),
- stream = logger.options.stream || process.stdout;
- stream.write(serializedMsg + '\n', 'utf8');
- if (logger.socketLogger && logger.socketLogger.send) {
- logger.socketLogger.send(serializedMsg);
+ log : function(msg, logOptions) {
+ try {
+ var serializedMsg = JSON.stringify(msg);
+ stream = logger.stream;
+ stream.write(serializedMsg + '\n', 'utf8');
+ if (logger.socketLogger && logger.socketLogger.send) {
+ logger.socketLogger.send(serializedMsg);
+ }
+ } catch (ex) {
+ if (!logger.failSilently)
+ throw ex;
}
- } catch (ex) {
}
}
return logger;
}
-module.exports = makeLogger();
+exports.defaultLogger = makeLogger();
+exports.newLogger = makeLogger;

0 comments on commit 51eade2

Please sign in to comment.