diff --git a/config/config.yaml b/config/config.yaml index ccf678e..a765a25 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -3,10 +3,11 @@ # level: info # forceColor: false # disableColor: false +# alertHookLevels: [warn,error,fatal] # Alert Configurations # alert: -# customTags: [testnet,dev] +# customTags: [dev,test] # dingtalk: # enabled: false # webhook: https://oapi.dingtalk.com/robot/send?access_token=${your_access_token} diff --git a/log/hook.go b/log/hook/hook.go similarity index 98% rename from log/hook.go rename to log/hook/hook.go index 3191de1..b6d403d 100644 --- a/log/hook.go +++ b/log/hook/hook.go @@ -1,4 +1,4 @@ -package log +package hook import ( "strings" diff --git a/log/hook_test.go b/log/hook/hook_test.go similarity index 98% rename from log/hook_test.go rename to log/hook/hook_test.go index 1d9c48a..64eb943 100644 --- a/log/hook_test.go +++ b/log/hook/hook_test.go @@ -1,4 +1,4 @@ -package log +package hook import ( "os" diff --git a/log/log.go b/log/log.go index ca4bce2..74c2783 100644 --- a/log/log.go +++ b/log/log.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/Conflux-Chain/go-conflux-util/log/hook" viperUtil "github.com/Conflux-Chain/go-conflux-util/viper" "github.com/ethereum/go-ethereum/log" "github.com/pkg/errors" @@ -17,6 +18,9 @@ type LoggingConfig struct { Level string `default:"info"` // logging level ForceColor bool // helpful on windows DisableColor bool // helpful to output logs in file + + // logrus level hooked for alert notification + AlertHookLevels []string `default:"[warn,error,fatal]"` } // MustInitFromViper inits logging from viper settings and adapts Geth logger. @@ -32,12 +36,28 @@ func MustInitFromViper() { // Init inits logging with specified log level func MustInit(conf LoggingConfig) { + // parse logging level level, err := logrus.ParseLevel(conf.Level) if err != nil { logrus.WithError(err).WithField("level", conf.Level).Fatal("Failed to parse log level") } logrus.SetLevel(level) + // hook alert logging levels + var hookLvls []logrus.Level + for _, lvlStr := range conf.AlertHookLevels { + lvl, err := logrus.ParseLevel(lvlStr) + if err != nil { + logrus.WithError(err).WithField("level", lvlStr).Fatal("Failed to parse log level for alert hooking") + } + hookLvls = append(hookLvls, lvl) + } + + if len(hookLvls) > 0 { + hook.AddDingTalkAlertHook(hookLvls) + } + + // set text formtter formatter := &logrus.TextFormatter{ FullTimestamp: true, }