Skip to content

Commit

Permalink
chore: set log_level
Browse files Browse the repository at this point in the history
  • Loading branch information
albttx committed Nov 10, 2022
1 parent 4def4ec commit d7f7542
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/cosmos-notifyer/cli_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (s *service) startWatcher(chain Chain) {
ctx = ctxlogger.WithValue(ctx, "rpc", rpc)

l := ctxlogger.Logger(ctx)
l.Logger.SetLevel(s.cfg.GetLogLevel())

c := cosmosblocks.NewClient(cosmosblocks.Config{
RPCEndpoint: rpc,
Expand Down
17 changes: 17 additions & 0 deletions cmd/cosmos-notifyer/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import "github.com/sirupsen/logrus"

type Config struct {
LogLevel string `yaml:"log_level"`

Chains []Chain `yaml:"chains"`

Notifications struct {
Expand All @@ -25,6 +29,19 @@ type Chain struct {
} `yaml:"notification"`
}

func (cfg Config) GetLogLevel() logrus.Level {
if cfg.LogLevel == "DEBUG" {
return logrus.DebugLevel
} else if cfg.LogLevel == "INFO" {
return logrus.InfoLevel
} else if cfg.LogLevel == "WARN" {
return logrus.WarnLevel
} else if cfg.LogLevel == "ERROR" {
return logrus.ErrorLevel
}
return logrus.InfoLevel
}

func (c Chain) GetTokenCoefficient() int {
if c.Token.Coefficient == 0 {
return 1000000
Expand Down
3 changes: 3 additions & 0 deletions config.example.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
---
# Could be one of "DEBUG", "INFO", "WARN", "ERROR"
log_level: "INFO"

notifications:
discord:
webhook: "https://discord.com/api/webhooks/xxxxxxxxx"
Expand Down
8 changes: 1 addition & 7 deletions internal/ctxlogger/ctxlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ type ContextKey string
type ContextData map[string]interface{}

const (
requestIDKey = ContextKey("request_id")
dataKey = ContextKey("data")
dataKey = ContextKey("data")
)

// WithValue inject data value inside the context
Expand All @@ -26,17 +25,12 @@ func WithValue(ctx context.Context, key string, value interface{}) context.Conte
}

// Logger create a logrus.NewEntry based on the StandardLogger
// the request_id is injected
func Logger(ctx context.Context) *logrus.Entry {
l := logrus.WithContext(ctx)
if ctx == nil {
return l
}

if reqID, ok := ctx.Value(requestIDKey).(string); ok {
l = l.WithField("request_id", reqID)
}

if data, ok := ctx.Value(dataKey).(ContextData); ok {
for k, v := range data {
l = l.WithField(k, v)
Expand Down

0 comments on commit d7f7542

Please sign in to comment.