Reputation: 7103
I have a log file where events are in multiple lines. In order to make event into single line, first I have to separate lines that contain date and lines from those that are without date. Now I am trying to write a logic to check a line and if it doesn't have date, merge it with prevLine
.
How can I combine multiples lines into one using regular expression or any other module that helps to achieve this task?
ctrl.js
var regex = /\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\w+\]/;
var prevLine;
var readStream = fs.createReadStream(dir + '/' + logFile,'utf8');
readStream.pipe(split()).on('data', function (line) {
if (regex.test(line)) {
console.log('Line with date:', line);
parseLog(line,prevLine);
} else {
console.log('Line without date:', line);
line = prevLine + line;
}
function parseLog(line, prev) {
if (line.indexOf('|') === -1) line = prev + line;
}
});
fileData
[2017-03-23T18:13:16Z]|verbose|bmid: n/a|infra.topicWorkers|topology changed, emitting topology event { newTopology:
[ '-0000001337',
'-0000001338',
'-0000001339',
'-0000001340',
'-0000001341',
'-0000001342' ],
oldTopology:
[ '-0000001337',
'-0000001338',
'-0000001339',
'-0000001340',
'-0000001341' ],
workerId: 6,
pid: 30488 }
[2017-03-23T18:13:16Z]|verbose|bmid: n/a|infra.topicWorkers|topology changed, emitting topology event { newTopology:
[ '-0000001337',
'-0000001338',
'-0000001339',
'-0000001340',
'-0000001341',
'-0000001342' ],
oldTopology: [],
workerId: 4,
pid: 30481 }
Upvotes: 0
Views: 358
Reputation: 111336
You can do something like this:
const split = require('split');
const regex = /\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\w+\]/;
let items = [];
let item = [];
fs.createReadtStream(path.join(dir, logFile), 'utf8')
.pipe(split()).on('data', (line) => {
if (regex.test(line)) {
item = [];
items.push(item);
}
item.push(line);
});
let lines = items.map(item => item.join(' '));
lines.forEach(line => console.log(line));
You can add a new line to an array every time there is a new line, but when there is a line with a date then you can put that array into another array and create a new array for those single lines. Then you can combine the elements within the inner arrays by joining them and you will have a large array of combined lines - the array called lines
in that example.
Upvotes: 1