Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Now with colored log messages

  • Loading branch information...
commit 35dcbef94b3bf984a44e36b577e01996c1a4bde8 1 parent 09b0598
Ryan Olds authored March 31, 2012
2  aggregator/lib/collector.js
@@ -49,7 +49,7 @@ Collector.prototype.removeWatcher = function(toUnwatch, monitor, callback) {
49 49
     return callback('Unknown watchable');
50 50
   }
51 51
 
52  
-  var watchable = this.watchables[toUnwatch]
  52
+  var watchable = this.watchables[toUnwatch];
53 53
 
54 54
   var pos = watchable.watchers.indexOf(monitor);
55 55
   if(pos === -1) {
23  aggregator/lib/public/css/monitor.css
... ...
@@ -1,34 +1,35 @@
1 1
 
2  
-#list {
  2
+#messages {
  3
+
  4
+}
  5
+
  6
+#collectors {
3 7
     width: 200px;
4  
-    position: absolute;
5  
-    right: 0px;
  8
+    float: right;
  9
+    height: 100%;
  10
+    margin-left: 10px;
6 11
 }
7 12
 
8  
-#list .collector {
  13
+.collector {
9 14
 
10 15
 }
11 16
 
12  
-#list .collector > .name {
  17
+.collector > .name {
13 18
     height: 20px;
14 19
     background-color: #EEE;
15 20
     font-size: 18px;
16 21
     padding: 5px;
17 22
 }
18 23
 
19  
-#list .watchables {
  24
+.watchables {
20 25
     font-size: 14px;
21 26
     margin-left: 15px;
22 27
 }
23 28
 
24  
-#list .watchable {
  29
+.watchable {
25 30
     padding: 5px 0 5px 5px;
26 31
 }
27 32
 
28 33
 .clickable {
29 34
     cursor: pointer;
30  
-}
31  
-
32  
-#messages {
33  
-
34 35
 }
12  aggregator/lib/public/js/collector.js
@@ -4,7 +4,7 @@ var Collector = function(monitor, id, watchables) {
4 4
   this.id = id;
5 5
   this.watchables = watchables;
6 6
   this.ui = {};
7  
-  this.active = [];
  7
+  this.active = {};
8 8
 };
9 9
 
10 10
 Collector.prototype.createUI = function() {
@@ -63,13 +63,13 @@ Collector.prototype.createWatchable = function(id) {
63 63
   $(item).click({'collector': this, 'watchable': id}, function(e) {
64 64
     var collector = e.data.collector;
65 65
     var watchable = e.data.watchable;
66  
-    var pos = $.inArray(watchable, collector.active);
67  
-    if(pos === -1) {
68  
-      collector.active.push(watchable);
  66
+
  67
+    if(!collector.active[watchable]) {
  68
+      collector.active[watchable] = collector.parent.getColor();
69 69
       collector.watch(e.data.watchable);
70  
-      e.currentTarget.parentNode.style.backgroundColor = collector.parent.getColor();
  70
+      e.currentTarget.parentNode.style.backgroundColor = collector.active[watchable];
71 71
     } else {
72  
-      collector.active.splice(pos, 1);
  72
+      delete collector.active[watchable];
73 73
       collector.unwatch(e.data.watchable);
74 74
       e.currentTarget.parentNode.style.backgroundColor = 'transparent';
75 75
     }
59  aggregator/lib/public/js/monitor.js
@@ -2,22 +2,28 @@
2 2
 var Monitor = function(url) {
3 3
   this.url = url;
4 4
   this.ui = {};
  5
+  this.collectors = {};
5 6
 
6 7
   this.setupUI();
7 8
   this.setupSocketIO();
8 9
 };
9 10
 
10 11
 Monitor.prototype.setupUI = function() {
11  
-  var list = document.createElement('div');
12  
-  list.id = 'list';
13  
-  this.ui.list = list;
  12
+  var collectors = document.createElement('div');
  13
+  collectors.id = 'collectors';
  14
+  this.ui.collectors = collectors;
14 15
 
15 16
   var messages = document.createElement('div');
16 17
   messages.id = 'messages';
  18
+  this.ui.messages = messages;
  19
+
  20
+  var clear = document.createElement('div');
  21
+  clear.style.clear = 'both';
17 22
 
18 23
   var main = document.getElementById('loggregator');
19  
-  main.appendChild(list);
  24
+  main.appendChild(collectors);
20 25
   main.appendChild(messages);
  26
+  main.appendChild(clear);
21 27
 };
22 28
 
23 29
 Monitor.prototype.setupSocketIO = function() {
@@ -41,6 +47,30 @@ Monitor.prototype.setupSocketIO = function() {
41 47
         };
42 48
       });
43 49
     });
  50
+
  51
+    socket.on('data', function(data) {
  52
+      var collector = that.getCollector(data.hostname);
  53
+
  54
+      var message = document.createElement('div');
  55
+      message.className = 'message';
  56
+      message.style.color = collector.active[data.watchable];
  57
+
  58
+      var time = document.createElement('span');
  59
+      time.className = 'time';
  60
+      time.appendChild(document.createTextNode(data.date));
  61
+      var host = document.createElement('span');
  62
+      host.className = 'hostname';
  63
+      host.appendChild(document.createTextNode(['[', data.hostname, ']'].join('')));
  64
+      var log = document.createElement('span');
  65
+      log.className = 'log';
  66
+      log.appendChild(document.createTextNode(data.lines));
  67
+
  68
+      message.appendChild(time);
  69
+      message.appendChild(host);
  70
+      message.appendChild(log);
  71
+
  72
+      that.ui.messages.appendChild(message);
  73
+    });
44 74
   });
