3 Commits
0.2.2 ... 0.2.4

2 changed files with 18 additions and 11 deletions

View File

@ -1,6 +1,6 @@
# Package # Package
version = "0.2.2" version = "0.2.4"
author = "Jonathan Bernard" author = "Jonathan Bernard"
description = "Small utility to pretty-print strucutured logs." description = "Small utility to pretty-print strucutured logs."
license = "MIT" license = "MIT"
@ -10,5 +10,5 @@ bin = @["slfmt"]
# Dependencies # Dependencies
requires @["nim >= 2.2.0", "docopt"] requires @["nim >= 2.2.0", "docopt >= 0.7.1"]
requires @["timeutils", "zero_functional"] requires @["timeutils", "zero_functional"]

View File

@ -4,7 +4,7 @@ import docopt, timeutils, zero_functional
from std/logging import Level from std/logging import Level
from std/sequtils import toSeq from std/sequtils import toSeq
const VERSION = "0.2.2" const VERSION = "0.2.4"
const USAGE = """Usage: const USAGE = """Usage:
slfmt [options] slfmt [options]
@ -64,11 +64,11 @@ proc fullFormatField(name: string, value: JsonNode): string =
let valLines = splitLines(strVal) let valLines = splitLines(strVal)
if name.len + strVal.len + 6 > terminalWidth() or valLines.len > 1: if name.len + strVal.len + 6 > terminalWidth() or valLines.len > 1:
result &= "\n" & valLines.mapIt(" " & it).join("\n") & "\n" result &= "\p" & valLines.mapIt(" " & it).join("\p") & "\p"
else: result &= strVal & "\n" else: result &= strVal & "\p"
proc fullFormat(logJson: JsonNode): string = proc fullFormat(logJson: JsonNode): string =
result = '-'.repeat(terminalWidth()) & "\n" result = '-'.repeat(terminalWidth()) & "\p"
# Print the known fields in order first # Print the known fields in order first
for f in fieldDisplayOrder: for f in fieldDisplayOrder:
@ -79,7 +79,7 @@ proc fullFormat(logJson: JsonNode): string =
# Print the rest of the fields # Print the rest of the fields
for (key, val) in pairs(logJson): result &= fullFormatField(key, val) for (key, val) in pairs(logJson): result &= fullFormatField(key, val)
result &= "\n" result &= "\p"
proc compactFormat(logJson: JsonNode): string = proc compactFormat(logJson: JsonNode): string =
let ts = parseIso8601(logJson["ts"].getStr).local.formatIso8601 let ts = parseIso8601(logJson["ts"].getStr).local.formatIso8601
@ -98,19 +98,26 @@ proc compactFormat(logJson: JsonNode): string =
filter(not ["level", "scope", "ts", "msg"].contains(it[0])) filter(not ["level", "scope", "ts", "msg"].contains(it[0]))
let restMsg = join(restNodes --> let restMsg = join(restNodes -->
map("$1: $2" % [decorate(it[0], fgCyan), it[1].getStr]), " ") map("$1: $2" % [
decorate(it[0], fgCyan),
if it[1].kind == JString: it[1].getStr
else: pretty(it[1]),
" "]))
if restMsg.len + result.len + 2 < terminalWidth(): if restMsg.len + result.len + 2 < terminalWidth():
result &= " " & restMsg result &= " " & restMsg
else: else:
var line = " " var line = " "
for (key, val) in restNodes: for (key, val) in restNodes:
let field = "$1: $2" % [decorate(key, fgCyan), val.getStr] let fieldVal =
if val.kind == JString: val.getStr
else: pretty(val)
let field = "$1: $2" % [decorate(key, fgCyan), fieldVal]
if line.len + field.len + 2 > terminalWidth(): if line.len + field.len + 2 > terminalWidth():
result &= "\n " & line result &= "\p " & line
line = " " line = " "
line &= field & " " line &= field & " "
result &= "\n " & line result &= "\p " & line
proc parseLogLine(logLine: string): JsonNode = proc parseLogLine(logLine: string): JsonNode =
result = parseJson(logLine) result = parseJson(logLine)