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 }