118 lines
3.5 KiB
Nim
118 lines
3.5 KiB
Nim
import std/[json, options, sequtils, streams, strutils, terminal, times]
|
|
import timeutils
|
|
import docopt
|
|
|
|
from std/logging import Level
|
|
|
|
const VERSION = "0.1.2"
|
|
|
|
const USAGE = """Usage:
|
|
slfmt [options]
|
|
|
|
Options:
|
|
|
|
-h, --help Print this usage and help information
|
|
-l, --log-level <lvl> Only show log events at or above this level
|
|
-n, --namespace <ns> Only show log events from this namespace
|
|
"""
|
|
|
|
const fieldDisplayOrder = @[
|
|
"scope", "level", "ts", "code", "sid", "sub", "msg", "err", "stack", "method", "args"]
|
|
|
|
func parseLogLevel(s: string): Level =
|
|
case s.toUpper
|
|
of "DEBUG": result = Level.lvlDebug
|
|
of "INFO": result = Level.lvlInfo
|
|
of "NOTICE": result = Level.lvlNotice
|
|
of "WARN": result = Level.lvlWarn
|
|
of "ERROR": result = Level.lvlError
|
|
of "FATAL": result = Level.lvlFatal
|
|
else: result = Level.lvlAll
|
|
|
|
func decorate(
|
|
s: string,
|
|
fg = fgDefault,
|
|
style: set[Style] = {}): string =
|
|
|
|
result = ""
|
|
|
|
if style != {}:
|
|
result &= toSeq(items(style)).mapIt(ansiStyleCode(it)).join("")
|
|
|
|
if fg != fgDefault: result &= ansiForegroundColorCode(fg)
|
|
|
|
result &= s & ansiResetCode
|
|
|
|
|
|
proc formatField(name: string, value: JsonNode): string =
|
|
result = decorate(name, fgCyan) & ":" & " ".repeat(max(1, 10 - name.len))
|
|
|
|
var strVal: string = ""
|
|
case name:
|
|
of "ts":
|
|
let dt = parseIso8601(value.getStr)
|
|
strVal = decorate(dt.local.formatIso8601 & " (local) ", fgBlue, {styleBright}) &
|
|
dt.utc.formatIso8601 & " (UTC)"
|
|
of "sid", "sub": strVal = decorate(value.getStr, fgGreen)
|
|
of "err": strVal = decorate(value.getStr, fgRed)
|
|
of "msg": strVal = decorate(value.getStr, fgYellow)
|
|
of "stack": strVal = decorate(value.getStr, fgBlack, {styleBright})
|
|
else:
|
|
if value.kind == JString: strVal = decorate(value.getStr)
|
|
else: strVal = pretty(value)
|
|
|
|
let valLines = splitLines(strVal)
|
|
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 prettyPrintFormat(logJson: JsonNode): string =
|
|
result = '-'.repeat(terminalWidth()) & "\n"
|
|
|
|
# Print the known fields in order first
|
|
for f in fieldDisplayOrder:
|
|
if logJson.hasKey(f):
|
|
result &= formatField(f, logJson[f])
|
|
logJson.delete(f)
|
|
|
|
# Print the rest of the fields
|
|
for (key, val) in pairs(logJson): result &= formatField(key, val)
|
|
|
|
result &= "\n"
|
|
|
|
proc parseLogLine(logLine: string): JsonNode =
|
|
result = parseJson(logLine)
|
|
|
|
when isMainModule:
|
|
try:
|
|
let args = docopt(USAGE, version = VERSION)
|
|
|
|
let logLevel =
|
|
if args["--log-level"]: some(parseLogLevel($args["--log-level"]))
|
|
else: none[Level]()
|
|
|
|
let namespace =
|
|
if args["--namespace"]: some($args["--namespace"])
|
|
else: none[string]()
|
|
|
|
var line: string = ""
|
|
let sin = newFileStream(stdin)
|
|
while(sin.readLine(line)):
|
|
try:
|
|
let logJson = parseLogLine(line)
|
|
|
|
if logLevel.isSome and logJson.hasKey("level"):
|
|
let lvl = parseLogLevel(logJson["level"].getStr)
|
|
if lvl < logLevel.get: continue
|
|
|
|
if namespace.isSome and logJson.hasKey("scope"):
|
|
if not logJson["scope"].getStr.startsWith(namespace.get): continue
|
|
|
|
stdout.writeLine(prettyPrintFormat(logJson))
|
|
except ValueError, JsonParsingError:
|
|
stdout.writeLine(line)
|
|
except:
|
|
stderr.writeLine("slfmt - FATAL: " & getCurrentExceptionMsg())
|
|
stderr.writeLine(getCurrentException().getStackTrace())
|
|
quit(QuitFailure)
|