diff --git a/bot/normal/normal.go b/bot/normal/normal.go index 1b17307..6300922 100644 --- a/bot/normal/normal.go +++ b/bot/normal/normal.go @@ -4,8 +4,6 @@ import ( "context" "encoding/json" "fmt" - "path" - "runtime" "strings" "sync" @@ -76,8 +74,6 @@ func New( // Start starts the liquidity bot goroutine(s). func (b *bot) Start() error { - setupLogger(true) // TODO: pretty from config? - ctx := context.Background() walletPubKey, err := b.setupWallet(ctx) @@ -141,24 +137,6 @@ func (b *bot) Start() error { return nil } -func setupLogger(pretty bool) { - log.SetReportCaller(true) - log.SetFormatter(&log.JSONFormatter{ - CallerPrettyfier: func(f *runtime.Frame) (string, string) { - filename := path.Base(f.File) - function := strings.ReplaceAll(f.Function, "code.vegaprotocol.io/", "") - idx := strings.Index(function, ".") - function = fmt.Sprintf("%s/%s/%s():%d", function[:idx], filename, function[idx+1:], f.Line) - return function, "" - }, - PrettyPrint: pretty, - DataKey: "_vals", - FieldMap: log.FieldMap{ - log.FieldKeyMsg: "_msg", - }, - }) -} - func (b *bot) pauseChannel() chan types.PauseSignal { in := make(chan types.PauseSignal) go func() { diff --git a/config/config_stagnet1.yaml b/config/config_stagnet1.yaml index 6ba5a01..c1eae93 100644 --- a/config/config_stagnet1.yaml +++ b/config/config_stagnet1.yaml @@ -3,8 +3,8 @@ server: env: prod # dev, prod listen: ":7800" - logformat: text # json, text - loglevel: debug # debug, info, warning, error + logformat: json # json_pretty, text + loglevel: debug # debug, trace, info, warning, error callTimeoutMills: 10000 vegaAssetID: fc7fd956078fb1fc9db5c19b88f0874c4299b2a7639ad05a47a28c0aef291b55 @@ -23,13 +23,13 @@ token: syncTimeoutSec: 1000 whale: walletPubKey: bdb4598b5e22ac01a812d166cc3006ac70575cd6b91b5bb4de52e158e510ec23 - walletName: whale # secret - walletPassphrase: pastazazube # secret + walletName: # secret + walletPassphrase: # secret syncTimeoutSec: 0 ownerPrivateKeys: # assetID: privateKey c9fe6fc24fce121b2cc72680543a886055abb560043fda394ba5376203b7527d: # secret fc7fd956078fb1fc9db5c19b88f0874c4299b2a7639ad05a47a28c0aef291b55: # secret - faucetURL: https://faucet.stagnet3.vega.xyz + faucetURL: https://faucet.stagnet1.vega.xyz slack: appToken: # secret botToken: # secret diff --git a/service/service.go b/service/service.go index ca1a246..20bf855 100644 --- a/service/service.go +++ b/service/service.go @@ -5,6 +5,8 @@ import ( "encoding/json" "fmt" "net/http" + "path" + "runtime" "strings" "sync" "time" @@ -74,6 +76,10 @@ func NewService(config config.Config) (s *Service, err error) { bots: make(map[string]Bot), } + if err = setupLogger(config.Server); err != nil { + return nil, fmt.Errorf("failed to setup logger: %w", err) + } + pricingEngine := pricing.NewEngine(*config.Pricing) whaleService, err := getWhale(config) @@ -91,6 +97,47 @@ func NewService(config config.Config) (s *Service, err error) { return s, err } +func setupLogger(conf *config.ServerConfig) error { + level, err := log.ParseLevel(conf.LogLevel) + if err != nil { + return fmt.Errorf("failed to parse log level: %w", err) + } + + log.SetLevel(level) + + var callerPrettyfier func(*runtime.Frame) (string, string) + + if conf.LogLevel == "debug" { + log.SetReportCaller(true) + + callerPrettyfier = func(f *runtime.Frame) (string, string) { + filename := path.Base(f.File) + function := strings.ReplaceAll(f.Function, "code.vegaprotocol.io/", "") + idx := strings.Index(function, ".") + function = fmt.Sprintf("%s/%s/%s():%d", function[:idx], filename, function[idx+1:], f.Line) + return function, "" + } + } + + var formatter log.Formatter = &log.TextFormatter{ + CallerPrettyfier: callerPrettyfier, + } + + if conf.LogFormat == "json" || conf.LogFormat == "json_pretty" { + formatter = &log.JSONFormatter{ + PrettyPrint: conf.LogFormat == "json_pretty", + DataKey: "_vals", + FieldMap: log.FieldMap{ + log.FieldKeyMsg: "_msg", + }, + CallerPrettyfier: callerPrettyfier, + } + } + + log.SetFormatter(formatter) + return nil +} + func getWhale(config config.Config) (*whale.Service, error) { dataNode := node.NewDataNode( config.Locations,