Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added file logging system to make errors easier to track

  • Loading branch information...
commit e0e6813e1d030b994608dd8483eaf0e268d85c3b 1 parent 842c8f6
Ryan LeFevre authored
21 NodeMonitor_Node.js
@@ -9,7 +9,8 @@
9 9 */
10 10
11 11 var fs = require('fs'),
12   - net = require('net');
  12 + net = require('net'),
  13 + logger = require('./lib/logger');
13 14
14 15 var MonitorNode = {
15 16 config: require('./config/node_config'),
@@ -18,8 +19,10 @@ var MonitorNode = {
18 19 last_send: true
19 20 };
20 21
21   -MonitorNode.start = function() {
22   - console.log("Starting NodeMonitor Node!");
  22 +MonitorNode.start = function() {
  23 + logger.set_dest('node');
  24 + logger.enable_console(true);
  25 + logger.write("Starting NodeMonitor Node!");
23 26
24 27 this.load_plugins();
25 28 this.server_connect();
@@ -39,29 +42,29 @@ MonitorNode.load_plugins = function() {
39 42 plugin_count++;
40 43 });
41 44
42   - console.log(plugin_count + " plugins loaded.");
  45 + logger.write(plugin_count + " plugins loaded.");
43 46 }
44 47
45 48 MonitorNode.server_connect = function() {
46 49 this.server_conn = net.createConnection(this.config.server_port, this.config.server_addr);
47 50
48 51 this.server_conn.on('connect', function() {
49   - console.log("Connected to NodeMonitor Server at " + MonitorNode.config.server_addr + ":" + MonitorNode.config.server_port);
  52 + logger.write("Connected to NodeMonitor Server at " + MonitorNode.config.server_addr + ":" + MonitorNode.config.server_port);
50 53 MonitorNode.server_conn.setKeepAlive(true);
51 54 MonitorNode.execute_plugins();
52 55 });
53 56 this.server_conn.on('error', function(exception) {
54   - console.log('Error: ' + exception.message);
  57 + logger.write('Error: ' + exception.message);
55 58 MonitorNode.server_reconnect();
56 59 });
57 60 this.server_conn.on('timeout', function() {
58   - console.log('Error: connection to server timed out');
  61 + logger.write('Error: connection to server timed out');
59 62 MonitorNode.server_reconnect();
60 63 });
61 64 }
62 65
63 66 MonitorNode.server_reconnect = function() {
64   - console.log('Attempting reconnect to server in 2 seconds...');
  67 + logger.write('Attempting reconnect to server in 2 seconds...');
65 68 setTimeout(function() {
66 69 MonitorNode.server_connect();
67 70 }, 2000);
@@ -82,7 +85,7 @@ MonitorNode.execute_plugins = function() {
82 85
83 86 MonitorNode.send_data = function(plugin, render, data) {
84 87 if(this.server_conn.readyState != 'open' && this.server_conn.readyState != 'writeOnly') {
85   - console.log('Error: socket not ready, skipping transmission.');
  88 + logger.write('Error: socket not ready, skipping transmission.');
86 89 return;
87 90 }
88 91
23 NodeMonitor_Server.js
@@ -11,7 +11,8 @@
11 11 var net = require('net'),
12 12 http = require('http'),
13 13 io = require('./lib/socket.io'),
14   - fs = require('fs');
  14 + fs = require('fs'),
  15 + logger = require('./lib/logger');
15 16
16 17 var MonitorServer = {
17 18 config: require('./config/server_config'),
@@ -20,8 +21,10 @@ var MonitorServer = {
20 21 websocket: null
21 22 };
22 23
23   -MonitorServer.start = function() {
24   - console.log('Starting NodeMonitor Server!');
  24 +MonitorServer.start = function() {
  25 + logger.set_dest('server');
  26 + logger.enable_console(true);
  27 + logger.write('Starting NodeMonitor Server!');
25 28
26 29 this.start_node_listener();
27 30 this.start_web_server();
@@ -31,7 +34,8 @@ MonitorServer.start_node_listener = function() {
31 34 this.server = net.createServer(function(stream) {
32 35 stream.setEncoding('utf8');
33 36 stream.on('connect', function(t) {
34   - console.log("Node connected!");
  37 + logger.write("Node connected from " + stream.remoteAddress);
  38 + logger.write("Now monitoring " + MonitorServer.server.connections +" node(s)");
35 39 });
36 40 stream.on('data', function(data) {
37 41 var reqs = data.split(/\/\/START\/\/(.*?)\/\/END\/\//);
@@ -39,7 +43,6 @@ MonitorServer.start_node_listener = function() {
39 43 reqs.forEach(function(req) {
40 44 if(req == ""){ return true; }
41 45
42   - //console.log(JSON.parse(req));
43 46 if(MonitorServer.websocket) {
44 47 MonitorServer.websocket.broadcast(req);
45 48 }
@@ -47,12 +50,12 @@ MonitorServer.start_node_listener = function() {
47 50 }
48 51 });
49 52 stream.on('end', function() {
50   - console.log("Node disconnecting...");
  53 + logger.write("Node disconnecting...");
51 54 });
52 55 });
53 56
54 57 this.server.listen(this.config.node_listen_port, this.config.node_listen_addr);
55   - console.log("NodeMonitor Server now listening on " + this.config.node_listen_addr + ":" + this.config.node_listen_port);
  58 + logger.write("NodeMonitor Server now listening on " + this.config.node_listen_addr + ":" + this.config.node_listen_port);
56 59 }
57 60
58 61 MonitorServer.start_web_server = function() {
@@ -66,7 +69,7 @@ MonitorServer.start_web_server = function() {
66 69 url = req.url.replace('..', '');
67 70 }
68 71
69   - console.log(req.socket.remoteAddress + ' - ' + req.method + ' ' + url);
  72 + logger.write(req.socket.remoteAddress + ' - ' + req.method + ' ' + url);
70 73 fs.readFile('./html' + url, function(err, data) {
71 74 if(err) {
72 75 res.close();
@@ -83,9 +86,7 @@ MonitorServer.start_web_server = function() {
83 86 });
84 87
85 88 this.httpserver.listen(this.config.http_listen_port);
86   -
87   - this.websocket = io.listen(this.httpserver);
88   -
  89 + this.websocket = io.listen(this.httpserver);
89 90 }
90 91
91 92 // Start the server!
32 lib/logger.js
... ... @@ -0,0 +1,32 @@
  1 +var dest = 'server';
  2 +var log_console = false;
  3 +
  4 +var fs = require('fs'),
  5 + exec = require('child_process').exec;
  6 +
  7 +this.set_dest = function(set_dest) {
  8 + dest = set_dest;
  9 +}
  10 +
  11 +this.enable_console = function(set) {
  12 + log_console = set;
  13 +}
  14 +
  15 +this.write = function(msg) {
  16 + if(log_console)
  17 + console.log(msg);
  18 +
  19 + var filename = './logs/'+dest+'_log';
  20 + msg += '\n';
  21 + fs.open(filename, 'a', 0666, function(err, fd) {
  22 + if(err) return;
  23 +
  24 + exec('date "+%Y-%m-%d %H:%M:%S"', function(err, stdout, stderr) {
  25 + if(err) return;
  26 +
  27 + msg = stdout.replace('\n', '') + ' - ' + msg;
  28 + fs.write(fd, msg, null, 'utf8', function(err, written) {
  29 + });
  30 + });
  31 + });
  32 +}
0  logs/node_log
No changes.
0  logs/server_log
No changes.

0 comments on commit e0e6813

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