Skip to content

Instantly share code, notes, and snippets.

@aleung
Created November 2, 2016 11:30
Show Gist options
  • Save aleung/297b84fb38e6eedd3d1148ab04cf5a5b to your computer and use it in GitHub Desktop.
Save aleung/297b84fb38e6eedd3d1148ab04cf5a5b to your computer and use it in GitHub Desktop.
Merge consul agent logs which are fetched by Ansible
#!/usr/bin/env node
"use strict";
const fs = require('fs');
const readline = require('readline');
const logLineRegexp = /\s+(.+?)\[(.+?)\](.*)/;
function processFile(file, host) {
const lineReader = readline.createInterface({
input: fs.createReadStream(file)
});
lineReader.on('line', (line) => {
const matches = line.match(logLineRegexp);
if (matches) {
console.log(matches[1], '|', host, '|', matches[2], '|', matches[3]);
}
});
}
const filenameRegexp = /agent-\d+\.\d+\.\d+\.(\d+)\.log/;
fs.readdir('.', (err, files) => {
for (let file of files) {
const matches = file.match(filenameRegexp);
if (matches) {
const host = matches[1];
processFile(file, host);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment