add support for logging topics (#293)

* add support for logging topics

* add top level codex topic
This commit is contained in:
Dmitriy Ryajov 2022-11-01 14:05:40 -06:00 committed by GitHub
parent 6e4a8b86ab
commit ae46f4dc2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 3 deletions

View File

@ -24,6 +24,9 @@ when isMainModule:
import ./codex/utils/fileutils import ./codex/utils/fileutils
logScope:
topics = "codex"
when defined(posix): when defined(posix):
import system/ansi_c import system/ansi_c

View File

@ -18,6 +18,7 @@ import std/strutils
import std/typetraits import std/typetraits
import pkg/chronicles import pkg/chronicles
import pkg/chronicles/helpers
import pkg/chronicles/topics_registry import pkg/chronicles/topics_registry
import pkg/confutils/defs import pkg/confutils/defs
import pkg/confutils/std/net import pkg/confutils/std/net
@ -46,9 +47,9 @@ type
CodexConf* = object CodexConf* = object
logLevel* {. logLevel* {.
defaultValue: LogLevel.INFO defaultValue: "INFO"
desc: "Sets the log level", desc: "Sets the log level",
name: "log-level" }: LogLevel name: "log-level" }: string
logFormat* {. logFormat* {.
hidden hidden
@ -251,6 +252,19 @@ proc stripAnsi(v: string): string =
res res
proc updateLogLevel*(logLevel: string) {.raises: [Defect, ValueError].} =
# Updates log levels (without clearing old ones)
let directives = logLevel.split(";")
try:
setLogLevel(parseEnum[LogLevel](directives[0]))
except ValueError:
raise (ref ValueError)(msg: "Please specify one of TRACE, DEBUG, INFO, NOTICE, WARN, ERROR or FATAL")
if directives.len > 1:
for topicName, settings in parseTopicDirectives(directives[1..^1]):
if not setTopicState(topicName, settings.state, settings.logLevel):
warn "Unrecognized logging topic", topic = topicName
proc setupLogging*(conf: CodexConf) = proc setupLogging*(conf: CodexConf) =
when defaultChroniclesStream.outputs.type.arity != 2: when defaultChroniclesStream.outputs.type.arity != 2:
warn "Logging configuration options not enabled in the current build" warn "Logging configuration options not enabled in the current build"
@ -286,7 +300,14 @@ proc setupLogging*(conf: CodexConf) =
of LogKind.None: of LogKind.None:
noOutput noOutput
setLogLevel(conf.logLevel) try:
updateLogLevel(conf.logLevel)
except ValueError as err:
try:
stderr.write "Invalid value for --log-level. " & err.msg & "\n"
except IOError:
echo "Invalid value for --log-level. " & err.msg
quit QuitFailure
proc setupMetrics*(config: CodexConf) = proc setupMetrics*(config: CodexConf) =
if config.metricsEnabled: if config.metricsEnabled: