Skip to content

Commit

Permalink
Added logging module with log levels.
Browse files Browse the repository at this point in the history
Removed module-level LOGGERs, attached logger instances to
Harvester, Server instances for better encapsulation, stubbing.

Modified Stream, History button CSS, just 'cause.
  • Loading branch information
msmathers committed Sep 28, 2011
1 parent 5e5f232 commit 3f9872e
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 32 deletions.
3 changes: 2 additions & 1 deletion bin/install/harvester
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
# Log.io harvester install script (run as root)
#

# Copy log.io source to /usr/local/include
# Copy log.io source to /usr/local/lib
echo "Copying source to /usr/local/lib/node_modules/log.io/";
mkdir -p /usr/local/lib/node_modules/log.io
cp -r `dirname $0`/../../lib/harvester/ /usr/local/lib/node_modules/log.io/
cp -r `dirname $0`/../../lib/logging.js /usr/local/lib/node_modules/log.io/

# Copy run_harvester to /usr/local/bin
echo "Copying bin/run_harvester to /usr/local/bin";
Expand Down
3 changes: 2 additions & 1 deletion bin/install/server
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
# Log.io server install script (run as root)
#

# Copy log.io source to /usr/local/include
# Copy log.io source to /usr/local/lib

echo "Copying source to /usr/local/lib/node_modules/log.io/";
mkdir -p /usr/local/lib/node_modules/log.io
cp -r `dirname $0`/../../lib/server/ /usr/local/lib/node_modules/log.io/
cp -r `dirname $0`/../../lib/client/ /usr/local/lib/node_modules/log.io/
cp -r `dirname $0`/../../lib/logging.js /usr/local/lib/node_modules/log.io/

# Copy run_server to /usr/local/bin
echo "Copying bin/run_server to /usr/local/bin";
Expand Down
7 changes: 6 additions & 1 deletion bin/run_harvester
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
*/

// Hack console.log for logging
// TODO(msmathers): forever will handle this in v0.2.1
var fs = require('fs');
var fd = fs.openSync('/var/log/log.io/harvester.log', 'a');
console.log = function(msg) { fs.writeSync(fd, msg + "\n"); }
var flog = function(msg) { fs.writeSync(fd, msg + "\n"); }
console.log = flog;
console.info = flog;
console.warn = flog;
console.error = flog;

// Paths
var node_lib = '/usr/local/lib/node_modules/';
Expand Down
7 changes: 6 additions & 1 deletion bin/run_server
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
*/

// Hack console.log for logging
// TODO(msmathers): forever will handle this in v0.2.1
var fs = require('fs');
var fd = fs.openSync('/var/log/log.io/server.log', 'a');
console.log = function(msg) { fs.writeSync(fd, msg + "\n"); }
var flog = function(msg) { fs.writeSync(fd, msg + "\n"); }
console.log = flog;
console.info = flog;
console.warn = flog;
console.error = flog;

