Skip to content

Commit

Permalink
Fix logging and config
Browse files Browse the repository at this point in the history
  • Loading branch information
zale144 committed Oct 5, 2022
1 parent 8cc55a5 commit 0c0b195
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 27 deletions.
22 changes: 0 additions & 22 deletions bot/normal/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"path"
"runtime"
"strings"
"sync"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 5 additions & 5 deletions config/config_stagnet1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
47 changes: 47 additions & 0 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"fmt"
"net/http"
"path"
"runtime"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down

0 comments on commit 0c0b195

Please sign in to comment.