From ae46f4dc2fdd8ef369c0d2c82c8fa938c9de91c9 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 1 Nov 2022 14:05:40 -0600 Subject: [PATCH] add support for logging topics (#293) * add support for logging topics * add top level codex topic --- codex.nim | 3 +++ codex/conf.nim | 27 ++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/codex.nim b/codex.nim index 1003d21a..01ed5459 100644 --- a/codex.nim +++ b/codex.nim @@ -24,6 +24,9 @@ when isMainModule: import ./codex/utils/fileutils + logScope: + topics = "codex" + when defined(posix): import system/ansi_c diff --git a/codex/conf.nim b/codex/conf.nim index 57008153..1cde715b 100644 --- a/codex/conf.nim +++ b/codex/conf.nim @@ -18,6 +18,7 @@ import std/strutils import std/typetraits import pkg/chronicles +import pkg/chronicles/helpers import pkg/chronicles/topics_registry import pkg/confutils/defs import pkg/confutils/std/net @@ -46,9 +47,9 @@ type CodexConf* = object logLevel* {. - defaultValue: LogLevel.INFO + defaultValue: "INFO" desc: "Sets the log level", - name: "log-level" }: LogLevel + name: "log-level" }: string logFormat* {. hidden @@ -251,6 +252,19 @@ proc stripAnsi(v: string): string = 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) = when defaultChroniclesStream.outputs.type.arity != 2: warn "Logging configuration options not enabled in the current build" @@ -286,7 +300,14 @@ proc setupLogging*(conf: CodexConf) = of LogKind.None: 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) = if config.metricsEnabled: