Skip to content

Commit

Permalink
Set default log level for alert hooking
Browse files Browse the repository at this point in the history
  • Loading branch information
wanliqun committed Apr 29, 2024
1 parent 624dd64 commit 8382dfe
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion log/hook.go → log/hook/hook.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package log
package hook

import (
"strings"
Expand Down
2 changes: 1 addition & 1 deletion log/hook_test.go → log/hook/hook_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package log
package hook

import (
"os"
Expand Down
20 changes: 20 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand All @@ -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,
}
Expand Down

0 comments on commit 8382dfe

Please sign in to comment.