From 6964c827bc87bd6f1db5b93b95eabc718a6e9d00 Mon Sep 17 00:00:00 2001 From: Kay Date: Wed, 14 Feb 2024 20:11:32 +0000 Subject: [PATCH] feat(logger): adding file_only option --- config/example_config.toml | 4 ++++ util/logger/config.go | 2 ++ util/logger/logger.go | 12 +++++++----- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/config/example_config.toml b/config/example_config.toml index 75d0741fc..119a28e23 100644 --- a/config/example_config.toml +++ b/config/example_config.toml @@ -119,6 +119,10 @@ # Default is `true`. ## compress = true + # `file_only` determines if the logs only get written on file. + # Default is `false`. + ## file_only = false + # `logger.levels` contains the level of logger per module. # Available log levels are: # "trace", "debug", "info", "warn", and "error". diff --git a/util/logger/config.go b/util/logger/config.go index ec3bb6972..505a65cd7 100644 --- a/util/logger/config.go +++ b/util/logger/config.go @@ -5,6 +5,7 @@ type Config struct { MaxBackups int `toml:"max_backups"` RotateLogAfterDays int `toml:"rotate_log_after_days"` Compress bool `toml:"compress"` + FileOnly bool `toml:"file_only"` Levels map[string]string `toml:"levels"` } @@ -15,6 +16,7 @@ func DefaultConfig() *Config { MaxBackups: 0, RotateLogAfterDays: 1, Compress: true, + FileOnly: false, } conf.Levels["default"] = "info" diff --git a/util/logger/logger.go b/util/logger/logger.go index d1711d786..222e94de9 100644 --- a/util/logger/logger.go +++ b/util/logger/logger.go @@ -80,11 +80,13 @@ func InitGlobalLogger(conf *Config) { } writers = append(writers, fw) - // console writer - if conf.Colorful { - writers = append(writers, zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "15:04:05"}) - } else { - writers = append(writers, os.Stderr) + if !conf.FileOnly { + // console writer + if conf.Colorful { + writers = append(writers, zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: "15:04:05"}) + } else { + writers = append(writers, os.Stderr) + } } globalInst = &logger{