From 11cc9c9a395c1225c25e79ccf04cf9d38e5473d8 Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Mon, 20 Jan 2025 21:24:22 -0600 Subject: [PATCH] Fix bug when a line happens to parse as JSON but isn't an object. For example, if `false` happened to be the only value on a line, this would be parsed as valid JSON (a boolean) but it is not a structured log message that slfmt should try to parse and format. --- slfmt.nimble | 2 +- src/slfmt.nim | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/slfmt.nimble b/slfmt.nimble index f508864..64ab93e 100644 --- a/slfmt.nimble +++ b/slfmt.nimble @@ -1,6 +1,6 @@ # Package -version = "0.2.0" +version = "0.2.1" author = "Jonathan Bernard" description = "Small utility to pretty-print strucutured logs." license = "MIT" diff --git a/src/slfmt.nim b/src/slfmt.nim index 11f7e48..3728017 100644 --- a/src/slfmt.nim +++ b/src/slfmt.nim @@ -4,7 +4,7 @@ import docopt, timeutils, zero_functional from std/logging import Level from std/sequtils import toSeq -const VERSION = "0.2.0" +const VERSION = "0.2.1" const USAGE = """Usage: slfmt [options] @@ -134,6 +134,8 @@ when isMainModule: while(sin.readLine(line)): try: let logJson = parseLogLine(line) + if logJson.kind != JObject: + raise newException(JsonParsingError, "Expected a JSON object") if logLevel.isSome and logJson.hasKey("level"): let lvl = parseLogLevel(logJson["level"].getStr)