From d98618a934fe9d4a1e3f7ab3166f791c4d8a9bc5 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sat, 25 Apr 2020 03:24:13 +1200 Subject: [PATCH] Don't crash if log line isn't understood --- scripts/process_log.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/scripts/process_log.js b/scripts/process_log.js index 12c8a3c..09ff15e 100644 --- a/scripts/process_log.js +++ b/scripts/process_log.js @@ -89,14 +89,20 @@ const regex = /^([^ ]+) - - \[([^\]]+)\] "([A-Z]+) ([^"]+) HTTP\/(?:1.0|1.1|2.0) function parseLine(line) { const result = line.match(regex) - return { - ip: result[1], - date: result[2], - method: result[3], - path: result[4], - status: result[5], - bytes: +result[6], - userAgent: result[7] + if (!result) { + console.log("Line didn't match regular expression:") + console.log(line) + return null + } else { + return { + ip: result[1], + date: result[2], + method: result[3], + path: result[4], + status: result[5], + bytes: +result[6], + userAgent: result[7] + } } } @@ -124,6 +130,7 @@ let dateCollection = null reader.on("line", line => { const parsed = parseLine(line) + if (!parsed) return const dateObject = new Date(parsed.date.replace(":", " ")) //console.log(parsed)