Skip to content

Instantly share code, notes, and snippets.

@codeactual
Created April 14, 2012 01:11
Show Gist options
  • Save codeactual/2381268 to your computer and use it in GitHub Desktop.
Save codeactual/2381268 to your computer and use it in GitHub Desktop.
mainevent's nginx access log parser module (JS)
'use strict';
var extend = require(__dirname + '/../../prototype.js').extend;
exports.NginxAccessParser = extend({name: 'NginxAccess', humanName: 'nginx access'}, {
parse: function(log) {
// From fluentd-0.10.9/lib/fluent/parser.rb:
return this.namedCapture(log, '^(?<host>[^ ]*) [^ ]+ (?<user>[^ ]+) \\[(?<time>[^\\]]+)\\] "(?<method>\\S+) (?<path>[^ ]+) \\S+ (?<code>[^ ]+) (?<size>[^ ]+) ("(?<referer>[^\\"]+)")? ("(?<agent>[^\\"]+)")?$');
},
buildTemplateContext: function(template, log) {
if (log.code) {
switch (log.code.toString()[0]) {
case '2': log.__codeClass = 'success'; break;
case '3': log.__codeClass = 'info'; break;
case '4': log.__codeClass = 'important'; break;
case '5': log.__codeClass = 'important'; break;
}
}
if (log.referer) {
log.referer = '-' == log.referer ? '' : log.referer;
log.__refererMin = log.referer.replace(/^http(s)?:\/\//, '');
}
return log;
},
extractTime: function(log) {
var matches = log.time.match(/^(\d+)\/([A-Za-z]+)\/(\d{4}):(\d{2}:\d{2}:\d{2} \+\d{4})/);
if (!matches) {
return NaN;
}
var parsable = util.format(
'%d/%d/%d %s',
mainevent.shared.Date.monthNameToNum(matches[2]),
matches[1],
matches[3],
matches[4]
);
return Date.parse(parsable);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment