Skip to content

Commit c28f714

Browse files
authored
Make sure init is run to set dragon logadaptor (#484)
* Make sure init is run to set dragon logadaptor and fixed race
1 parent 26608b4 commit c28f714

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

cluster/dragon/dragon.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dragon
33
import (
44
"context"
55
"fmt"
6+
"github.com/squareup/pranadb/cluster/dragon/logadaptor"
67
"github.com/squareup/pranadb/protos/squareup/cash/pranadb/v1/notifications"
78
"github.com/squareup/pranadb/remoting"
89
"math/rand"
@@ -49,6 +50,10 @@ const (
4950
pullQueryRetryTimeout = 10 * time.Second
5051
)
5152

53+
func init() {
54+
logger.SetLoggerFactory(logadaptor.LogrusLogFactory)
55+
}
56+
5257
func NewDragon(cnf conf.Config) (*Dragon, error) {
5358
if len(cnf.RaftAddresses) < 3 {
5459
return nil, errors.Error("minimum cluster size is 3 nodes")

cluster/dragon/logadaptor/logrus_adaptor.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,49 @@ package logadaptor
33
import (
44
"github.com/lni/dragonboat/v3/logger"
55
log "github.com/sirupsen/logrus"
6+
"sync/atomic"
67
)
78

89
/*
910
This adaptor allows us to plug the dragonboat logging into the logrus logger we use in Prana.
1011
*/
1112

12-
func init() {
13-
logger.SetLoggerFactory(logrusLogFactory)
14-
}
15-
16-
func logrusLogFactory(pkgName string) logger.ILogger {
13+
func LogrusLogFactory(pkgName string) logger.ILogger {
1714
return &LogrusILogger{}
1815
}
1916

2017
type LogrusILogger struct {
21-
level logger.LogLevel
18+
level int64
19+
}
20+
21+
func (l *LogrusILogger) getLevel() logger.LogLevel {
22+
return logger.LogLevel(atomic.LoadInt64(&l.level))
2223
}
2324

2425
func (l *LogrusILogger) SetLevel(level logger.LogLevel) {
25-
l.level = level
26+
atomic.StoreInt64(&l.level, int64(level))
2627
}
2728

2829
func (l *LogrusILogger) Debugf(format string, args ...interface{}) {
29-
if l.level >= logger.DEBUG {
30+
if l.getLevel() >= logger.DEBUG {
3031
log.Debugf(format, args...)
3132
}
3233
}
3334

3435
func (l *LogrusILogger) Infof(format string, args ...interface{}) {
35-
if l.level >= logger.INFO {
36+
if l.getLevel() >= logger.INFO {
3637
log.Infof(format, args...)
3738
}
3839
}
3940

4041
func (l *LogrusILogger) Warningf(format string, args ...interface{}) {
41-
if l.level >= logger.WARNING {
42+
if l.getLevel() >= logger.WARNING {
4243
log.Warnf(format, args...)
4344
}
4445
}
4546

4647
func (l *LogrusILogger) Errorf(format string, args ...interface{}) {
47-
if l.level >= logger.ERROR {
48+
if l.getLevel() >= logger.ERROR {
4849
log.Errorf(format, args...)
4950
}
5051
}

0 commit comments

Comments
 (0)