From 541d425f3ed770ec9796e9a943a1a0ed3971d81f Mon Sep 17 00:00:00 2001 From: Song Gao Date: Wed, 8 Nov 2023 17:14:56 +0800 Subject: [PATCH] feat: support log level (#2394) Signed-off-by: yisaer --- .../configuration/global_configurations.md | 6 +++ .../configuration/global_configurations.md | 6 +++ etc/kuiper.yaml | 2 + internal/conf/conf.go | 39 +++++++++++++++---- internal/server/rest.go | 12 ++++-- 5 files changed, 54 insertions(+), 11 deletions(-) diff --git a/docs/en_US/configuration/global_configurations.md b/docs/en_US/configuration/global_configurations.md index 627e5c39a0..3dbb0c4762 100644 --- a/docs/en_US/configuration/global_configurations.md +++ b/docs/en_US/configuration/global_configurations.md @@ -8,6 +8,8 @@ Example, in case of config: ```yaml basic: + # debug | info | warn | error | fatal | panic + loglevel: info # true|false, with debug level, it prints more debug info debug: false # true|false, if it's set to true, then the log will be print to console @@ -50,6 +52,8 @@ The configuration item **ignoreCase** is used to specify whether case is ignored ```yaml basic: + # debug | info | warn | error | fatal | panic + loglevel: info # true|false, with debug level, it prints more debug info debug: false # true|false, if it's set to true, then the log will be print to console @@ -62,6 +66,8 @@ basic: maxAge: 72 ``` +When debug is false, eKuiper's log level can be controlled through logLevel. When debug is true, eKuiper's log level will be fixed to debug. + ## System log When the user sets the value of the environment variable named KuiperSyslogKey to true or set syslog enable to true, the diff --git a/docs/zh_CN/configuration/global_configurations.md b/docs/zh_CN/configuration/global_configurations.md index 0ebcbd56bc..8b87a056ae 100644 --- a/docs/zh_CN/configuration/global_configurations.md +++ b/docs/zh_CN/configuration/global_configurations.md @@ -4,6 +4,8 @@ eKuiper 的配置文件位于 `$eKuiper/etc/kuiper.yaml` 中。 配置文件为 ```yaml basic: + # debug | info | warn | error | fatal | panic + loglevel: info # true|false, with debug level, it prints more debug info debug: false # true|false, if it's set to true, then the log will be print to console @@ -46,6 +48,8 @@ basic: ```yaml basic: + # debug | info | warn | error | fatal | panic + loglevel: info # true|false, with debug level, it prints more debug info debug: false # true|false, if it's set to true, then the log will be print to console @@ -60,6 +64,8 @@ basic: ignoreCase: false ``` +当 debug 为 false 时,eKuiper 的日志级别可以通过 logLevel 控制,当 debug 为 true 时,eKuiper 的日志级别将固定为 debug。 + ## 系统日志 用户将名为 KuiperSyslogKey 的环境变量的值设置为 true 或者 syslog enable 配置为 true 时,日志将打印到系统日志中。更多 diff --git a/etc/kuiper.yaml b/etc/kuiper.yaml index d344b5e5cf..2772df2dd2 100644 --- a/etc/kuiper.yaml +++ b/etc/kuiper.yaml @@ -1,4 +1,6 @@ basic: + # debug | info | warn | error | fatal | panic + logLevel: info # true|false, with debug level, it prints more debug info debug: false # true|false, if it's set to true, then the log will be print to console diff --git a/internal/conf/conf.go b/internal/conf/conf.go index 970cd69d6d..841a53036e 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -34,7 +34,15 @@ import ( "github.com/lf-edge/ekuiper/pkg/schedule" ) -const ConfFileName = "kuiper.yaml" +const ( + ConfFileName = "kuiper.yaml" + DebugLogLevel = "debug" + InfoLogLevel = "info" + WarnLogLevel = "warn" + ErrorLogLevel = "error" + FatalLogLevel = "fatal" + PanicLogLevel = "panic" +) var ( Config *KuiperConf @@ -158,6 +166,7 @@ func (s *syslogConf) Validate() error { type KuiperConf struct { Basic struct { + LogLevel string `yaml:"logLevel"` Debug bool `yaml:"debug"` ConsoleLog bool `yaml:"consoleLog"` FileLog bool `yaml:"fileLog"` @@ -204,12 +213,25 @@ type KuiperConf struct { } } -func SetDebugLevel(v bool) { - lvl := logrus.InfoLevel - if v { - lvl = logrus.DebugLevel +func SetLogLevel(level string, debug bool) { + if debug { + Log.SetLevel(logrus.DebugLevel) + return + } + switch level { + case DebugLogLevel: + Log.SetLevel(logrus.DebugLevel) + case InfoLogLevel: + Log.SetLevel(logrus.InfoLevel) + case WarnLogLevel: + Log.SetLevel(logrus.WarnLevel) + case ErrorLogLevel: + Log.SetLevel(logrus.ErrorLevel) + case FatalLogLevel: + Log.SetLevel(logrus.FatalLevel) + case PanicLogLevel: + Log.SetLevel(logrus.PanicLevel) } - Log.SetLevel(lvl) } func SetConsoleAndFileLog(consoleLog, fileLog bool) error { @@ -299,9 +321,10 @@ func InitConf() { Config.Basic.RulePatrolInterval = "10s" } - if Config.Basic.Debug { - SetDebugLevel(true) + if Config.Basic.LogLevel == "" { + Config.Basic.LogLevel = InfoLogLevel } + SetLogLevel(Config.Basic.LogLevel, Config.Basic.Debug) if os.Getenv(logger.KuiperSyslogKey) == "true" || Config.Basic.Syslog != nil { c := Config.Basic.Syslog diff --git a/internal/server/rest.go b/internal/server/rest.go index 5989d9a12c..ff23e4a22f 100644 --- a/internal/server/rest.go +++ b/internal/server/rest.go @@ -1145,6 +1145,7 @@ func configurationStatusExport() Configuration { func configurationUpdateHandler(w http.ResponseWriter, r *http.Request) { basic := struct { + LogLevel *string `json:"logLevel"` Debug *bool `json:"debug"` ConsoleLog *bool `json:"consoleLog"` FileLog *bool `json:"fileLog"` @@ -1156,9 +1157,14 @@ func configurationUpdateHandler(w http.ResponseWriter, r *http.Request) { return } - if basic.Debug != nil { - conf.SetDebugLevel(*basic.Debug) - conf.Config.Basic.Debug = *basic.Debug + if basic.LogLevel != nil || basic.Debug != nil { + if basic.LogLevel != nil { + conf.Config.Basic.LogLevel = *basic.LogLevel + } + if basic.Debug != nil { + conf.Config.Basic.Debug = *basic.Debug + } + conf.SetLogLevel(conf.Config.Basic.LogLevel, conf.Config.Basic.Debug) } if basic.TimeZone != nil {