-
Notifications
You must be signed in to change notification settings - Fork 3
/
sinks_output.go
94 lines (77 loc) · 2.02 KB
/
sinks_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
package log
import (
"fmt"
"os"
"strconv"
"strings"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/sourcegraph/log/internal/sinkcores/outputcore"
"github.com/sourcegraph/log/internal/stderr"
"github.com/sourcegraph/log/output"
)
type outputSink struct {
development bool
}
func (s *outputSink) Name() string { return "OutputSink" }
func (s *outputSink) build() (zapcore.Core, error) {
w, err := stderr.Open()
if err != nil {
return nil, err
}
level := zap.NewAtomicLevelAt(Level(os.Getenv(EnvLogLevel)).Parse())
format := output.ParseFormat(os.Getenv(EnvLogFormat))
if s.development {
format = output.FormatConsole
}
sampling, err := parseSamplingConfig()
if err != nil {
return nil, err
}
overrides, err := parseOverrides()
if err != nil {
return nil, err
}
return outputcore.NewCore(w, level, format, sampling, overrides, s.development), nil
}
// update is a no-op because outputSink cannot be changed live.
func (s *outputSink) update(updated SinksConfig) error { return nil }
func parseSamplingConfig() (config zap.SamplingConfig, err error) {
if val, set := os.LookupEnv(EnvLogSamplingInitial); set {
config.Initial, err = strconv.Atoi(val)
if err != nil {
err = fmt.Errorf("SRC_LOG_SAMPLING_INITIAL is invalid: %w", err)
return
}
} else {
config.Initial = 100
}
if val, set := os.LookupEnv(EnvLogSamplingInitial); set {
config.Thereafter, err = strconv.Atoi(val)
if err != nil {
err = fmt.Errorf("SRC_LOG_SAMPLING_THEREAFTER is invalid: %w", err)
return
}
} else {
config.Thereafter = 100
}
return
}
func parseOverrides() ([]outputcore.Override, error) {
raw := os.Getenv(EnvLogScopeLevel)
var overrides []outputcore.Override
for _, kv := range strings.Split(raw, ",") {
if kv == "" {
continue
}
p := strings.SplitN(kv, "=", 2)
if len(p) != 2 {
return nil, fmt.Errorf("%s=%q is invalid", EnvLogScopeLevel, raw)
}
overrides = append(overrides, outputcore.Override{
Scope: p[0],
Level: Level(p[1]).Parse(),
})
}
return overrides, nil
}