Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/util/test_logger.go
4093 views
1
package util
2
3
import (
4
"os"
5
"testing"
6
"time"
7
8
"github.com/go-kit/log"
9
"github.com/grafana/agent/pkg/flow/logging"
10
"github.com/stretchr/testify/require"
11
)
12
13
// TestLogger generates a logger for a test.
14
func TestLogger(t *testing.T) log.Logger {
15
t.Helper()
16
17
l := log.NewSyncLogger(log.NewLogfmtLogger(os.Stderr))
18
l = log.WithPrefix(l,
19
"test", t.Name(),
20
"ts", log.Valuer(testTimestamp),
21
)
22
23
return l
24
}
25
26
// TestFlowLogger generates a Flow-compatible logger for a test.
27
func TestFlowLogger(t require.TestingT) *logging.Logger {
28
if t, ok := t.(*testing.T); ok {
29
t.Helper()
30
}
31
32
sink, err := logging.WriterSink(os.Stderr, logging.SinkOptions{
33
Level: logging.LevelDebug,
34
Format: logging.FormatLogfmt,
35
})
36
require.NoError(t, err)
37
38
return logging.New(sink)
39
}
40
41
// testTimestamp is a log.Valuer that returns the timestamp
42
// without the date or timezone, reducing the noise in the test.
43
func testTimestamp() interface{} {
44
t := time.Now().UTC()
45
return t.Format("15:04:05.000")
46
}
47
48