-
Notifications
You must be signed in to change notification settings - Fork 478
/
sink_options.go
126 lines (103 loc) · 2.79 KB
/
sink_options.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package logging
import (
"encoding"
"fmt"
"github.com/go-kit/log/level"
"github.com/grafana/agent/pkg/river"
)
// SinkOptions is a set of options used to construct and configure a logging
// sink.
type SinkOptions struct {
Level Level `river:"level,attr,optional"`
Format Format `river:"format,attr,optional"`
// IncludeTimestamps disables timestamps on log lines. It is not exposed as a
// river tag as it is only expected to be used during tests.
IncludeTimestamps bool
// TODO: attributes to forward logs to loki.* components.
}
// DefaultSinkOptions holds defaults for creating a logging sink.
var DefaultSinkOptions = SinkOptions{
Level: LevelDefault,
Format: FormatDefault,
IncludeTimestamps: true,
}
var _ river.Unmarshaler = (*SinkOptions)(nil)
// UnmarshalRiver implements river.Unmarshaler.
func (o *SinkOptions) UnmarshalRiver(f func(interface{}) error) error {
*o = DefaultSinkOptions
type options SinkOptions
return f((*options)(o))
}
// Level represents how verbose logging should be.
type Level string
// Supported log levels
const (
LevelDebug Level = "debug"
LevelInfo Level = "info"
LevelWarn Level = "warn"
LevelError Level = "error"
LevelDefault = LevelInfo
)
var (
_ encoding.TextMarshaler = LevelDefault
_ encoding.TextUnmarshaler = (*Level)(nil)
)
// MarshalText implements encoding.TextMarshaler.
func (ll Level) MarshalText() (text []byte, err error) {
return []byte(ll), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (ll *Level) UnmarshalText(text []byte) error {
switch Level(text) {
case "":
*ll = LevelDefault
case LevelDebug, LevelInfo, LevelWarn, LevelError:
*ll = Level(text)
default:
return fmt.Errorf("unrecognized log level %q", string(text))
}
return nil
}
// Filter returns a go-kit logging filter from the level.
func (ll Level) Filter() level.Option {
switch ll {
case LevelDebug:
return level.AllowDebug()
case LevelInfo:
return level.AllowInfo()
case LevelWarn:
return level.AllowWarn()
case LevelError:
return level.AllowError()
default:
return level.AllowAll()
}
}
// Format represents a text format to use when writing logs.
type Format string
// Supported log formats.
const (
FormatLogfmt Format = "logfmt"
FormatJSON Format = "json"
FormatDefault = FormatLogfmt
)
var (
_ encoding.TextMarshaler = FormatDefault
_ encoding.TextUnmarshaler = (*Format)(nil)
)
// MarshalText implements encoding.TextMarshaler.
func (ll Format) MarshalText() (text []byte, err error) {
return []byte(ll), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (ll *Format) UnmarshalText(text []byte) error {
switch Format(text) {
case "":
*ll = FormatDefault
case FormatLogfmt, FormatJSON:
*ll = Format(text)
default:
return fmt.Errorf("unrecognized log format %q", string(text))
}
return nil
}