Compare commits

..

No commits in common. "main" and "0.1.2" have entirely different histories.
main ... 0.1.2

2 changed files with 12 additions and 56 deletions

View File

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

View File

@ -1,10 +1,10 @@
import std/[json, options, sequtils, streams, strutils, terminal, times]
import docopt, timeutils, zero_functional
import timeutils
import docopt
from std/logging import Level
from std/sequtils import toSeq
const VERSION = "0.2.3"
const VERSION = "0.1.2"
const USAGE = """Usage:
slfmt [options]
@ -12,7 +12,6 @@ const USAGE = """Usage:
Options:
-h, --help Print this usage and help information
-c, --compact Compact output
-l, --log-level <lvl> Only show log events at or above this level
-n, --namespace <ns> Only show log events from this namespace
"""
@ -45,7 +44,7 @@ func decorate(
result &= s & ansiResetCode
proc fullFormatField(name: string, value: JsonNode): string =
proc formatField(name: string, value: JsonNode): string =
result = decorate(name, fgCyan) & ":" & " ".repeat(max(1, 10 - name.len))
var strVal: string = ""
@ -63,62 +62,24 @@ proc fullFormatField(name: string, value: JsonNode): string =
else: strVal = pretty(value)
let valLines = splitLines(strVal)
if name.len + strVal.len + 6 > terminalWidth() or valLines.len > 1:
if name.len > 10 or strVal.len + 16 > terminalWidth() or valLines.len > 1:
result &= "\n" & valLines.mapIt(" " & it).join("\n") & "\n"
else: result &= strVal & "\n"
proc fullFormat(logJson: JsonNode): string =
proc prettyPrintFormat(logJson: JsonNode): string =
result = '-'.repeat(terminalWidth()) & "\n"
# Print the known fields in order first
for f in fieldDisplayOrder:
if logJson.hasKey(f):
result &= fullFormatField(f, logJson[f])
result &= formatField(f, logJson[f])
logJson.delete(f)
# Print the rest of the fields
for (key, val) in pairs(logJson): result &= fullFormatField(key, val)
for (key, val) in pairs(logJson): result &= formatField(key, val)
result &= "\n"
proc compactFormat(logJson: JsonNode): string =
let ts = parseIso8601(logJson["ts"].getStr).local.formatIso8601
var level = logJson["level"].getStr
if level == "ERROR": level = decorate(alignLeft(level, 7), fgRed)
elif level == "WARN": level = decorate(alignLeft(level, 7), fgYellow)
else: level = alignLeft(level, 7)
result = "$1 $2 $3: $4" % [
level,
decorate(alignLeft(ts[0..21], 23), fgBlue, {styleBright}),
decorate(logJson["scope"].getStr, fgGreen),
decorate(logJson["msg"].getStr, fgYellow)]
let restNodes = (toSeq(pairs(logJson))) -->
filter(not ["level", "scope", "ts", "msg"].contains(it[0]))
let restMsg = join(restNodes -->
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():
result &= " " & restMsg
else:
var line = " "
for (key, val) in restNodes:
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():
result &= "\n " & line
line = " "
line &= field & " "
result &= "\n " & line
proc parseLogLine(logLine: string): JsonNode =
result = parseJson(logLine)
@ -134,15 +95,11 @@ when isMainModule:
if args["--namespace"]: some($args["--namespace"])
else: none[string]()
let compact = args["--compact"]
var line: string = ""
let sin = newFileStream(stdin)
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)
@ -151,8 +108,7 @@ when isMainModule:
if namespace.isSome and logJson.hasKey("scope"):
if not logJson["scope"].getStr.startsWith(namespace.get): continue
if compact: stdout.writeLine(compactFormat(logJson))
else: stdout.writeLine(fullFormat(logJson))
stdout.writeLine(prettyPrintFormat(logJson))
except ValueError, JsonParsingError:
stdout.writeLine(line)
except: