From 7c83051c3de6ffcd3e4cf1626c7fed09a55dc85c Mon Sep 17 00:00:00 2001 From: Samuel Clay Date: Wed, 1 May 2013 12:51:59 -0700 Subject: [PATCH] Better logging for unread counts. --- node/log.coffee | 5 +++++ node/log.js | 13 +++++++++++++ node/unread_counts.coffee | 11 ++++++----- node/unread_counts.js | 10 ++++++---- 4 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 node/log.coffee create mode 100644 node/log.js diff --git a/node/log.coffee b/node/log.coffee new file mode 100644 index 000000000..60794fabf --- /dev/null +++ b/node/log.coffee @@ -0,0 +1,5 @@ +info = (username, message) -> + timestamp = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '') + console.log "[#{timestamp}] ---> [#{username}] #{message}" + +exports.info = info \ No newline at end of file diff --git a/node/log.js b/node/log.js new file mode 100644 index 000000000..995a76e84 --- /dev/null +++ b/node/log.js @@ -0,0 +1,13 @@ +// Generated by CoffeeScript 1.4.0 +(function() { + var info; + + info = function(username, message) { + var timestamp; + timestamp = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''); + return console.log("[" + timestamp + "] ---> [" + username + "] " + message); + }; + + exports.info = info; + +}).call(this); diff --git a/node/unread_counts.coffee b/node/unread_counts.coffee index b6e8c2ec8..25189bd27 100644 --- a/node/unread_counts.coffee +++ b/node/unread_counts.coffee @@ -1,5 +1,6 @@ fs = require 'fs' redis = require 'redis' +log = require './log.js' REDIS_SERVER = if process.env.NODE_ENV == 'development' then 'localhost' else 'db12' SECURE = !!process.env.NODE_SSL @@ -39,9 +40,9 @@ io.configure 'development', -> io.sockets.on 'connection', (socket) -> socket.on 'subscribe:feeds', (@feeds, @username) -> - console.log " ---> [#{@username}] Subscribing to #{feeds.length} feeds " + - " (#{io.sockets.clients().length} users on) " + - " #{if SECURE then "(SSL)" else "(non-SSL)"}" + log.info @username, "Subscribing to #{feeds.length} feeds " + + " (#{io.sockets.clients().length} users on) " + + " #{if SECURE then "(SSL)" else "(non-SSL)"}" if not @username return @@ -52,7 +53,7 @@ io.sockets.on 'connection', (socket) -> socket.subscribe.subscribe @username socket.subscribe.on 'message', (channel, message) => - console.log " ---> [#{@username}] Update on #{channel}: #{message}" + log.info @username, "Update on #{channel}: #{message}" if channel == @username socket.emit 'user:update', channel, message else @@ -61,6 +62,6 @@ io.sockets.on 'connection', (socket) -> socket.on 'disconnect', () -> socket.subscribe?.end() ip = socket.handshake.address.address - console.log " ---> [#{@username}] Disconnect (#{@feeds?.length} feeds, #{ip})," + + log.info @username, "Disconnect (#{@feeds?.length} feeds, #{ip})," + " there are now #{io.sockets.clients().length-1} users. " + " #{if SECURE then "(SSL)" else "(non-SSL)"}" diff --git a/node/unread_counts.js b/node/unread_counts.js index 8f8b13e9d..18619ed79 100644 --- a/node/unread_counts.js +++ b/node/unread_counts.js @@ -1,11 +1,13 @@ // Generated by CoffeeScript 1.4.0 (function() { - var REDIS_SERVER, SECURE, ca, certificate, fs, io, privateKey, redis; + var REDIS_SERVER, SECURE, ca, certificate, fs, io, log, privateKey, redis; fs = require('fs'); redis = require('redis'); + log = require('./log.js'); + REDIS_SERVER = process.env.NODE_ENV === 'development' ? 'localhost' : 'db12'; SECURE = !!process.env.NODE_SSL; @@ -40,7 +42,7 @@ _this = this; this.feeds = feeds; this.username = username; - console.log((" ---> [" + this.username + "] Subscribing to " + feeds.length + " feeds ") + (" (" + (io.sockets.clients().length) + " users on) ") + (" " + (SECURE ? "(SSL)" : "(non-SSL)"))); + log.info(this.username, ("Subscribing to " + feeds.length + " feeds ") + (" (" + (io.sockets.clients().length) + " users on) ") + (" " + (SECURE ? "(SSL)" : "(non-SSL)"))); if (!this.username) { return; } @@ -51,7 +53,7 @@ socket.subscribe.subscribe(this.feeds); socket.subscribe.subscribe(this.username); return socket.subscribe.on('message', function(channel, message) { - console.log(" ---> [" + _this.username + "] Update on " + channel + ": " + message); + log.info(_this.username, "Update on " + channel + ": " + message); if (channel === _this.username) { return socket.emit('user:update', channel, message); } else { @@ -65,7 +67,7 @@ _ref.end(); } ip = socket.handshake.address.address; - return console.log((" ---> [" + this.username + "] Disconnect (" + ((_ref1 = this.feeds) != null ? _ref1.length : void 0) + " feeds, " + ip + "),") + (" there are now " + (io.sockets.clients().length - 1) + " users. ") + (" " + (SECURE ? "(SSL)" : "(non-SSL)"))); + return log.info(this.username, ("Disconnect (" + ((_ref1 = this.feeds) != null ? _ref1.length : void 0) + " feeds, " + ip + "),") + (" there are now " + (io.sockets.clients().length - 1) + " users. ") + (" " + (SECURE ? "(SSL)" : "(non-SSL)"))); }); });