Last active
August 29, 2015 14:06
-
-
Save CatTail/7c3248d8d596ed8e452b to your computer and use it in GitHub Desktop.
Simple apache nginx parser with NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var patterns = { | |
'[': '\\[([^\\]]+)\\]', | |
'![': '[^\\[]+', | |
'"': '"([^"]+)"', | |
'!"': '[^"]+', | |
'default': '(\\S+)', // default pattern | |
'!default': '\\s+' | |
}; | |
function Parser(format) { | |
var self = this; | |
self.format = format; | |
self.position = []; | |
self.pattern = self.format.split(' ').reduce(function(pre, cur, index) { | |
var key, variable = cur; | |
key = patterns[cur[0]] ? | |
(variable = variable.slice(1, -1), cur[0]) : | |
'default'; | |
pre += (index === 0 ? '^' : patterns['!' + key]) + patterns[key]; | |
self.position[index] = variable; | |
return pre; | |
}, '', self); | |
self.regexp = new RegExp(self.pattern); | |
} | |
Parser.prototype.match = function(text) { | |
return text.match(this.regexp).slice(1); | |
}; | |
Parser.prototype.parse = function(text) { | |
var log = {}; | |
this.match(text).forEach(function(match, index) { | |
log[this.position[index]] = match; | |
}, this); | |
return log; | |
}; | |
module.exports = function(pattern) { | |
return new Parser(pattern); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[nginx.example.com] xxx.xxx.xxx.xxx - - [13/Sep/2012:11:24:12 +0800] 0.022 6083127088 "POST /search/ HTTP/1.1" 200 547 "http://nt.example.com/category/all" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36" "key=value; key2=value2" nt.example.com xx.xx.xx.xxx:80 0.022 http |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var debug = require('debug')('test:parser'); | |
var createParser = require('../lib/parser'); | |
describe('parser', function() { | |
var fields = ['remote_addr', 'remote_user', 'time_local', 'request_time', | |
'connection', 'request', 'status', 'body_bytes_sent', 'http_referer', | |
'http_user_agent', 'http_cookie', 'host', 'upstream_addr', | |
'upstream_response_time' | |
]; | |
var format = '[remote_addr] remote_user [time_local] request_time connection "request" status body_bytes_sent "http_referer" "http_user_agent" "http_cookie" host upstream_addr upstream_response_time'; | |
var sample = fs.readFileSync(__dirname + '/request.txt', 'utf8'); | |
var parser; | |
beforeEach(function() { | |
parser = createParser(format); | |
}); | |
it('should create pattern', function() { | |
debug('pattern', parser.pattern); | |
debug('regexp', parser.regexp); | |
parser.should.have.properties('pattern', 'regexp'); | |
}); | |
it('should create placeholder variable position map', function() { | |
debug('position', parser.position); | |
parser.should.have.property('position'); | |
}); | |
it('should match group', function() { | |
var matches = parser.match(sample); | |
debug('matches', matches); | |
matches.length.should.equal(fields.length); | |
}); | |
it('should parse nginx log format', function() { | |
var log = parser.parse(sample); | |
debug('log', log); | |
log.should.have.properties(fields); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment