Skip to content

Instantly share code, notes, and snippets.

@munro
Created March 29, 2012 22:58
Show Gist options
  • Save munro/2244660 to your computer and use it in GitHub Desktop.
Save munro/2244660 to your computer and use it in GitHub Desktop.
Apache Logs to Json
var fs = require('fs');
var start_fs = (new Date()).getTime();
var data = fs.readFileSync('./100_log', 'utf-8');
var start = (new Date()).getTime();
var log = data.split('\n').map(function (line) {
var match = line.match(/\[([^\]]+)\] \[([^\]]+)\] \[([^ ]+) ([^\]]+)\] (.*)/);
if (match === null) {
return null;
}
return JSON.stringify({
date: new Date(match[1]),
type: match[2],
ip: match[4],
message: match[5]
});
});
var duration = (new Date()).getTime() - start;
fs.writeFileSync('./100_log.json', log.reduce(function (prev, curr) {
if (curr === null) {
return prev;
}
return prev + curr + '\n';
}, ''), 'utf-8');
var duration_fs = (new Date()).getTime() - start_fs;
console.log('Parsed and encoded 100 lines:', duration, 'ms');
console.log('Time per line:', duration / 100, 'ms');
console.log('Read/Parsed/Encoded/Write 100 lines:', duration_fs, 'ms');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment