Skip to content

Commit

Permalink
add level config
Browse files Browse the repository at this point in the history
  • Loading branch information
CharellKing committed Sep 12, 2024
1 parent bb7492c commit ee88be2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Ela is a tool to migrator data between different elasticsearch with different ve
## Config

```yaml
level: "info" # log level which is used to log message, can be assigned to 'debug', 'info', 'warn', 'error'.
elastics: # elasticsearch clusters
es5: # cluster name which is unique
addresses: # elasticsearch addresses which has many masters
Expand Down
1 change: 1 addition & 0 deletions config/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ type ESConfig struct {
type Config struct {
ESConfigs map[string]*ESConfig `mapstructure:"elastics"`
Tasks []*TaskCfg `mapstructure:"tasks"`
Level string `mapstructure:"level"`
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func main() {
return
}

utils.InitLogger(&cfg)

ctx := context.Background()
taskMgr, err := service.NewTaskMgr(&cfg)
if err != nil {
Expand Down
17 changes: 15 additions & 2 deletions utils/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,37 @@ package utils

import (
"context"
"github.com/CharellKing/ela/config"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
"os"
)

var logger *log.Logger

func init() {
func InitLogger(cfg *config.Config) {
levelMap := map[string]log.Level{
"debug": log.DebugLevel,
"info": log.InfoLevel,
"warn": log.WarnLevel,
"error": log.ErrorLevel,
}

level, ok := levelMap[cfg.Level]
if !ok {
level = log.InfoLevel
}
logger = &log.Logger{
Out: os.Stdout,
Formatter: &log.JSONFormatter{},
Hooks: make(log.LevelHooks),
Level: log.InfoLevel,
Level: level,
}
logger.SetReportCaller(true)
}

func GetLogger(ctx context.Context) *log.Entry {

entry := log.NewEntry(logger)

ctxKeyMap := map[CtxKey]func(ctx context.Context) string{
Expand Down

0 comments on commit ee88be2

Please sign in to comment.