bbgo: provide logging configuration

This commit is contained in:
c9s 2023-02-22 15:25:39 +08:00
parent e3fa4587d9
commit 905b25655d
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 33 additions and 4 deletions

View File

@ -14,6 +14,10 @@ func BootstrapEnvironmentLightweight(ctx context.Context, environ *Environment,
return errors.Wrap(err, "exchange session configure error")
}
if userConfig.Logging != nil {
environ.SetLogging(userConfig.Logging)
}
if userConfig.Persistence != nil {
if err := ConfigurePersistence(ctx, userConfig.Persistence); err != nil {
return errors.Wrap(err, "persistence configure error")
@ -32,6 +36,10 @@ func BootstrapEnvironment(ctx context.Context, environ *Environment, userConfig
return errors.Wrap(err, "exchange session configure error")
}
if userConfig.Logging != nil {
environ.SetLogging(userConfig.Logging)
}
if userConfig.Persistence != nil {
if err := ConfigurePersistence(ctx, userConfig.Persistence); err != nil {
return errors.Wrap(err, "persistence configure error")

View File

@ -102,6 +102,8 @@ type Environment struct {
syncStatus SyncStatus
syncConfig *SyncConfig
loggingConfig *LoggingConfig
sessions map[string]*ExchangeSession
}
@ -127,6 +129,10 @@ func (environ *Environment) Sessions() map[string]*ExchangeSession {
return environ.sessions
}
func (environ *Environment) SetLogging(config *LoggingConfig) {
environ.loggingConfig = config
}
func (environ *Environment) SelectSessions(names ...string) map[string]*ExchangeSession {
if len(names) == 0 {
return environ.sessions

View File

@ -261,10 +261,25 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment)
}
}
// add trade logger
session.UserDataStream.OnTradeUpdate(func(trade types.Trade) {
log.Info(trade.String())
})
if environ.loggingConfig != nil {
if environ.loggingConfig.Trade {
session.UserDataStream.OnTradeUpdate(func(trade types.Trade) {
log.Info(trade.String())
})
}
if environ.loggingConfig.Order {
session.UserDataStream.OnOrderUpdate(func(order types.Order) {
log.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())
})
}
if viper.GetBool("debug-kline") {
session.MarketDataStream.OnKLine(func(kline types.KLine) {