Skip to content

Commit

Permalink
refactor(querylog): make LoggerWriter logger configurable
Browse files Browse the repository at this point in the history
For use in tests.
  • Loading branch information
ThinkChaos committed Apr 4, 2024
1 parent c56f0f9 commit 58ce7ea
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
7 changes: 2 additions & 5 deletions querylog/logger_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ package querylog
import (
"strings"

"github.com/0xERR0R/blocky/log"
"github.com/0xERR0R/blocky/util"
"github.com/sirupsen/logrus"
)

const loggerPrefixLoggerWriter = "queryLog"

type LoggerWriter struct {
logger *logrus.Entry
}

func NewLoggerWriter() *LoggerWriter {
return &LoggerWriter{logger: log.PrefixedLog(loggerPrefixLoggerWriter)}
func NewLoggerWriter(logger *logrus.Entry) *LoggerWriter {
return &LoggerWriter{logger}
}

func (d *LoggerWriter) Write(entry *LogEntry) {
Expand Down
6 changes: 4 additions & 2 deletions resolver/query_logging_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const (
cleanUpRunPeriod = 12 * time.Hour
queryLoggingResolverType = "query_logging"
logChanCap = 1000

loggerPrefixLoggerWriter = "queryLog"
)

// QueryLoggingResolver writes query information (question, answer, duration, ...)
Expand Down Expand Up @@ -50,7 +52,7 @@ func NewQueryLoggingResolver(ctx context.Context, cfg config.QueryLog) *QueryLog
writer, err = querylog.NewDatabaseWriter(ctx, "postgresql", cfg.Target, cfg.LogRetentionDays,
cfg.FlushInterval.ToDuration())
case config.QueryLogTypeConsole:
writer = querylog.NewLoggerWriter()
writer = querylog.NewLoggerWriter(log.PrefixedLog(loggerPrefixLoggerWriter))
case config.QueryLogTypeNone:
writer = querylog.NewNoneWriter()
}
Expand All @@ -68,7 +70,7 @@ func NewQueryLoggingResolver(ctx context.Context, cfg config.QueryLog) *QueryLog
if err != nil {
logger.Error("can't create query log writer, using console as fallback: ", err)

writer = querylog.NewLoggerWriter()
writer = querylog.NewLoggerWriter(log.PrefixedLog(loggerPrefixLoggerWriter))
cfg.Type = config.QueryLogTypeConsole
}

Expand Down
12 changes: 11 additions & 1 deletion resolver/query_logging_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
. "github.com/0xERR0R/blocky/helpertest"
"github.com/0xERR0R/blocky/log"
"github.com/0xERR0R/blocky/querylog"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/mock"

"github.com/0xERR0R/blocky/config"
. "github.com/0xERR0R/blocky/model"
Expand All @@ -21,7 +23,6 @@ import (
"github.com/miekg/dns"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/mock"
)

type SlowMockWriter struct {
Expand All @@ -44,6 +45,7 @@ var _ = Describe("QueryLoggingResolver", func() {
m *mockResolver
tmpDir *TmpFolder
mockAnswer *dns.Msg
writer *log.MockLoggerHook

ctx context.Context
cancelFn context.CancelFunc
Expand All @@ -69,6 +71,14 @@ var _ = Describe("QueryLoggingResolver", func() {
}

sut = NewQueryLoggingResolver(ctx, sutConfig)

if sutConfig.Type == config.QueryLogTypeConsole {
var logger *logrus.Entry

logger, writer = log.NewMockEntry()
sut.writer = querylog.NewLoggerWriter(logger)
}

m = &mockResolver{}
m.On("Resolve", mock.Anything).Return(&Response{Res: mockAnswer, Reason: "reason"}, nil)
sut.Next(m)
Expand Down

0 comments on commit 58ce7ea

Please sign in to comment.