-
Notifications
You must be signed in to change notification settings - Fork 485
/
options.go
116 lines (96 loc) · 2.47 KB
/
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
package logging
import (
"encoding"
"fmt"
"github.com/go-kit/log/level"
"github.com/grafana/agent/pkg/river"
)
// Options is a set of options used to construct and configure a Logger.
type Options struct {
Level Level `river:"level,attr,optional"`
Format Format `river:"format,attr,optional"`
// TODO: log sink parameter (e.g., to use the Windows Event logger)
}
// DefaultOptions holds defaults for creating a Logger.
var DefaultOptions = Options{
Level: LevelDefault,
Format: FormatDefault,
}
var _ river.Defaulter = (*Options)(nil)
// SetToDefault implements river.Defaulter.
func (o *Options) SetToDefault() {
*o = DefaultOptions
}
// 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
}