Permalink
Browse files

If anything goes wrong with the logger, just fail silently instead of…

… bringing down the house
  • Loading branch information...
1 parent 589fede commit 14a044ccefe5c6301d3caa90f2ac55ff60dbc130 @saikat committed Sep 2, 2010
Showing with 30 additions and 25 deletions.
  1. +30 −25 lib/socket-logger.js
View
@@ -7,36 +7,41 @@ function makeLogger(options) {
// the API to be
logger.listen = function(event, obj) {
obj.on(event, function() {
- // Socket event, the argument was a Client
- if (arguments && arguments[0] && arguments[0].hasOwnProperty('sessionId'))
- logger.log({'action' : event, 'client' : arguments[0].sessionId});
- // Possible message event
- else if (arguments && arguments[0]) {
- try {
- var parsedMsg = JSON.parse(arguments[0]);
- } catch (ex) {
- }
- var theMsg = {'action' : event};
- if (parsedMsg)
- theMsg.body = parsedMsg;
- if (obj.hasOwnProperty('sessionId'))
- theMsg.client = obj.sessionId;
+ try {
+ // Socket event, the argument was a Client
+ if (arguments && arguments[0] && arguments[0].hasOwnProperty('sessionId'))
+ logger.log({'action' : event, 'client' : arguments[0].sessionId});
+ // Possible message event
+ else if (arguments && arguments[0]) {
+ try {
+ var parsedMsg = JSON.parse(arguments[0]);
+ } catch (ex) {
+ }
+ var theMsg = {'action' : event};
+ if (parsedMsg)
+ theMsg.body = parsedMsg;
+ if (obj.hasOwnProperty('sessionId'))
+ theMsg.client = obj.sessionId;
- logger.log(theMsg);
+ logger.log(theMsg);
- if (parsedMsg && parsedMsg.authToken === logger.authToken)
- logger.socketLogger = obj;
- }
- else
- logger.log({'action' : event});
+ if (parsedMsg && parsedMsg.authToken === logger.authToken)
+ logger.socketLogger = obj;
+ }
+ else
+ logger.log({'action' : event});
+ } catch (ex) { }
});
}
logger.log = function(msg, logOptions) {
- 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);
+ 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);
+ }
+ } catch (ex) {
}
}
return logger;

0 comments on commit 14a044c

Please sign in to comment.