bbgo: add log fields support to the core

This commit is contained in:
c9s 2023-09-25 17:16:27 +08:00
parent e86a75c406
commit 550b010499
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 34 additions and 16 deletions

View File

@ -94,6 +94,7 @@ type NotificationConfig struct {
type LoggingConfig struct {
Trade bool `json:"trade,omitempty"`
Order bool `json:"order,omitempty"`
Fields map[string]interface{} `json:"fields,omitempty"`
}
type Session struct {

View File

@ -126,6 +126,14 @@ func NewEnvironment() *Environment {
}
}
func (environ *Environment) Logger() log.FieldLogger {
if environ.loggingConfig != nil && len(environ.loggingConfig.Fields) > 0 {
return log.WithFields(environ.loggingConfig.Fields)
}
return log.StandardLogger()
}
func (environ *Environment) Session(name string) (*ExchangeSession, bool) {
s, ok := environ.sessions[name]
return s, ok
@ -857,7 +865,9 @@ func (environ *Environment) setupSlack(userConfig *Config, slackToken string, pe
interact.AddMessenger(messenger)
}
func (environ *Environment) setupTelegram(userConfig *Config, telegramBotToken string, persistence service.PersistenceService) error {
func (environ *Environment) setupTelegram(
userConfig *Config, telegramBotToken string, persistence service.PersistenceService,
) error {
tt := strings.Split(telegramBotToken, ":")
telegramID := tt[0]

View File

@ -116,7 +116,7 @@ type ExchangeSession struct {
usedSymbols map[string]struct{}
initializedSymbols map[string]struct{}
logger *log.Entry
logger log.FieldLogger
}
func NewExchangeSession(name string, exchange types.Exchange) *ExchangeSession {
@ -182,10 +182,14 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment)
return ErrSessionAlreadyInitialized
}
var log = log.WithField("session", session.Name)
var logger = environ.Logger()
logger = logger.WithField("session", session.Name)
// override the default logger
session.logger = logger
// load markets first
log.Infof("querying market info from %s...", session.Name)
logger.Infof("querying market info from %s...", session.Name)
var disableMarketsCache = false
var markets types.MarketMap
@ -233,7 +237,7 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment)
// query and initialize the balances
if !session.PublicOnly {
log.Infof("querying account balances...")
logger.Infof("querying account balances...")
account, err := session.Exchange.QueryAccount(ctx)
if err != nil {
@ -244,8 +248,7 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment)
session.Account = account
session.accountMutex.Unlock()
log.Infof("account %s balances:", session.Name)
account.Balances().Print()
logger.Infof("account %s balances:\n%s", session.Name, account.Balances().String())
// forward trade updates and order updates to the order executor
session.UserDataStream.OnTradeUpdate(session.OrderExecutor.EmitTradeUpdate)
@ -275,29 +278,29 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment)
if environ.loggingConfig != nil {
if environ.loggingConfig.Trade {
session.UserDataStream.OnTradeUpdate(func(trade types.Trade) {
log.Info(trade.String())
logger.Info(trade.String())
})
}
if environ.loggingConfig.Order {
session.UserDataStream.OnOrderUpdate(func(order types.Order) {
log.Info(order.String())
logger.Info(order.String())
})
}
} else {
// if logging config is nil, then apply default logging setup
// add trade logger
session.UserDataStream.OnTradeUpdate(func(trade types.Trade) {
log.Info(trade.String())
logger.Info(trade.String())
})
}
if viper.GetBool("debug-kline") {
session.MarketDataStream.OnKLine(func(kline types.KLine) {
log.WithField("marketData", "kline").Infof("kline: %+v", kline)
logger.WithField("marketData", "kline").Infof("kline: %+v", kline)
})
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
log.WithField("marketData", "kline").Infof("kline closed: %+v", kline)
logger.WithField("marketData", "kline").Infof("kline closed: %+v", kline)
})
}
@ -558,7 +561,9 @@ func (session *ExchangeSession) MarketDataStore(symbol string) (s *MarketDataSto
}
// KLine updates will be received in the order listend in intervals array
func (session *ExchangeSession) SerialMarketDataStore(ctx context.Context, symbol string, intervals []types.Interval, useAggTrade ...bool) (store *SerialMarketDataStore, ok bool) {
func (session *ExchangeSession) SerialMarketDataStore(
ctx context.Context, symbol string, intervals []types.Interval, useAggTrade ...bool,
) (store *SerialMarketDataStore, ok bool) {
st, ok := session.MarketDataStore(symbol)
if !ok {
return nil, false
@ -628,7 +633,9 @@ func (session *ExchangeSession) OrderStores() map[string]*core.OrderStore {
}
// Subscribe save the subscription info, later it will be assigned to the stream
func (session *ExchangeSession) Subscribe(channel types.Channel, symbol string, options types.SubscribeOptions) *ExchangeSession {
func (session *ExchangeSession) Subscribe(
channel types.Channel, symbol string, options types.SubscribeOptions,
) *ExchangeSession {
if channel == types.KLineChannel && len(options.Interval) == 0 {
panic("subscription interval for kline can not be empty")
}