// Paths
var node_lib = '/usr/local/lib/node_modules/';
Expand Down
6 changes: 3 additions & 3 deletions lib/client/css/web_client.css
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ body {

.add-button {
margin: 2px 0px 0px 10px;
padding: 3px 6px;
font-size: 14px;
padding: 4px 8px;
font-size: 12px;
color: #000;
font-weight: normal;
font-weight: bold;
background: #444;
border: 1px solid #aaa;
border-bottom: 1px solid #888;
Expand Down
29 changes: 14 additions & 15 deletions lib/harvester/log_harvester.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@
var __ = require('underscore');
var io = require('socket.io-client');
var lf = require('./log_file.js');
var logging = require('../logging.js');

var STATUS_INTERVAL = 60 * 1000; // 60 seconds
var RECONNECT_INTERVAL = 5 * 1000; // 5 seconds
var HEARTBEAT_PERIOD = 20 * 1000; // 20 seconds
var HEARTBEAT_FAILS = 3; // Reconnect after 3 missed heartbeats
var LOGGER = function(msg) {
console.log((new Date().toTimeString().slice(0,8)) + " - " + msg);
}

// LogHarvester gets registered as a Node on the LogServer.
// Contains config information, LogFile pool, and socket
var LogHarvester = function(conf) {
if (!conf) { throw Error("Missing configuration"); }
if (!conf.server) { throw Error("Missing server configuration"); }
if (!conf.log_file_paths) { conf.log_file_paths = {} };
this._log = new logging.Logger();
this._conf = conf;
this._conf.server = conf.server;
this._conf.log_file_paths = conf.log_file_paths;
Expand Down Expand Up @@ -59,7 +58,7 @@ LogHarvester.prototype = {

// Register announcement callback
harvester.socket.on('connect', function() {
LOGGER("Connected to server, sending announcement...");
harvester._log.info("Connected to server, sending announcement...");
harvester.announce();
harvester.connected = true;
harvester.reconnecting = false;
Expand All @@ -68,16 +67,16 @@ LogHarvester.prototype = {

// Server heartbeat
harvester.socket.on('heartbeat', function() {
LOGGER("Received server heartbeat");
harvester._log.info("Received server heartbeat");
harvester.last_heartbeat = new Date().getTime();
return;
});

// Node with same label already exists on server, kill process
harvester.socket.on('node_already_exists', function() {
LOGGER("ERROR: A node of the same name is already registered");
LOGGER("with the log server. Change this harvester's instance_name.");
LOGGER("Exiting.");
harvester._log.error("ERROR: A node of the same name is already registered");
harvester._log.error("with the log server. Change this harvester's instance_name.");
harvester._log.error("Exiting.");
process.exit(1);
});

Expand All @@ -90,15 +89,15 @@ LogHarvester.prototype = {
// Begin sending log messages to server
harvester.socket.on('enable_log', function(message) {
_log_file_helper(message, function(log_file) {
LOGGER("Enabling log file: " + log_file.label);
harvester._log.info("Enabling log file: " + log_file.label);
log_file.enable();
});
});

// Stop sending log messages to server
harvester.socket.on('disable_log', function(message) {
_log_file_helper(message, function(log_file) {
LOGGER("Disabling log file: " + log_file.label);
harvester._log.info("Disabling log file: " + log_file.label);
log_file.disable();
});
});
Expand All @@ -119,22 +118,22 @@ LogHarvester.prototype = {
// Begin watching log files
__(harvester.log_files).each(function(log_file, label) {
log_file.watch();
LOGGER("Watching: " + log_file.path + " (" + label + ")");
harvester._log.info("Watching: " + log_file.path + " (" + label + ")");
});

// Check for heartbeat every HEARTBEAT_PERIOD, reconnect if necessary
setInterval(function() {
var delta = ((new Date().getTime()) - harvester.last_heartbeat);
if (delta > (HEARTBEAT_PERIOD * HEARTBEAT_FAILS)) {
LOGGER("Failed heartbeat check, reconnecting...");
harvester._log.warn("Failed heartbeat check, reconnecting...");
harvester.connected = false;
harvester.reconnect();
}
}, HEARTBEAT_PERIOD);

// Print status every minute
setInterval(function() {
LOGGER("Watching " + __(harvester.log_files).size()
harvester._log.info("Watching " + __(harvester.log_files).size()
+ " log files, " + " sent " + harvester.messages_sent
+ " log messages.");
}, harvester._conf.status_frequency);
Expand All @@ -153,7 +152,7 @@ LogHarvester.prototype = {
reconnect: function(force) {
if (!force && this.reconnecting) { return; }
this.reconnecting = true;
LOGGER("Reconnecting to server...");
this._log.info("Reconnecting to server...");
var harvester = this;
setTimeout(function() {
if (harvester.connected) { return; }
Expand All @@ -172,7 +171,7 @@ LogHarvester.prototype = {
this.socket.emit(event, message);
// If server is down, a non-writeable stream error is thrown.
} catch(err) {
LOGGER("ERROR: Unable to send message over socket.");
this._log.error("ERROR: Unable to send message over socket.");
this.connected = false;
this.reconnect();
}
Expand Down
40 changes: 40 additions & 0 deletions lib/logging.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Generic logging module.
*
* Log Levels:
* - 3 (Debug)
* - 2 (Info)
* - 1 (Warn)
* - 0 (Error)
*/

var Logger = function(log_level) {
this._log_level = log_level ? log_level : 2
}

Logger.prototype = {
_timestamp: function(msg) {
return (new Date()).toLocaleString().slice(0,24);
},

debug: function(msg) {
if (this._log_level < 3) { return; }
console.info("[" + this._timestamp() + "] DEBUG: " + msg);
},

info: function(msg) {
if (this._log_level < 2) { return; }
console.info("[" + this._timestamp() + "] INFO: " + msg);
},

warn: function(msg) {
if (this._log_level < 1) { return; }
console.warn("[" + this._timestamp() + "] WARN: " + msg);
},

error: function(msg) {
if (this._log_level < 0) { return; }
console.error("[" + this._timestamp() + "] ERROR: " + msg);
}
}

exports.Logger = Logger;
13 changes: 6 additions & 7 deletions lib/server/log_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ var io = require('socket.io');
var __ = require('underscore');
var _node = require('./node.js');
var _wc = require('./web_client.js');
var logging = require('../logging.js');
var STATUS_INTERVAL = 60 * 1000; // 60 seconds
var HEARTBEAT_INTERVAL = 20 * 1000; // 20 seconds
var LOGGER = function(msg) {
console.log((new Date().toTimeString().slice(0,8)) + " - " + msg);
}

// LogServer runs a regular HTTP server
// Announce messages add each client to the appropriate pools
var LogServer = function(http_server) {
this._log = new logging.Logger();
this.http_server = http_server;
this.nodes = {};
this.web_clients = {};
Expand All @@ -25,7 +24,7 @@ var LogServer = function(http_server) {

// Print status every minute
setInterval(function() {
LOGGER("Nodes: " + __(log_server.nodes).size() + ", " +
log_server._log.info("Nodes: " + __(log_server.nodes).size() + ", " +
"WebClients: " + __(log_server.web_clients).size() + ", " +
"Messages Sent: " + log_server.message_count);
}, STATUS_INTERVAL);
Expand All @@ -47,7 +46,7 @@ LogServer.prototype = {

// If this node already exists, ignore announcemen
if (log_server.nodes[label]) {
LOGGER("Warning: Node '" + label + "' already exists, ignoring");
this._log.warn("Warning: Node '" + label + "' already exists, ignoring");
socket.emit('node_already_exists');
return;
}
Expand Down Expand Up @@ -88,11 +87,11 @@ LogServer.prototype = {
log_server.io.set('log level', 1); // TODO(msmathers): Make configurable
log_server.io.sockets.on('connection', function(socket) {
socket.on('announce_node', function(message) {
LOGGER("Registering new node");
log_server._log.info("Registering new node");
log_server.announce_node(socket, message);
});
socket.on('announce_web_client', function(message) {
LOGGER("Registering new web_client");
log_server._log.info("Registering new web_client");
log_server.announce_web_client(socket);
})
});
Expand Down
10 changes: 9 additions & 1 deletion tests/harvester/log_harvester_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ lf.LogFile = function(path, label, harvester) {
}
}

// Stub out console.log to suppress logging
// Stub out logger to suppress log messages
var FAKE_LOGGER = {
debug: function(msg){},
info: function(msg){},
warn: function(msg){},
error: function(msg){}
}

var TEST_CONFIG = {
'server' : {
'host' : 'server.host.com',
Expand All @@ -71,6 +78,7 @@ module.exports = testCase({

setUp: function(callback) {
this.obj_ut = new LogHarvester(TEST_CONFIG);
this.obj_ut._log = FAKE_LOGGER;
this.obj_ut.connect();
callback();
},
Expand Down
11 changes: 9 additions & 2 deletions tests/server/log_server_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ var _wc = require('../../lib/server/web_client.js');
var testCase = require('nodeunit').testCase;
var LogServer = require('../../lib/server/log_server.js').LogServer;

// Stub out console.log()
//console.log = function(){}
// Stub out logger to suppress log messages
var FAKE_LOGGER = {
debug: function(msg){},
info: function(msg){},
warn: function(msg){},
error: function(msg){}
}

// Unit Tests
module.exports = testCase({
Expand All @@ -22,6 +27,7 @@ module.exports = testCase({
setInterval = function(){};
this.http_server = {};
this.obj_ut = new LogServer(this.http_server);
this.obj_ut._log = FAKE_LOGGER;
callback();
},

Expand Down Expand Up @@ -183,6 +189,7 @@ module.exports = testCase({
// Stub out connection callback
var t = this;
this.obj_ut.io = {
set: function(k,v){},
sockets: {
events: {},
broadcasts: [],
Expand Down

0 comments on commit 3f9872e

Please sign in to comment.