Permalink
Browse files

Now with colored log messages

  • Loading branch information...
1 parent 09b0598 commit 35dcbef94b3bf984a44e36b577e01996c1a4bde8 @ryanrolds committed Apr 1, 2012
@@ -49,7 +49,7 @@ Collector.prototype.removeWatcher = function(toUnwatch, monitor, callback) {
return callback('Unknown watchable');
}
- var watchable = this.watchables[toUnwatch]
+ var watchable = this.watchables[toUnwatch];
var pos = watchable.watchers.indexOf(monitor);
if(pos === -1) {
@@ -1,34 +1,35 @@
-#list {
+#messages {
+
+}
+
+#collectors {
width: 200px;
- position: absolute;
- right: 0px;
+ float: right;
+ height: 100%;
+ margin-left: 10px;
}
-#list .collector {
+.collector {
}
-#list .collector > .name {
+.collector > .name {
height: 20px;
background-color: #EEE;
font-size: 18px;
padding: 5px;
}
-#list .watchables {
+.watchables {
font-size: 14px;
margin-left: 15px;
}
-#list .watchable {
+.watchable {
padding: 5px 0 5px 5px;
}
.clickable {
cursor: pointer;
-}
-
-#messages {
-
}
@@ -4,7 +4,7 @@ var Collector = function(monitor, id, watchables) {
this.id = id;
this.watchables = watchables;
this.ui = {};
- this.active = [];
+ this.active = {};
};
Collector.prototype.createUI = function() {
@@ -63,13 +63,13 @@ Collector.prototype.createWatchable = function(id) {
$(item).click({'collector': this, 'watchable': id}, function(e) {
var collector = e.data.collector;
var watchable = e.data.watchable;
- var pos = $.inArray(watchable, collector.active);
- if(pos === -1) {
- collector.active.push(watchable);
+
+ if(!collector.active[watchable]) {
+ collector.active[watchable] = collector.parent.getColor();
collector.watch(e.data.watchable);
- e.currentTarget.parentNode.style.backgroundColor = collector.parent.getColor();
+ e.currentTarget.parentNode.style.backgroundColor = collector.active[watchable];
} else {
- collector.active.splice(pos, 1);
+ delete collector.active[watchable];
collector.unwatch(e.data.watchable);
e.currentTarget.parentNode.style.backgroundColor = 'transparent';
}
@@ -2,22 +2,28 @@
var Monitor = function(url) {
this.url = url;
this.ui = {};
+ this.collectors = {};
this.setupUI();
this.setupSocketIO();
};
Monitor.prototype.setupUI = function() {
- var list = document.createElement('div');
- list.id = 'list';
- this.ui.list = list;
+ var collectors = document.createElement('div');
+ collectors.id = 'collectors';
+ this.ui.collectors = collectors;
var messages = document.createElement('div');
messages.id = 'messages';
+ this.ui.messages = messages;
+
+ var clear = document.createElement('div');
+ clear.style.clear = 'both';
var main = document.getElementById('loggregator');
- main.appendChild(list);
+ main.appendChild(collectors);
main.appendChild(messages);
+ main.appendChild(clear);
};
Monitor.prototype.setupSocketIO = function() {
@@ -41,23 +47,56 @@ Monitor.prototype.setupSocketIO = function() {
};
});
});
+
+ socket.on('data', function(data) {
+ var collector = that.getCollector(data.hostname);
+
+ var message = document.createElement('div');
+ message.className = 'message';
+ message.style.color = collector.active[data.watchable];
+
+ var time = document.createElement('span');
+ time.className = 'time';
+ time.appendChild(document.createTextNode(data.date));
+ var host = document.createElement('span');
+ host.className = 'hostname';
+ host.appendChild(document.createTextNode(['[', data.hostname, ']'].join('')));
+ var log = document.createElement('span');
+ log.className = 'log';
+ log.appendChild(document.createTextNode(data.lines));
+
+ message.appendChild(time);
+ message.appendChild(host);
+ message.appendChild(log);
+
+ that.ui.messages.appendChild(message);
+ });
});
this.socket = socket;
};
Monitor.prototype.addCollector = function(id, watchables) {
var collector = new Collector(this, id, watchables);
- this.ui.list.appendChild(collector.createUI());
+ this.collectors[id] = collector;
+
+ this.ui.collectors.appendChild(collector.createUI());
+};
+
+Monitor.prototype.getCollector = function(id) {
+ var c = this.collectors[id];
+ return (c) ? c : undefined;
};
Monitor.prototype.watch = function(id, watchable) {
var data = {
'hostname': id,
'watchable': watchable
};
- this.socket.emit('watch', data, function() {
-
+ this.socket.emit('watch', data, function(error, response) {
+ if(error) {
+ throw new Error(error);
+ }
});
};
@@ -66,8 +105,10 @@ Monitor.prototype.unwatch = function(id, watchable) {
'hostname': id,
'watchable': watchable
};
- this.socket.emit('unwatch', data, function() {
-
+ this.socket.emit('unwatch', data, function(error, response) {
+ if(error) {
+ throw new Error(error);
+ }
});
};
View
@@ -2,6 +2,7 @@
var os = require('os');
var io = require('socket.io-client');
var FileNotify = require('filenotify');
+var LoremIpStream = require('loremipstream');
var Collector = function Collector(files, url, key, callback) {
var that = this;
@@ -19,8 +20,21 @@ var Collector = function Collector(files, url, key, callback) {
// Start monitoring of a file/stream
conn.on('watch', function(data, callback) {
+ console.log('watch', arguments);
if(!watchers[data.file]) {
- watchers[data.file] = new FileNotify(files[data.file]);
+ var file = files[data.file];
+ if(file !== '__loremipsum') {
+ watchers[data.file] = new FileNotify(files[file]);
+ } else {
+ var lorem = new LoremIpStream({
+ dataSize: 100,
+ dataInterval: 1000
+ });
+ lorem.setEncoding('utf8');
+
+ watchers[data.file] = lorem;
+ }
+
watchers[data.file].on('data', function(lines) {
that.conn.emit('data', {
'date': new Date().toUTCString(),
@@ -39,6 +53,7 @@ var Collector = function Collector(files, url, key, callback) {
conn.on('unwatch', function(data, callback) {
if(watchers[data.file]) {
watchers[data.file].destroy();
+ delete watchers[data.file];
}
if(callback) {
View
@@ -9,7 +9,8 @@
"dependencies": {
"async": "",
"socket.io-client": "",
- "filenotify": ""
+ "filenotify": "",
+ "loremipstream": ""
},
"devDependencies": {
"mocha": "",
View
@@ -2,7 +2,7 @@
var Collector = require('../collector')
var files = {
- 'accesslog': __dirname + '/assets/access.log'
+ 'accesslog': '__loremipsum'
};
var coll = new Collector(files, 'http://localhost:9069/loggregator', 'blah');

0 comments on commit 35dcbef

Please sign in to comment.