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 f214039
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 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
24 changes: 18 additions & 6 deletions querylog/logger_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,46 @@ package querylog
import (
"time"

"github.com/sirupsen/logrus/hooks/test"
"github.com/0xERR0R/blocky/log"
"github.com/sirupsen/logrus"

. "github.com/onsi/gomega"

. "github.com/onsi/ginkgo/v2"
)

var _ = Describe("LoggerWriter", func() {
var (
logger *logrus.Entry
hook *log.MockLoggerHook
)

BeforeEach(func() {
logger, hook = log.NewMockEntry()
})

Describe("logger query log", func() {
When("New log entry was created", func() {
It("should be logged", func() {
writer := NewLoggerWriter()
logger, hook := test.NewNullLogger()
writer := NewLoggerWriter(logger)
writer.logger = logger.WithField("k", "v")

writer.Write(&LogEntry{
Start: time.Now(),
DurationMs: 20,
})

Expect(hook.Entries).Should(HaveLen(1))
Expect(hook.LastEntry().Message).Should(Equal("query resolved"))
Expect(hook.Calls).Should(HaveLen(1))
Expect(hook.Messages).Should(ContainElement(Equal("query resolved")))
})
})

When("Cleanup is called", func() {
It("should do nothing", func() {
writer := NewLoggerWriter()
writer := NewLoggerWriter(logger)
writer.CleanUp()

Expect(hook.Calls).Should(BeEmpty())
})
})
})
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
14 changes: 13 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,11 +45,14 @@ var _ = Describe("QueryLoggingResolver", func() {
m *mockResolver
tmpDir *TmpFolder
mockAnswer *dns.Msg
writer *log.MockLoggerHook

ctx context.Context
cancelFn context.CancelFunc
)

_ = writer // used in follow-up commits

Describe("Type", func() {
It("follows conventions", func() {
expectValidResolverType(sut)
Expand All @@ -69,6 +73,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 f214039

Please sign in to comment.