Skip to content

Commit 401221d

Browse files
authored
Allow users to configure either unstructured or JSON logging (hashicorp#7130)
* hclog Allow users to choose between unstructured and JSON logging
1 parent 848938a commit 401221d

File tree

402 files changed

+46339
-40169
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

402 files changed

+46339
-40169
lines changed

agent/acl.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (a *Agent) resolveIdentityFromToken(secretID string) (bool, structs.ACLIden
6262
func (a *Agent) aclAccessorID(secretID string) string {
6363
_, ident, err := a.resolveIdentityFromToken(secretID)
6464
if err != nil {
65-
a.logger.Printf("[DEBUG] agent.acl: %v", err)
65+
a.logger.Debug("error", err)
6666
return ""
6767
}
6868
if ident == nil {
@@ -281,7 +281,7 @@ func (a *Agent) filterMembers(token string, members *[]serf.Member) error {
281281
continue
282282
}
283283
accessorID := a.aclAccessorID(token)
284-
a.logger.Printf("[DEBUG] agent: dropping node from result due to ACLs, node=%q accessorID=%q", node, accessorID)
284+
a.logger.Debug("dropping node from result due to ACLs", "node", node, "accessorID", accessorID)
285285
m = append(m[:i], m[i+1:]...)
286286
i--
287287
}
@@ -311,7 +311,7 @@ func (a *Agent) filterServicesWithAuthorizer(authz acl.Authorizer, services *map
311311
if authz.ServiceRead(service.Service, &authzContext) == acl.Allow {
312312
continue
313313
}
314-
a.logger.Printf("[DEBUG] agent: dropping service from result due to ACLs, service=%q", id.String())
314+
a.logger.Debug("dropping service from result due to ACLs", "service", id.String())
315315
delete(*services, id)
316316
}
317317
return nil
@@ -347,7 +347,7 @@ func (a *Agent) filterChecksWithAuthorizer(authz acl.Authorizer, checks *map[str
347347
continue
348348
}
349349
}
350-
a.logger.Printf("[DEBUG] agent: dropping check from result due to ACLs, check=%q", id.String())
350+
a.logger.Debug("dropping check from result due to ACLs", "check", id.String())
351351
delete(*checks, id)
352352
}
353353
return nil

agent/acl_test.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package agent
33
import (
44
"fmt"
55
"io"
6-
"log"
76
"testing"
87
"time"
98

@@ -14,9 +13,9 @@ import (
1413
"github.com/hashicorp/consul/agent/local"
1514
"github.com/hashicorp/consul/agent/structs"
1615
"github.com/hashicorp/consul/lib"
17-
"github.com/hashicorp/consul/logger"
1816
"github.com/hashicorp/consul/sdk/testutil"
1917
"github.com/hashicorp/consul/types"
18+
"github.com/hashicorp/go-hclog"
2019
"github.com/hashicorp/serf/serf"
2120

2221
"github.com/stretchr/testify/require"
@@ -39,9 +38,6 @@ type TestACLAgent struct {
3938
// to os.Stderr.
4039
LogOutput io.Writer
4140

42-
// LogWriter is used for streaming logs.
43-
LogWriter *logger.LogWriter
44-
4541
// DataDir is the data directory which is used when Config.DataDir
4642
// is not set. It is created automatically and removed when
4743
// Shutdown() is called.
@@ -60,7 +56,11 @@ func NewTestACLAgent(t *testing.T, name string, hcl string, resolveFn func(strin
6056
hclDataDir := `data_dir = "acl-agent"`
6157

6258
logOutput := testutil.TestWriter(t)
63-
logger := log.New(logOutput, a.Name+" - ", log.LstdFlags|log.Lmicroseconds)
59+
logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{
60+
Name: a.Name,
61+
Level: hclog.Debug,
62+
Output: logOutput,
63+
})
6464

6565
a.Config = TestConfig(logger,
6666
config.Source{Name: a.Name, Format: "hcl", Data: a.HCL},
@@ -74,7 +74,6 @@ func NewTestACLAgent(t *testing.T, name string, hcl string, resolveFn func(strin
7474
a.Agent = agent
7575

7676
agent.LogOutput = logOutput
77-
agent.LogWriter = a.LogWriter
7877
agent.logger = logger
7978
agent.MemSink = metrics.NewInmemSink(1*time.Second, time.Minute)
8079

agent/ae/ae.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package ae
33

44
import (
55
"fmt"
6-
"log"
6+
"github.com/hashicorp/consul/lib"
7+
"github.com/hashicorp/consul/logging"
8+
"github.com/hashicorp/go-hclog"
79
"math"
810
"sync"
911
"time"
10-
11-
"github.com/hashicorp/consul/lib"
1212
)
1313

1414
// scaleThreshold is the number of nodes after which regular sync runs are
@@ -60,7 +60,7 @@ type StateSyncer struct {
6060
ShutdownCh chan struct{}
6161

6262
// Logger is the logger.
63-
Logger *log.Logger
63+
Logger hclog.Logger
6464

6565
// ClusterSize returns the number of members in the cluster to
6666
// allow staggering the sync runs based on cluster size.
@@ -107,12 +107,16 @@ const (
107107
retryFailIntv = 15 * time.Second
108108
)
109109

110-
func NewStateSyncer(state SyncState, intv time.Duration, shutdownCh chan struct{}, logger *log.Logger) *StateSyncer {
110+
func NewStateSyncer(state SyncState, intv time.Duration, shutdownCh chan struct{}, logger hclog.Logger) *StateSyncer {
111+
if logger == nil {
112+
logger = hclog.New(&hclog.LoggerOptions{})
113+
}
114+
111115
s := &StateSyncer{
112116
State: state,
113117
Interval: intv,
114118
ShutdownCh: shutdownCh,
115-
Logger: logger,
119+
Logger: logger.Named(logging.AntiEntropy),
116120
SyncFull: NewTrigger(),
117121
SyncChanges: NewTrigger(),
118122
serverUpInterval: serverUpIntv,
@@ -166,7 +170,7 @@ func (s *StateSyncer) nextFSMState(fs fsmState) fsmState {
166170

167171
err := s.State.SyncFull()
168172
if err != nil {
169-
s.Logger.Printf("[ERR] agent: failed to sync remote state: %v", err)
173+
s.Logger.Error("failed to sync remote state", "error", err)
170174
return retryFullSyncState
171175
}
172176

@@ -196,7 +200,7 @@ func (s *StateSyncer) nextFSMState(fs fsmState) fsmState {
196200

197201
err := s.State.SyncChanges()
198202
if err != nil {
199-
s.Logger.Printf("[ERR] agent: failed to sync changes: %v", err)
203+
s.Logger.Error("failed to sync changes", "error", err)
200204
}
201205
return partialSyncState
202206

agent/ae/ae_test.go

+30-26
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package ae
33
import (
44
"errors"
55
"fmt"
6-
"log"
7-
"os"
86
"reflect"
97
"sync"
108
"testing"
119
"time"
1210

1311
"github.com/hashicorp/consul/lib"
12+
"github.com/hashicorp/consul/sdk/testutil"
13+
"github.com/hashicorp/go-hclog"
1414
"github.com/stretchr/testify/assert"
1515
)
1616

@@ -86,7 +86,7 @@ func TestAE_staggerDependsOnClusterSize(t *testing.T) {
8686
libRandomStagger = func(d time.Duration) time.Duration { return d }
8787
defer func() { libRandomStagger = lib.RandomStagger }()
8888

89-
l := testSyncer()
89+
l := testSyncer(t)
9090
if got, want := l.staggerFn(10*time.Millisecond), 10*time.Millisecond; got != want {
9191
t.Fatalf("got %v want %v", got, want)
9292
}
@@ -106,7 +106,7 @@ func TestAE_Run_SyncFullBeforeChanges(t *testing.T) {
106106
}
107107

108108
// indicate that we have partial changes before starting Run
109-
l := testSyncer()
109+
l := testSyncer(t)
110110
l.State = state
111111
l.ShutdownCh = shutdownCh
112112
l.SyncChanges.Trigger()
@@ -132,15 +132,15 @@ func TestAE_Run_Quit(t *testing.T) {
132132
t.Fatal("Run should panic")
133133
}
134134
}()
135-
l := testSyncer()
135+
l := testSyncer(t)
136136
l.ClusterSize = nil
137137
l.Run()
138138
})
139139
t.Run("runFSM quits", func(t *testing.T) {
140140
// start timer which explodes if runFSM does not quit
141141
tm := time.AfterFunc(time.Second, func() { panic("timeout") })
142142

143-
l := testSyncer()
143+
l := testSyncer(t)
144144
l.runFSM(fullSyncState, func(fsmState) fsmState { return doneState })
145145
// should just quit
146146
tm.Stop()
@@ -150,23 +150,23 @@ func TestAE_Run_Quit(t *testing.T) {
150150
func TestAE_FSM(t *testing.T) {
151151
t.Run("fullSyncState", func(t *testing.T) {
152152
t.Run("Paused -> retryFullSyncState", func(t *testing.T) {
153-
l := testSyncer()
153+
l := testSyncer(t)
154154
l.Pause()
155155
fs := l.nextFSMState(fullSyncState)
156156
if got, want := fs, retryFullSyncState; got != want {
157157
t.Fatalf("got state %v want %v", got, want)
158158
}
159159
})
160160
t.Run("SyncFull() error -> retryFullSyncState", func(t *testing.T) {
161-
l := testSyncer()
161+
l := testSyncer(t)
162162
l.State = &mock{syncFull: func() error { return errors.New("boom") }}
163163
fs := l.nextFSMState(fullSyncState)
164164
if got, want := fs, retryFullSyncState; got != want {
165165
t.Fatalf("got state %v want %v", got, want)
166166
}
167167
})
168168
t.Run("SyncFull() OK -> partialSyncState", func(t *testing.T) {
169-
l := testSyncer()
169+
l := testSyncer(t)
170170
l.State = &mock{}
171171
fs := l.nextFSMState(fullSyncState)
172172
if got, want := fs, partialSyncState; got != want {
@@ -178,7 +178,7 @@ func TestAE_FSM(t *testing.T) {
178178
t.Run("retryFullSyncState", func(t *testing.T) {
179179
// helper for testing state transitions from retrySyncFullState
180180
test := func(ev event, to fsmState) {
181-
l := testSyncer()
181+
l := testSyncer(t)
182182
l.retrySyncFullEvent = func() event { return ev }
183183
fs := l.nextFSMState(retryFullSyncState)
184184
if got, want := fs, to; got != want {
@@ -208,7 +208,7 @@ func TestAE_FSM(t *testing.T) {
208208
t.Run("partialSyncState", func(t *testing.T) {
209209
// helper for testing state transitions from partialSyncState
210210
test := func(ev event, to fsmState) {
211-
l := testSyncer()
211+
l := testSyncer(t)
212212
l.syncChangesEvent = func() event { return ev }
213213
fs := l.nextFSMState(partialSyncState)
214214
if got, want := fs, to; got != want {
@@ -225,7 +225,7 @@ func TestAE_FSM(t *testing.T) {
225225
test(syncFullTimerEvent, fullSyncState)
226226
})
227227
t.Run("syncChangesEvent+Paused -> partialSyncState", func(t *testing.T) {
228-
l := testSyncer()
228+
l := testSyncer(t)
229229
l.Pause()
230230
l.syncChangesEvent = func() event { return syncChangesNotifEvent }
231231
fs := l.nextFSMState(partialSyncState)
@@ -234,7 +234,7 @@ func TestAE_FSM(t *testing.T) {
234234
}
235235
})
236236
t.Run("syncChangesEvent+SyncChanges() error -> partialSyncState", func(t *testing.T) {
237-
l := testSyncer()
237+
l := testSyncer(t)
238238
l.State = &mock{syncChanges: func() error { return errors.New("boom") }}
239239
l.syncChangesEvent = func() event { return syncChangesNotifEvent }
240240
fs := l.nextFSMState(partialSyncState)
@@ -243,7 +243,7 @@ func TestAE_FSM(t *testing.T) {
243243
}
244244
})
245245
t.Run("syncChangesEvent+SyncChanges() OK -> partialSyncState", func(t *testing.T) {
246-
l := testSyncer()
246+
l := testSyncer(t)
247247
l.State = &mock{}
248248
l.syncChangesEvent = func() event { return syncChangesNotifEvent }
249249
fs := l.nextFSMState(partialSyncState)
@@ -268,14 +268,14 @@ func TestAE_FSM(t *testing.T) {
268268
t.Fatal("invalid state should panic")
269269
}
270270
}()
271-
l := testSyncer()
271+
l := testSyncer(t)
272272
l.nextFSMState(fsmState("invalid"))
273273
})
274274
}
275275

276276
func TestAE_RetrySyncFullEvent(t *testing.T) {
277277
t.Run("trigger shutdownEvent", func(t *testing.T) {
278-
l := testSyncer()
278+
l := testSyncer(t)
279279
l.ShutdownCh = make(chan struct{})
280280
evch := make(chan event)
281281
go func() { evch <- l.retrySyncFullEvent() }()
@@ -285,7 +285,7 @@ func TestAE_RetrySyncFullEvent(t *testing.T) {
285285
}
286286
})
287287
t.Run("trigger shutdownEvent during FullNotif", func(t *testing.T) {
288-
l := testSyncer()
288+
l := testSyncer(t)
289289
l.ShutdownCh = make(chan struct{})
290290
evch := make(chan event)
291291
go func() { evch <- l.retrySyncFullEvent() }()
@@ -297,7 +297,7 @@ func TestAE_RetrySyncFullEvent(t *testing.T) {
297297
}
298298
})
299299
t.Run("trigger syncFullNotifEvent", func(t *testing.T) {
300-
l := testSyncer()
300+
l := testSyncer(t)
301301
l.serverUpInterval = 10 * time.Millisecond
302302
evch := make(chan event)
303303
go func() { evch <- l.retrySyncFullEvent() }()
@@ -307,7 +307,7 @@ func TestAE_RetrySyncFullEvent(t *testing.T) {
307307
}
308308
})
309309
t.Run("trigger syncFullTimerEvent", func(t *testing.T) {
310-
l := testSyncer()
310+
l := testSyncer(t)
311311
l.retryFailInterval = 10 * time.Millisecond
312312
evch := make(chan event)
313313
go func() { evch <- l.retrySyncFullEvent() }()
@@ -319,7 +319,7 @@ func TestAE_RetrySyncFullEvent(t *testing.T) {
319319

320320
func TestAE_SyncChangesEvent(t *testing.T) {
321321
t.Run("trigger shutdownEvent", func(t *testing.T) {
322-
l := testSyncer()
322+
l := testSyncer(t)
323323
l.ShutdownCh = make(chan struct{})
324324
evch := make(chan event)
325325
go func() { evch <- l.syncChangesEvent() }()
@@ -329,7 +329,7 @@ func TestAE_SyncChangesEvent(t *testing.T) {
329329
}
330330
})
331331
t.Run("trigger shutdownEvent during FullNotif", func(t *testing.T) {
332-
l := testSyncer()
332+
l := testSyncer(t)
333333
l.ShutdownCh = make(chan struct{})
334334
evch := make(chan event)
335335
go func() { evch <- l.syncChangesEvent() }()
@@ -341,7 +341,7 @@ func TestAE_SyncChangesEvent(t *testing.T) {
341341
}
342342
})
343343
t.Run("trigger syncFullNotifEvent", func(t *testing.T) {
344-
l := testSyncer()
344+
l := testSyncer(t)
345345
l.serverUpInterval = 10 * time.Millisecond
346346
evch := make(chan event)
347347
go func() { evch <- l.syncChangesEvent() }()
@@ -351,7 +351,7 @@ func TestAE_SyncChangesEvent(t *testing.T) {
351351
}
352352
})
353353
t.Run("trigger syncFullTimerEvent", func(t *testing.T) {
354-
l := testSyncer()
354+
l := testSyncer(t)
355355
l.Interval = 10 * time.Millisecond
356356
evch := make(chan event)
357357
go func() { evch <- l.syncChangesEvent() }()
@@ -360,7 +360,7 @@ func TestAE_SyncChangesEvent(t *testing.T) {
360360
}
361361
})
362362
t.Run("trigger syncChangesNotifEvent", func(t *testing.T) {
363-
l := testSyncer()
363+
l := testSyncer(t)
364364
evch := make(chan event)
365365
go func() { evch <- l.syncChangesEvent() }()
366366
l.SyncChanges.Trigger()
@@ -391,8 +391,12 @@ func (m *mock) SyncChanges() error {
391391
return nil
392392
}
393393

394-
func testSyncer() *StateSyncer {
395-
logger := log.New(os.Stderr, "", 0)
394+
func testSyncer(t *testing.T) *StateSyncer {
395+
logger := hclog.New(&hclog.LoggerOptions{
396+
Level: 0,
397+
Output: testutil.TestWriter(t),
398+
})
399+
396400
l := NewStateSyncer(nil, time.Second, nil, logger)
397401
l.stagger = func(d time.Duration) time.Duration { return d }
398402
l.ClusterSize = func() int { return 1 }

0 commit comments

Comments
 (0)