Browse files

Added file logging system to make errors easier to track

  • Loading branch information...
1 parent 842c8f6 commit e0e6813e1d030b994608dd8483eaf0e268d85c3b @meltingice committed Sep 5, 2010
Showing with 56 additions and 20 deletions.
  1. +12 −9 NodeMonitor_Node.js
  2. +12 −11 NodeMonitor_Server.js
  3. +32 −0 lib/logger.js
  4. 0 logs/node_log
  5. 0 logs/server_log
View
21 NodeMonitor_Node.js
@@ -9,7 +9,8 @@
*/
var fs = require('fs'),
- net = require('net');
+ net = require('net'),
+ logger = require('./lib/logger');
var MonitorNode = {
config: require('./config/node_config'),
@@ -18,8 +19,10 @@ var MonitorNode = {
last_send: true
};
-MonitorNode.start = function() {
- console.log("Starting NodeMonitor Node!");
+MonitorNode.start = function() {
+ logger.set_dest('node');
+ logger.enable_console(true);
+ logger.write("Starting NodeMonitor Node!");
this.load_plugins();
this.server_connect();
@@ -39,29 +42,29 @@ MonitorNode.load_plugins = function() {
plugin_count++;
});
- console.log(plugin_count + " plugins loaded.");
+ logger.write(plugin_count + " plugins loaded.");
}
MonitorNode.server_connect = function() {
this.server_conn = net.createConnection(this.config.server_port, this.config.server_addr);
this.server_conn.on('connect', function() {
- console.log("Connected to NodeMonitor Server at " + MonitorNode.config.server_addr + ":" + MonitorNode.config.server_port);
+ logger.write("Connected to NodeMonitor Server at " + MonitorNode.config.server_addr + ":" + MonitorNode.config.server_port);
MonitorNode.server_conn.setKeepAlive(true);
MonitorNode.execute_plugins();
});
this.server_conn.on('error', function(exception) {
- console.log('Error: ' + exception.message);
+ logger.write('Error: ' + exception.message);
MonitorNode.server_reconnect();
});
this.server_conn.on('timeout', function() {
- console.log('Error: connection to server timed out');
+ logger.write('Error: connection to server timed out');
MonitorNode.server_reconnect();
});
}
MonitorNode.server_reconnect = function() {
- console.log('Attempting reconnect to server in 2 seconds...');
+ logger.write('Attempting reconnect to server in 2 seconds...');
setTimeout(function() {
MonitorNode.server_connect();
}, 2000);
@@ -82,7 +85,7 @@ MonitorNode.execute_plugins = function() {
MonitorNode.send_data = function(plugin, render, data) {
if(this.server_conn.readyState != 'open' && this.server_conn.readyState != 'writeOnly') {
- console.log('Error: socket not ready, skipping transmission.');
+ logger.write('Error: socket not ready, skipping transmission.');
return;
}
View
23 NodeMonitor_Server.js
@@ -11,7 +11,8 @@
var net = require('net'),
http = require('http'),
io = require('./lib/socket.io'),
- fs = require('fs');
+ fs = require('fs'),
+ logger = require('./lib/logger');
var MonitorServer = {
config: require('./config/server_config'),
@@ -20,8 +21,10 @@ var MonitorServer = {
websocket: null
};
-MonitorServer.start = function() {
- console.log('Starting NodeMonitor Server!');
+MonitorServer.start = function() {
+ logger.set_dest('server');
+ logger.enable_console(true);
+ logger.write('Starting NodeMonitor Server!');
this.start_node_listener();
this.start_web_server();
@@ -31,28 +34,28 @@ MonitorServer.start_node_listener = function() {
this.server = net.createServer(function(stream) {
stream.setEncoding('utf8');
stream.on('connect', function(t) {
- console.log("Node connected!");
+ logger.write("Node connected from " + stream.remoteAddress);
+ logger.write("Now monitoring " + MonitorServer.server.connections +" node(s)");
});
stream.on('data', function(data) {
var reqs = data.split(/\/\/START\/\/(.*?)\/\/END\/\//);
if(reqs.length > 0) {
reqs.forEach(function(req) {
if(req == ""){ return true; }
- //console.log(JSON.parse(req));
if(MonitorServer.websocket) {
MonitorServer.websocket.broadcast(req);
}
});
}
});
stream.on('end', function() {
- console.log("Node disconnecting...");
+ logger.write("Node disconnecting...");
});
});
this.server.listen(this.config.node_listen_port, this.config.node_listen_addr);
- console.log("NodeMonitor Server now listening on " + this.config.node_listen_addr + ":" + this.config.node_listen_port);
+ logger.write("NodeMonitor Server now listening on " + this.config.node_listen_addr + ":" + this.config.node_listen_port);
}
MonitorServer.start_web_server = function() {
@@ -66,7 +69,7 @@ MonitorServer.start_web_server = function() {
url = req.url.replace('..', '');
}
- console.log(req.socket.remoteAddress + ' - ' + req.method + ' ' + url);
+ logger.write(req.socket.remoteAddress + ' - ' + req.method + ' ' + url);
fs.readFile('./html' + url, function(err, data) {
if(err) {
res.close();
@@ -83,9 +86,7 @@ MonitorServer.start_web_server = function() {
});
this.httpserver.listen(this.config.http_listen_port);
-
- this.websocket = io.listen(this.httpserver);
-
+ this.websocket = io.listen(this.httpserver);
}
// Start the server!
View
32 lib/logger.js
@@ -0,0 +1,32 @@
+var dest = 'server';
+var log_console = false;
+
+var fs = require('fs'),
+ exec = require('child_process').exec;
+
+this.set_dest = function(set_dest) {
+ dest = set_dest;
+}
+
+this.enable_console = function(set) {
+ log_console = set;
+}
+
+this.write = function(msg) {
+ if(log_console)
+ console.log(msg);
+
+ var filename = './logs/'+dest+'_log';
+ msg += '\n';
+ fs.open(filename, 'a', 0666, function(err, fd) {
+ if(err) return;
+
+ exec('date "+%Y-%m-%d %H:%M:%S"', function(err, stdout, stderr) {
+ if(err) return;
+
+ msg = stdout.replace('\n', '') + ' - ' + msg;
+ fs.write(fd, msg, null, 'utf8', function(err, written) {
+ });
+ });
+ });
+}
View
0 logs/node_log
No changes.
View
0 logs/server_log
No changes.

0 comments on commit e0e6813

Please sign in to comment.