common-log-format
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -6,3 +6,3 @@ "use strict"; | ||
module.exports = Clf; | ||
function Clf(options){ | ||
@@ -15,17 +15,21 @@ if (!(this instanceof Clf)) return new Clf(options); | ||
Clf.prototype._transform = function(chunk, enc, done){ | ||
if (chunk === null) throw "WHUT"; | ||
var self = this; | ||
var input = chunk.toString(); | ||
var re = /([^ ]*) ([^ ]*) ([^ ]*) \[([^\]]*)\] "([^"]*)" ([^ ]*) ([^ ]*)/; | ||
var matches = input.match(re); | ||
var obj = { | ||
remoteHost: matches[1], | ||
remoteLogName: matches[2], | ||
authUser: matches[3], | ||
date: new Date(matches[4]), | ||
request: matches[5], | ||
status: Number(matches[6]), | ||
bytes: Number(matches[7]) | ||
}; | ||
this.push(JSON.stringify(obj)); | ||
input.split(/\n/).forEach(function(line){ | ||
var re = /([^ ]*) ([^ ]*) ([^ ]*) \[([^\]]*)\] "([^"]*)" ([^ ]*) ([^ ]*)/; | ||
var matches = line.match(re); | ||
if (matches){ | ||
var obj = { | ||
remoteHost: matches[1], | ||
remoteLogName: matches[2], | ||
authUser: matches[3], | ||
date: new Date(matches[4]), | ||
request: matches[5], | ||
status: Number(matches[6]), | ||
bytes: Number(matches[7]) | ||
}; | ||
self.push(JSON.stringify(obj)); | ||
} | ||
}); | ||
done(); | ||
} |
{ | ||
"name": "common-log-format", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "A transform stream, converting common log format to JSON", | ||
@@ -6,0 +6,0 @@ "repository": "https://github.com/75lb/common-log-format.git", |
@@ -7,6 +7,5 @@ [![view on npm](http://img.shields.io/npm/v/common-log-format.svg)](https://www.npmjs.org/package/common-log-format) | ||
#common-log-format | ||
Pipe the [common log format](http://en.wikipedia.org/wiki/Common_Log_Format) in, get JSON out. Useful for converting web logs into a format more readily consumed by a node.js app. | ||
Pipe in [common log format](http://en.wikipedia.org/wiki/Common_Log_Format), get JSON out. Useful for converting web logs into a format more readily consumed by Javascript. | ||
##Example | ||
Standard log input such as: | ||
Standard log input such as: | ||
@@ -18,5 +17,5 @@ ``` | ||
would be converted to | ||
would be converted to | ||
```json | ||
{"remoteHost":"127.0.0.1","remoteLogName":"-","authUser":"-","date":"2014-06-11T16:24:02.000Z","request":"GET / HTTP/1.1","status":200,"bytes":10305} | ||
{"remoteHost":"127.0.0.1","remoteLogName":"-","authUser":"-","date":"2014-06-11T16:24:02.000Z","request":"GET / HTTP/1.1","status":200,"bytes":10305}{"remoteHost":"127.0.0.1","remoteLogName":"-","authUser":"-","date":"2014-06-11T16:24:08.000Z","request":"GET /package.json HTTP/1.1","status":304,"bytes":null} | ||
``` | ||
@@ -43,3 +42,3 @@ | ||
$ cat my-web-log.txt | clf | ||
{"remoteHost":"127.0.0.1","remoteLogName":"-","authUser":"-","date":"2014-06-11T16:05:26.000Z","request":"GET /package.json HTTP/1.1","status":200,"bytes":733}{"remoteHost":"127.0.0.1","remoteLogName":"-","authUser":"-","date":"2014-06-11T16:05:26.000Z","request":"GET /package.json HTTP/1.1","status":200,"bytes":733}{"remoteHost":"127.0.0.1","remoteLogName":"-","authUser":"-","date":"2014-06-11T16:05:26.000Z","request":"GET /package.json HTTP/1.1","status":200,"bytes":733}{"remoteHost":"127.0.0.1","remoteLogName":"-","authUser":"-","date":"2014-06-11T16:05:27.000Z","request":"GET /package.json HTTP/1.1","status":200,"bytes":733}{"remoteHost":"127.0.0.1","remoteLogName":"-","authUser":"-","date":"2014-06-11T16:05:27.000Z","request":"GET /package.json HTTP/1.1","status":200,"bytes":733} | ||
{"remoteHost":"127.0.0.1","remoteLogName":"-","authUser":"-","date":"2014-06-11T16:05:26.000Z","request":"GET /package.json HTTP/1.1","status":200,"bytes":733} | ||
``` |
@@ -22,1 +22,40 @@ var test = require("tape"); | ||
}); | ||
test("connect default, twice", function(t){ | ||
t.plan(2); | ||
var transform = new Clf(); | ||
transform.on("readable", function(){ | ||
var json = this.read(); | ||
logObject = JSON.parse(json); | ||
t.deepEqual(logObject, { | ||
remoteHost: "127.0.0.1", | ||
remoteLogName: "-", | ||
authUser: "-", | ||
date: "2014-06-11T15:51:48.000Z", | ||
request: "GET /package.json HTTP/1.1", | ||
status: 200, | ||
bytes: 733 | ||
}); | ||
}); | ||
transform.write('127.0.0.1 - - [Wed, 11 Jun 2014 15:51:48 GMT] "GET /package.json HTTP/1.1" 200 733 "http://localhost:8000/" "userAgent"'); | ||
transform.write('127.0.0.1 - - [Wed, 11 Jun 2014 15:51:48 GMT] "GET /package.json HTTP/1.1" 200 733 "http://localhost:8000/" "userAgent"'); | ||
}); | ||
test("two lines, one write", function(t){ | ||
t.plan(2); | ||
var transform = new Clf(); | ||
transform.on("readable", function(){ | ||
var json = this.read(); | ||
logObject = JSON.parse(json); | ||
t.deepEqual(logObject, { | ||
remoteHost: "127.0.0.1", | ||
remoteLogName: "-", | ||
authUser: "-", | ||
date: "2014-06-11T15:51:48.000Z", | ||
request: "GET /package.json HTTP/1.1", | ||
status: 200, | ||
bytes: 733 | ||
}); | ||
}); | ||
transform.write('127.0.0.1 - - [Wed, 11 Jun 2014 15:51:48 GMT] "GET /package.json HTTP/1.1" 200 733 "http://localhost:8000/" "userAgent"\n127.0.0.1 - - [Wed, 11 Jun 2014 15:51:48 GMT] "GET /package.json HTTP/1.1" 200 733 "http://localhost:8000/" "userAgent"'); | ||
}); |
6252
89
42