Permalink
Browse files

[loggly] redefining JSON one day at a time

* shows really messy parsing of json
  • Loading branch information...
dscape committed Dec 6, 2012
1 parent 3510e0d commit 2dcfd6df7766be426d5d44bc5a0bead59ed9471c
Showing with 87 additions and 0 deletions.
  1. +86 −0 samples/loggly.js
  2. +1 −0 samples/loggly.txt
View
@@ -0,0 +1,86 @@
+var fs = require('fs')
+ , Stream = require('stream').Stream
+ , clarinet = require('../clarinet')
+ , parse_stream = clarinet.createStream()
+ , previous = ''
+ , buffer = {}
+ , stack = []
+ , new_thing = false
+ ;
+
+function debug_log() {
+ if(process.env.DEBUG) {
+ console.log.apply(null, arguments);
+ }
+}
+
+parse_stream.on('openobject', function(name) {
+ if(new_thing) {
+ console.log(JSON.stringify(buffer, null, 2));
+ buffer = {};
+ new_thing = false;
+ }
+ previous = name;
+ stack.push(name);
+ debug_log('=== {', name, buffer);
+});
+
+parse_stream.on('closeobject', function() {
+ stack.pop();
+ debug_log('=== }', null, buffer);
+});
+
+parse_stream.on('key', function(name) {
+ previous = name;
+ stack.pop();
+ stack.push(name);
+ debug_log('=== ,', name, buffer);
+});
+
+parse_stream.on('value', function(value) {
+ if(previous === 'event') {
+ value = JSON.parse(value);
+ }
+ var expected = stack.length-1;
+ stack.reduce(function (ac, x, i) {
+ if(i === expected) {
+ ac[x] = value;
+ }
+ ac[x] = ac[x] || {};
+ return ac[x];
+ }, buffer);
+ debug_log('=== v', value, buffer);
+});
+
+parse_stream.on('error', function (e) {
+ new_thing = true;
+});
+
+function fixLogglyStream() {
+ var log_stream = new Stream();
+ log_stream.readable = true;
+ log_stream.writable = true;
+
+ log_stream.write = function (buf) {
+ var as_string = buf.toString('utf-8').replace(/\\\\/g, '\\');
+ this.emit('data', as_string);
+ };
+
+ log_stream.end = function (buf) {
+ if (arguments.length) {
+ log_stream.write(buf);
+ }
+ log_stream.writable = false;
+ };
+
+ log_stream.destroy = function () {
+ log_stream.writable = false;
+ };
+
+ return log_stream;
+}
+
+fs.createReadStream(__dirname + '/loggly.txt')
+ .pipe(fixLogglyStream())
+ .pipe(parse_stream)
+ ;
View
@@ -0,0 +1 @@
+'\u0000\u0000\u0001:\u0019�J\u0012\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0002{"timestamp":1349049600530,"inputname":"master","event":"{\\"method\\":\\"POST\\",\\"url\\":\\"/apps/winstonf/blog/ipc\\",\\"remoteAddress\\":\\"10.112.37.158\\",\\"event\\":\\"service:incoming\\",\\"level\\":\\"info\\",\\"message\\":\\"service:incoming\\"}","ip":"165.225.130.178"}\u0000\u0000\u0001:\u0019�J\u001a\u0000\u0000\u0000\u0000\u0000\u0000\u0000�{"timestamp":1349049600538,"inputname":"master","event":"{\\"app\\":\\"blog\\",\\"username\\":\\"winstonf\\",\\"event\\":\\"app:show:begin\\",\\"level\\":\\"info\\",\\"message\\":\\"app:show:begin\\"}","ip":"165.225.130.178"}\u0000\u0000\u0001:\u0019�J\u001b\u0000\u0000\u0000\u0000\u0000\u0000\u0000�{"timestamp":1349049600539,"inputname":"master","event":"{\\"app\\":\\"blog\\",\\"username\\":\\"winstonf\\",\\"event\\":\\"app:show:success\\",\\"level\\":\\"info\\",\\"message\\":\\"app:show:success\\"}","ip":"165.225.130.178"}\u0000\u0000\u0001:\u0019�J5\u0000\u0000\u0000\u0000\u0000\u0000\u0000�{"timestamp":1349049600565,"inputname":"master","event":"{\\"app\\":\\"blogtest\\",\\"username\\":\\"winstonf\\",\\"event\\":\\"app:show:success\\",\\"level\\":\\"info\\",\\"message\\":\\"app:show:success\\"}","ip":"165.225.130.178"}\u0000\u0000\u0001:\u0019�JR\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0005{"timestamp":1349049600594,"inputname":"master","event":"{\\"method\\":\\"POST\\",\\"url\\":\\"/apps/winstonf/blogtest/ipc\\",\\"remoteAddress\\":\\"10.112.37.55\\",\\"event\\":\\"service:incoming\\",\\"level\\":\\"info\\",\\"message\\":\\"service:incoming\\"}","ip":"165.225.130.178"}\u0000\u0000\u0001:\u0019�JX\u0000\u0000\u0000\u0000\u0000\u0000\u0000�{"timestamp":1349049600600,"inputname":"master","event":"{\\"app\\":\\"blogtest\\",\\"username\\":\\"winstonf\\",\\"event\\":\\"app:show:begin\\",\\"level\\":\\"info\\",\\"message\\":\\"app:show:begin\\"}","ip":"165.225.130.178"}\u0000\u0000\u0001:\u0019�L\u001e\u0000\u0000\u0000\u0000\u0000\u0000\u0000�{"timestamp":1349049601054,"inputname":"master","event":"{\\"app\\":\\"nodeapps-helloworld\\",\\"username\\":\\"winstonf\\",\\"event\\":\\"app:show:begin\\",\\"level\\":\\"info\\",\\"message\\":\\"app:show:begin\\"}","ip":"165.225.130.178"}\u0000\u0000\u0001:\u0019�LU\u0000\u0000\u0000\u0000\u0000\u0000\u0000�{"timestamp":1349049601109,"inputname":"master","event":"{\\"app\\":\\"nodeapps-helloworld\\",\\"username\\":\\"winstonf\\",\\"event\\":\\"app:show:success\\",\\"level\\":\\"info\\",\\"message\\":\\"app:show:success\\"}","ip":"165.225.130.178"}\u0000\u0000\u0001:\u0019�LV\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0011{"timestamp":1349049601110,"inputname":"master","event":"{

0 comments on commit 2dcfd6d

Please sign in to comment.