Skip to content

Commit 98c7fbe

Browse files
author
kirill-amboss
authored
Make logger extendable (#123)
Extract a Logger interface so it can be customized by downstream dependencies.
1 parent dcc2e30 commit 98c7fbe

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

gateway.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,13 @@ func WithLocationPriorities(priorities []string) Option {
271271
}
272272
}
273273

274+
// WithLogger returns an Option that sets the logger of the gateway
275+
func WithLogger(l Logger) Option {
276+
return func(g *Gateway) {
277+
log = l
278+
}
279+
}
280+
274281
var nodeField = &QueryField{
275282
Name: "node",
276283
Type: ast.NamedType("Node", &ast.Position{}),

gateway_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,17 @@ func TestGateway(t *testing.T) {
139139
assert.Equal(t, priorities, gateway.locationPriorities)
140140
})
141141

142+
t.Run("WithLogger", func(t *testing.T) {
143+
logger := &DefaultLogger{}
144+
_, err := New(sources, WithLogger(logger))
145+
if err != nil {
146+
t.Error(err.Error())
147+
return
148+
}
149+
150+
assert.Equal(t, logger, log)
151+
})
152+
142153
t.Run("fieldURLs ignore introspection", func(t *testing.T) {
143154
locations := fieldURLs(sources, true)
144155

logging.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,26 @@ import (
55
"github.com/sirupsen/logrus"
66
)
77

8-
// Logger handles the logging in the gateway library
9-
type Logger struct {
8+
// Logger logs messages
9+
type Logger interface {
10+
Debug(args ...interface{})
11+
Info(args ...interface{})
12+
Warn(args ...interface{})
13+
14+
WithFields(fields LoggerFields) Logger
15+
QueryPlanStep(step *QueryPlanStep)
16+
}
17+
18+
// DefaultLogger handles the logging in the gateway library
19+
type DefaultLogger struct {
1020
fields logrus.Fields
1121
}
1222

1323
// LoggerFields is a wrapper over a map of key,value pairs to associate with the log
1424
type LoggerFields map[string]interface{}
1525

1626
// Debug should be used for any logging that would be useful for debugging
17-
func (l *Logger) Debug(args ...interface{}) {
27+
func (l *DefaultLogger) Debug(args ...interface{}) {
1828
entry := newLogEntry()
1929
// if there are fields
2030
if l.fields != nil {
@@ -26,7 +36,7 @@ func (l *Logger) Debug(args ...interface{}) {
2636
}
2737

2838
// Info should be used for any logging that doesn't necessarily need attention but is nice to see by default
29-
func (l *Logger) Info(args ...interface{}) {
39+
func (l *DefaultLogger) Info(args ...interface{}) {
3040
entry := newLogEntry()
3141
// if there are fields
3242
if l.fields != nil {
@@ -38,7 +48,7 @@ func (l *Logger) Info(args ...interface{}) {
3848
}
3949

4050
// Warn should be used for logging that needs attention
41-
func (l *Logger) Warn(args ...interface{}) {
51+
func (l *DefaultLogger) Warn(args ...interface{}) {
4252
entry := newLogEntry()
4353
// if there are fields
4454
if l.fields != nil {
@@ -50,26 +60,26 @@ func (l *Logger) Warn(args ...interface{}) {
5060
}
5161

5262
// WithFields adds the provided fields to the Log
53-
func (l *Logger) WithFields(fields LoggerFields) *Logger {
63+
func (l *DefaultLogger) WithFields(fields LoggerFields) Logger {
5464
// build up the logrus fields
5565
logrusFields := logrus.Fields{}
5666
for key, value := range fields {
5767
logrusFields[key] = value
5868
}
59-
return &Logger{fields: logrusFields}
69+
return &DefaultLogger{fields: logrusFields}
6070
}
6171

6272
// QueryPlanStep formats and logs a query plan step for human consumption
63-
func (l *Logger) QueryPlanStep(step *QueryPlanStep) {
64-
log.WithFields(LoggerFields{
73+
func (l *DefaultLogger) QueryPlanStep(step *QueryPlanStep) {
74+
l.WithFields(LoggerFields{
6575
"id": step.ParentID,
6676
"insertion point": step.InsertionPoint,
6777
}).Info(step.ParentType)
6878

69-
log.Info(graphql.FormatSelectionSet(step.SelectionSet))
79+
l.Info(graphql.FormatSelectionSet(step.SelectionSet))
7080
}
7181

72-
var log *Logger
82+
var log Logger = &DefaultLogger{}
7383

7484
func newLogEntry() *logrus.Entry {
7585
entry := logrus.New()
@@ -86,7 +96,3 @@ func newLogEntry() *logrus.Entry {
8696

8797
return logrus.NewEntry(entry)
8898
}
89-
func init() {
90-
log = &Logger{}
91-
92-
}

0 commit comments

Comments
 (0)