-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoutput.go
157 lines (137 loc) · 3.32 KB
/
output.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// SPDX-FileCopyrightText: 2022-present Open Networking Foundation <[email protected]>
//
// SPDX-License-Identifier: Apache-2.0
package dazl
import (
"fmt"
"gopkg.in/yaml.v3"
)
type outputsConfig struct {
Outputs map[string]outputSchema
}
func (c *outputsConfig) UnmarshalYAML(unmarshal func(any) error) error {
config := make(map[string]outputSchema)
if err := unmarshal(&config); err != nil {
var config []outputConfig
if err := unmarshal(&config); err != nil {
return err
}
c.Outputs = make(map[string]outputSchema)
for _, output := range config {
c.Outputs[output.Writer] = outputSchema{
Level: output.Level,
Sample: output.Sample,
}
}
return nil
}
c.Outputs = config
return nil
}
type outputConfig struct {
Writer string `json:"writer" yaml:"writer"`
Level levelConfig `json:"level" yaml:"level"`
Sample samplingConfig `json:"sample" yaml:"sample"`
}
func (c *outputConfig) UnmarshalYAML(unmarshal func(any) error) error {
config := make(map[string]any)
if err := unmarshal(&config); err != nil {
var text string
if err := unmarshal(&text); err != nil {
return err
}
return c.UnmarshalText([]byte(text))
}
if len(config) > 1 {
return fmt.Errorf("logger outputs must configure one writer per list item")
}
for key, value := range config {
text, err := yaml.Marshal(value)
if err != nil {
return err
}
var schema outputSchema
if err := yaml.Unmarshal(text, &schema); err != nil {
return err
}
c.Writer = key
c.Level = schema.Level
c.Sample = schema.Sample
}
return nil
}
func (c *outputConfig) UnmarshalText(text []byte) error {
c.Writer = string(text)
return nil
}
type outputSchema struct {
Level levelConfig `json:"level" yaml:"level"`
Sample samplingConfig `json:"sample" yaml:"sample"`
}
func newOutput(writer Writer, level Level, sampler Sampler) *dazlOutput {
return &dazlOutput{
writer: writer,
level: level,
sampler: sampler,
}
}
// dazlOutput is a dazl output implementation
type dazlOutput struct {
writer Writer
level Level
sampler Sampler
}
func (o *dazlOutput) WithWriter(writer Writer) *dazlOutput {
return &dazlOutput{
writer: writer,
level: o.level,
sampler: o.sampler,
}
}
func (o *dazlOutput) Level() Level {
return o.level
}
func (o *dazlOutput) WithLevel(level Level) *dazlOutput {
return &dazlOutput{
writer: o.writer,
level: level,
sampler: o.sampler,
}
}
func (o *dazlOutput) WithSampler(sampler Sampler) *dazlOutput {
return &dazlOutput{
writer: o.writer,
level: o.level,
sampler: sampler,
}
}
func (o *dazlOutput) Debug(msg string) {
if o.level.Enabled(DebugLevel) && o.sampler.Sample(DebugLevel) {
o.writer.Debug(msg)
}
}
func (o *dazlOutput) Info(msg string) {
if o.level.Enabled(InfoLevel) && o.sampler.Sample(InfoLevel) {
o.writer.Info(msg)
}
}
func (o *dazlOutput) Warn(msg string) {
if o.level.Enabled(WarnLevel) && o.sampler.Sample(WarnLevel) {
o.writer.Warn(msg)
}
}
func (o *dazlOutput) Error(msg string) {
if o.level.Enabled(ErrorLevel) && o.sampler.Sample(ErrorLevel) {
o.writer.Error(msg)
}
}
func (o *dazlOutput) Fatal(msg string) {
if o.level.Enabled(FatalLevel) && o.sampler.Sample(FatalLevel) {
o.writer.Fatal(msg)
}
}
func (o *dazlOutput) Panic(msg string) {
if o.level.Enabled(PanicLevel) && o.sampler.Sample(PanicLevel) {
o.writer.Panic(msg)
}
}