From c28f714f147fa4eff60266813de79240833e1dc9 Mon Sep 17 00:00:00 2001 From: Tim Fox Date: Mon, 18 Jul 2022 20:30:05 +0100 Subject: [PATCH] Make sure init is run to set dragon logadaptor (#484) * Make sure init is run to set dragon logadaptor and fixed race --- cluster/dragon/dragon.go | 5 +++++ cluster/dragon/logadaptor/logrus_adaptor.go | 23 +++++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/cluster/dragon/dragon.go b/cluster/dragon/dragon.go index cf3835d8..5913fe9d 100644 --- a/cluster/dragon/dragon.go +++ b/cluster/dragon/dragon.go @@ -3,6 +3,7 @@ package dragon import ( "context" "fmt" + "github.com/squareup/pranadb/cluster/dragon/logadaptor" "github.com/squareup/pranadb/protos/squareup/cash/pranadb/v1/notifications" "github.com/squareup/pranadb/remoting" "math/rand" @@ -49,6 +50,10 @@ const ( pullQueryRetryTimeout = 10 * time.Second ) +func init() { + logger.SetLoggerFactory(logadaptor.LogrusLogFactory) +} + func NewDragon(cnf conf.Config) (*Dragon, error) { if len(cnf.RaftAddresses) < 3 { return nil, errors.Error("minimum cluster size is 3 nodes") diff --git a/cluster/dragon/logadaptor/logrus_adaptor.go b/cluster/dragon/logadaptor/logrus_adaptor.go index 18c8f8dc..c1f05418 100644 --- a/cluster/dragon/logadaptor/logrus_adaptor.go +++ b/cluster/dragon/logadaptor/logrus_adaptor.go @@ -3,48 +3,49 @@ package logadaptor import ( "github.com/lni/dragonboat/v3/logger" log "github.com/sirupsen/logrus" + "sync/atomic" ) /* This adaptor allows us to plug the dragonboat logging into the logrus logger we use in Prana. */ -func init() { - logger.SetLoggerFactory(logrusLogFactory) -} - -func logrusLogFactory(pkgName string) logger.ILogger { +func LogrusLogFactory(pkgName string) logger.ILogger { return &LogrusILogger{} } type LogrusILogger struct { - level logger.LogLevel + level int64 +} + +func (l *LogrusILogger) getLevel() logger.LogLevel { + return logger.LogLevel(atomic.LoadInt64(&l.level)) } func (l *LogrusILogger) SetLevel(level logger.LogLevel) { - l.level = level + atomic.StoreInt64(&l.level, int64(level)) } func (l *LogrusILogger) Debugf(format string, args ...interface{}) { - if l.level >= logger.DEBUG { + if l.getLevel() >= logger.DEBUG { log.Debugf(format, args...) } } func (l *LogrusILogger) Infof(format string, args ...interface{}) { - if l.level >= logger.INFO { + if l.getLevel() >= logger.INFO { log.Infof(format, args...) } } func (l *LogrusILogger) Warningf(format string, args ...interface{}) { - if l.level >= logger.WARNING { + if l.getLevel() >= logger.WARNING { log.Warnf(format, args...) } } func (l *LogrusILogger) Errorf(format string, args ...interface{}) { - if l.level >= logger.ERROR { + if l.getLevel() >= logger.ERROR { log.Errorf(format, args...) } }