-
Notifications
You must be signed in to change notification settings - Fork 98
/
test_logger.go
45 lines (37 loc) · 970 Bytes
/
test_logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package util
import (
"os"
"testing"
"time"
"github.com/go-kit/log"
"github.com/grafana/alloy/internal/alloy/logging"
"github.com/stretchr/testify/require"
)
// TestLogger generates a logger for a test.
func TestLogger(t testing.TB) log.Logger {
t.Helper()
l := log.NewSyncLogger(log.NewLogfmtLogger(os.Stderr))
l = log.WithPrefix(l,
"test", t.Name(),
"ts", log.Valuer(testTimestamp),
)
return l
}
// TestAlloyLogger generates an Alloy-compatible logger for a test.
func TestAlloyLogger(t require.TestingT) *logging.Logger {
if t, ok := t.(*testing.T); ok {
t.Helper()
}
l, err := logging.New(os.Stderr, logging.Options{
Level: logging.LevelDebug,
Format: logging.FormatLogfmt,
})
require.NoError(t, err)
return l
}
// testTimestamp is a log.Valuer that returns the timestamp
// without the date or timezone, reducing the noise in the test.
func testTimestamp() interface{} {
t := time.Now().UTC()
return t.Format("15:04:05.000")
}