Debian Codegraph @

event/adapter/logrus/logrus_test.go

raw symbols SCIP
1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build !disable_events
6
7 package logrus_test
8
9 import (
10 "io"
11 "testing"
12
13 "github.com/google/go-cmp/cmp"
14 "github.com/sirupsen/logrus"
15 "golang.org/x/exp/event"
16 elogrus "golang.org/x/exp/event/adapter/logrus"
17 "golang.org/x/exp/event/eventtest"
18 "golang.org/x/exp/event/severity"
19 )
20
21 func Test(t *testing.T) {
22 ctx, th := eventtest.NewCapture()
23 log := logrus.New()
24 log.SetFormatter(elogrus.NewFormatter())
25 log.SetOutput(io.Discard)
26 // adding WithContext panics, because event.FromContext assumes
27 log.WithContext(ctx).WithField("traceID", 17).WithField("resource", "R").Info("mess")
28
29 want := []event.Event{{
30 ID: 1,
31 Kind: event.LogKind,
32 Labels: []event.Label{
33 severity.Info.Label(),
34 event.Value("traceID", 17),
35 event.Value("resource", "R"),
36 event.String("msg", "mess"),
37 },
38 }}
39 // logrus fields are stored in a map, so we have to sort to overcome map
40 // iteration indeterminacy.
41 if diff := cmp.Diff(want, th.Got, eventtest.CmpOptions()...); diff != "" {
42 t.Errorf("mismatch (-want, +got):\n%s", diff)
43 }
44 }