Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding buffering support for logging to disk

  • Loading branch information...
commit b86c534954ae14746f71cf009a8cde56aacc9bea 1 parent e6e65b4
@saikat authored
Showing with 12 additions and 3 deletions.
  1. +12 −3 lib/socket-logger.js
View
15 lib/socket-logger.js
@@ -1,3 +1,5 @@
+var bufferDuration = 1000;
+
function makeLogger() {
var logger = {
stream : process.stdout,
@@ -5,6 +7,13 @@ function makeLogger() {
authToken : null,
logLevel : 0,
failSilently : true,
+ buf : [],
+ flushBuffer : function() {
+ if (logger.buf.length) {
+ logger.stream.write(logger.buf.join(''), 'utf8');
+ logger.buf = [];
+ }
+ },
monitor : function(socket) {
socket.on('connection', function(client) {
logger.log(['connect', client.sessionId]);
@@ -41,18 +50,18 @@ function makeLogger() {
var currentTime = new Date(),
timestamp = currentTime.getFullYear() + "/" + (currentTime.getMonth() + 1) + "/" + currentTime.getDate() + ":" + currentTime.getHours() + ":" + currentTime.getMinutes() + ":" + currentTime.getSeconds();
msg.unshift(timestamp);
- msg = JSON.stringify(msg);
- stream = logger.stream;
- stream.write(msg + '\n', 'utf8');
+ msg = JSON.stringify(msg) + '\n';
if (logger.socketLogger && logger.socketLogger.send) {
logger.socketLogger.send(msg);
}
+ logger.buf.push(msg);
} catch (ex) {
if (!logger.failSilently)
throw ex;
}
}
}
+ setInterval(logger.flushBuffer, bufferDuration);
return logger;
}
Please sign in to comment.
Something went wrong with that request. Please try again.