Skip to content

Commit

Permalink
Make sure init is run to set dragon logadaptor (#484)
Browse files Browse the repository at this point in the history
* Make sure init is run to set dragon logadaptor and fixed race
  • Loading branch information
purplefox authored Jul 18, 2022
1 parent 26608b4 commit c28f714
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
5 changes: 5 additions & 0 deletions cluster/dragon/dragon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
23 changes: 12 additions & 11 deletions cluster/dragon/logadaptor/logrus_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
}
Expand Down

0 comments on commit c28f714

Please sign in to comment.