plapp/main.mjs

32 lines
1 KiB
JavaScript

import { createReadStream } from 'fs';
import { createInterface } from 'node:readline';
const fileToRead = process.argv[2];
// https://nodejs.org/api/readline.html#readline_example_read_file_stream_line_by_line
// TEST LOG LINE
// The logs are read line by line, and aren't put through the JS parser (aren't parsed as JS source).
// As such, for testing String.raw is indicated
const testLine = String.raw `2023-09-28T03:16:24.580872000Z {"log":"52.88.91.78 - - [28/Sep/2023:03:16:23 +0000] \"GET /api/v2/account/details HTTP/1.1\" 200 1006 \"-\" \"go-tfe\"","component":"nginx"}'`
async function processLineByLine(filename) {
const fileStream = createReadStream(filename);
const rl = createInterface({
input: fileStream,
crlfDelay: Infinity,
});
for await (const line of rl) {
console.log(JSON.parse(getLineJSON(line).log));
}
}
async function getLineJSON(line) {
return line.split(/ (?=\{)/)[1];
}
// processLineByLine(fileToRead);
console.log(await getLineJSON(testLine));