Skip to content

Commit

Permalink
feat: support log level (#2394)
Browse files Browse the repository at this point in the history
Signed-off-by: yisaer <[email protected]>
  • Loading branch information
Yisaer committed Nov 8, 2023
1 parent 488e034 commit 541d425
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
6 changes: 6 additions & 0 deletions docs/en_US/configuration/global_configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions docs/zh_CN/configuration/global_configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -60,6 +64,8 @@ basic:
ignoreCase: false
```

当 debug 为 false 时,eKuiper 的日志级别可以通过 logLevel 控制,当 debug 为 true 时,eKuiper 的日志级别将固定为 debug。

## 系统日志

用户将名为 KuiperSyslogKey 的环境变量的值设置为 true 或者 syslog enable 配置为 true 时,日志将打印到系统日志中。更多
Expand Down
2 changes: 2 additions & 0 deletions etc/kuiper.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
39 changes: 31 additions & 8 deletions internal/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions internal/server/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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 {
Expand Down

0 comments on commit 541d425

Please sign in to comment.