Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Adding buffering support for logging to disk

  • Loading branch information...
commit b86c534954ae14746f71cf009a8cde56aacc9bea 1 parent e6e65b4
Saikat Chakrabarti authored

Showing 1 changed file with 12 additions and 3 deletions. Show diff stats Hide diff stats

  1. +12 3 lib/socket-logger.js
15 lib/socket-logger.js
... ... @@ -1,3 +1,5 @@
  1 +var bufferDuration = 1000;
  2 +
1 3 function makeLogger() {
2 4 var logger = {
3 5 stream : process.stdout,
@@ -5,6 +7,13 @@ function makeLogger() {
5 7 authToken : null,
6 8 logLevel : 0,
7 9 failSilently : true,
  10 + buf : [],
  11 + flushBuffer : function() {
  12 + if (logger.buf.length) {
  13 + logger.stream.write(logger.buf.join(''), 'utf8');
  14 + logger.buf = [];
  15 + }
  16 + },
8 17 monitor : function(socket) {
9 18 socket.on('connection', function(client) {
10 19 logger.log(['connect', client.sessionId]);
@@ -41,18 +50,18 @@ function makeLogger() {
41 50 var currentTime = new Date(),
42 51 timestamp = currentTime.getFullYear() + "/" + (currentTime.getMonth() + 1) + "/" + currentTime.getDate() + ":" + currentTime.getHours() + ":" + currentTime.getMinutes() + ":" + currentTime.getSeconds();
43 52 msg.unshift(timestamp);
44   - msg = JSON.stringify(msg);
45   - stream = logger.stream;
46   - stream.write(msg + '\n', 'utf8');
  53 + msg = JSON.stringify(msg) + '\n';
47 54 if (logger.socketLogger && logger.socketLogger.send) {
48 55 logger.socketLogger.send(msg);
49 56 }
  57 + logger.buf.push(msg);
50 58 } catch (ex) {
51 59 if (!logger.failSilently)
52 60 throw ex;
53 61 }
54 62 }
55 63 }
  64 + setInterval(logger.flushBuffer, bufferDuration);
56 65 return logger;
57 66 }
58 67

0 comments on commit b86c534

Please sign in to comment.
Something went wrong with that request. Please try again.