Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/flow/logging/sink_options.go
4094 views
1
package logging
2
3
import (
4
"encoding"
5
"fmt"
6
7
"github.com/go-kit/log/level"
8
"github.com/grafana/agent/pkg/river"
9
)
10
11
// SinkOptions is a set of options used to construct and configure a logging
12
// sink.
13
type SinkOptions struct {
14
Level Level `river:"level,attr,optional"`
15
Format Format `river:"format,attr,optional"`
16
17
// IncludeTimestamps disables timestamps on log lines. It is not exposed as a
18
// river tag as it is only expected to be used during tests.
19
IncludeTimestamps bool
20
21
// TODO: attributes to forward logs to loki.* components.
22
}
23
24
// DefaultSinkOptions holds defaults for creating a logging sink.
25
var DefaultSinkOptions = SinkOptions{
26
Level: LevelDefault,
27
Format: FormatDefault,
28
29
IncludeTimestamps: true,
30
}
31
32
var _ river.Unmarshaler = (*SinkOptions)(nil)
33
34
// UnmarshalRiver implements river.Unmarshaler.
35
func (o *SinkOptions) UnmarshalRiver(f func(interface{}) error) error {
36
*o = DefaultSinkOptions
37
38
type options SinkOptions
39
return f((*options)(o))
40
}
41
42
// Level represents how verbose logging should be.
43
type Level string
44
45
// Supported log levels
46
const (
47
LevelDebug Level = "debug"
48
LevelInfo Level = "info"
49
LevelWarn Level = "warn"
50
LevelError Level = "error"
51
52
LevelDefault = LevelInfo
53
)
54
55
var (
56
_ encoding.TextMarshaler = LevelDefault
57
_ encoding.TextUnmarshaler = (*Level)(nil)
58
)
59
60
// MarshalText implements encoding.TextMarshaler.
61
func (ll Level) MarshalText() (text []byte, err error) {
62
return []byte(ll), nil
63
}
64
65
// UnmarshalText implements encoding.TextUnmarshaler.
66
func (ll *Level) UnmarshalText(text []byte) error {
67
switch Level(text) {
68
case "":
69
*ll = LevelDefault
70
case LevelDebug, LevelInfo, LevelWarn, LevelError:
71
*ll = Level(text)
72
default:
73
return fmt.Errorf("unrecognized log level %q", string(text))
74
}
75
return nil
76
}
77
78
// Filter returns a go-kit logging filter from the level.
79
func (ll Level) Filter() level.Option {
80
switch ll {
81
case LevelDebug:
82
return level.AllowDebug()
83
case LevelInfo:
84
return level.AllowInfo()
85
case LevelWarn:
86
return level.AllowWarn()
87
case LevelError:
88
return level.AllowError()
89
default:
90
return level.AllowAll()
91
}
92
}
93
94
// Format represents a text format to use when writing logs.
95
type Format string
96
97
// Supported log formats.
98
const (
99
FormatLogfmt Format = "logfmt"
100
FormatJSON Format = "json"
101
102
FormatDefault = FormatLogfmt
103
)
104
105
var (
106
_ encoding.TextMarshaler = FormatDefault
107
_ encoding.TextUnmarshaler = (*Format)(nil)
108
)
109
110
// MarshalText implements encoding.TextMarshaler.
111
func (ll Format) MarshalText() (text []byte, err error) {
112
return []byte(ll), nil
113
}
114
115
// UnmarshalText implements encoding.TextUnmarshaler.
116
func (ll *Format) UnmarshalText(text []byte) error {
117
switch Format(text) {
118
case "":
119
*ll = FormatDefault
120
case FormatLogfmt, FormatJSON:
121
*ll = Format(text)
122
default:
123
return fmt.Errorf("unrecognized log format %q", string(text))
124
}
125
return nil
126
}
127
128