-
Notifications
You must be signed in to change notification settings - Fork 478
/
options.go
120 lines (99 loc) · 2.48 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
117
118
119
120
package logging
import (
"encoding"
"fmt"
"log/slog"
"math"
"github.com/grafana/agent/component/common/loki"
"github.com/grafana/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"`
WriteTo []loki.LogsReceiver `river:"write_to,attr,optional"`
}
// 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
}
type slogLevel Level
func (l slogLevel) Level() slog.Level {
switch Level(l) {
case LevelDebug:
return slog.LevelDebug
case LevelInfo:
return slog.LevelInfo
case LevelWarn:
return slog.LevelWarn
case LevelError:
return slog.LevelError
default:
// Allow all logs.
return slog.Level(math.MinInt)
}
}
// 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
}