45 75
 
46 76
   this.socket = socket;
@@ -48,7 +78,14 @@ Monitor.prototype.setupSocketIO = function() {
48 78
 
49 79
 Monitor.prototype.addCollector = function(id, watchables) {
50 80
   var collector = new Collector(this, id, watchables);
51  
-  this.ui.list.appendChild(collector.createUI());
  81
+  this.collectors[id] = collector;
  82
+
  83
+  this.ui.collectors.appendChild(collector.createUI());
  84
+};
  85
+
  86
+Monitor.prototype.getCollector = function(id) {
  87
+  var c = this.collectors[id];
  88
+  return (c) ? c : undefined;
52 89
 };
53 90
 
54 91
 Monitor.prototype.watch = function(id, watchable) {
@@ -56,8 +93,10 @@ Monitor.prototype.watch = function(id, watchable) {
56 93
     'hostname': id,
57 94
     'watchable': watchable
58 95
   };
59  
-  this.socket.emit('watch', data, function() {
60  
-
  96
+  this.socket.emit('watch', data, function(error, response) {
  97
+    if(error) {
  98
+      throw new Error(error);
  99
+    }
61 100
   });
62 101
 };
63 102
 
@@ -66,8 +105,10 @@ Monitor.prototype.unwatch = function(id, watchable) {
66 105
     'hostname': id,
67 106
     'watchable': watchable
68 107
   };
69  
-  this.socket.emit('unwatch', data, function() {
70  
-
  108
+  this.socket.emit('unwatch', data, function(error, response) {
  109
+    if(error) {
  110
+      throw new Error(error);
  111
+    }
71 112
   });
72 113
 };
73 114
 
17  collector/lib/collector.js
@@ -2,6 +2,7 @@
2 2
 var os = require('os');
3 3
 var io = require('socket.io-client');
4 4
 var FileNotify = require('filenotify');
  5
+var LoremIpStream = require('loremipstream');
5 6
 
6 7
 var Collector = function Collector(files, url, key, callback) {
7 8
   var that = this;
@@ -19,8 +20,21 @@ var Collector = function Collector(files, url, key, callback) {
19 20
 
20 21
   // Start monitoring of a file/stream
21 22
   conn.on('watch', function(data, callback) {
  23
+    console.log('watch', arguments);
22 24
     if(!watchers[data.file]) {
23  
-      watchers[data.file] = new FileNotify(files[data.file]);
  25
+      var file = files[data.file];
  26
+      if(file !== '__loremipsum') {
  27
+        watchers[data.file] = new FileNotify(files[file]);
  28
+      } else { 
  29
+        var lorem = new LoremIpStream({
  30
+          dataSize: 100,
  31
+          dataInterval: 1000
  32
+        });
  33
+        lorem.setEncoding('utf8');
  34
+
  35
+        watchers[data.file] = lorem;
  36
+      }
  37
+
24 38
       watchers[data.file].on('data', function(lines) {
25 39
         that.conn.emit('data', {
26 40
           'date': new Date().toUTCString(),
@@ -39,6 +53,7 @@ var Collector = function Collector(files, url, key, callback) {
39 53
   conn.on('unwatch', function(data, callback) {
40 54
     if(watchers[data.file]) {
41 55
       watchers[data.file].destroy();
  56
+      delete watchers[data.file];
42 57
     }
43 58
 
44 59
     if(callback) {
3  collector/package.json
@@ -9,7 +9,8 @@
9 9
   "dependencies": {
10 10
     "async": "",
11 11
     "socket.io-client": "",
12  
-    "filenotify": ""
  12
+    "filenotify": "",
  13
+    "loremipstream": ""
13 14
   },
14 15
   "devDependencies": {
15 16
     "mocha": "",
2  example/collector.js
@@ -2,7 +2,7 @@
2 2
 var Collector = require('../collector')
3 3
 
4 4
 var files = {
5  
-  'accesslog': __dirname + '/assets/access.log'
  5
+  'accesslog': '__loremipsum'
6 6
 };
7 7
 
8 8
 var coll = new Collector(files, 'http://localhost:9069/loggregator', 'blah');

0 notes on commit 35dcbef

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