Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/flow/logging/logging_test.go
4094 views
1
package logging_test
2
3
import (
4
"os"
5
6
"github.com/go-kit/log/level"
7
"github.com/grafana/agent/pkg/flow/logging"
8
)
9
10
func Example() {
11
// Create a sink to send logs to. WriterSink supports options to customize
12
// the logs sent to the sink.
13
sink, err := logging.WriterSink(os.Stdout, logging.SinkOptions{
14
Level: logging.LevelDebug,
15
Format: logging.FormatLogfmt,
16
IncludeTimestamps: false,
17
})
18
if err != nil {
19
panic(err)
20
}
21
22
// Create a controller logger. A controller logger is a logger with no
23
// component ID.
24
controller := logging.New(sink)
25
26
// Create two component loggers. The first component sends logs to the
27
// controller, and the other sends logs to the first component.
28
component1 := logging.New(logging.LoggerSink(controller), logging.WithComponentID("outer"))
29
component2 := logging.New(logging.LoggerSink(component1), logging.WithComponentID("inner"))
30
31
innerController := logging.New(logging.LoggerSink(component2))
32
33
// Log some log lines.
34
level.Info(controller).Log("msg", "hello from the controller!")
35
level.Info(component1).Log("msg", "hello from the outer component!")
36
level.Info(component2).Log("msg", "hello from the inner component!")
37
level.Info(innerController).Log("msg", "hello from the inner controller!")
38
39
// Output:
40
// level=info msg="hello from the controller!"
41
// component=outer level=info msg="hello from the outer component!"
42
// component=outer/inner level=info msg="hello from the inner component!"
43
// component=outer/inner level=info msg="hello from the inner controller!"
44
}
45
46