-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
87 lines (72 loc) · 2.63 KB
/
config.go
File metadata and controls
87 lines (72 loc) · 2.63 KB
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
package logging
import (
"fmt"
"github.com/pkg/errors"
"go.uber.org/zap/zapcore"
"os"
"strings"
"time"
)
// Options define child loggers with their desired log level.
type Options map[string]zapcore.Level
// UnmarshalText implements encoding.TextUnmarshaler to allow Options to be parsed by env.
//
// This custom TextUnmarshaler is necessary as - for the moment - env does not support map[T]encoding.TextUnmarshaler.
// After <https://github.com/caarlos0/env/pull/323> got merged and a new env release was drafted, this method can be
// removed.
func (o *Options) UnmarshalText(text []byte) error {
optionsMap := make(map[string]zapcore.Level)
for _, entry := range strings.Split(string(text), ",") {
key, valueStr, found := strings.Cut(entry, ":")
if !found {
return fmt.Errorf("entry %q cannot be unmarshalled as an Option entry", entry)
}
valueLvl, err := zapcore.ParseLevel(valueStr)
if err != nil {
return fmt.Errorf("entry %q cannot be unmarshalled as level, %w", entry, err)
}
optionsMap[key] = valueLvl
}
*o = optionsMap
return nil
}
// Config defines Logger configuration.
type Config struct {
// zapcore.Level at 0 is for info level.
Level zapcore.Level `yaml:"level" env:"LEVEL" default:"0"`
Output string `yaml:"output" env:"OUTPUT"`
// Interval for periodic logging.
Interval time.Duration `yaml:"interval" env:"INTERVAL" default:"20s"`
Options `yaml:"options" env:"OPTIONS"`
}
// Validate checks constraints in the configuration and returns an error if they are violated.
// Also configures the log output if it is not configured:
// systemd-journald is used when Icinga DB is running under systemd, otherwise stderr.
func (l *Config) Validate() error {
if l.Interval <= 0 {
return errors.New("periodic logging interval must be positive")
}
if l.Output == "" {
if _, ok := os.LookupEnv("NOTIFY_SOCKET"); ok {
// When started by systemd, NOTIFY_SOCKET is set by systemd for Type=notify supervised services,
// which is the default setting for the Icinga DB service.
// This assumes that Icinga DB is running under systemd, so set output to systemd-journald.
l.Output = JOURNAL
} else {
// Otherwise set it to console, i.e. write log messages to stderr.
l.Output = CONSOLE
}
}
// To be on the safe side, always call AssertOutput.
return AssertOutput(l.Output)
}
// AssertOutput returns an error if output is not a valid logger output.
func AssertOutput(o string) error {
if o == CONSOLE || o == JOURNAL {
return nil
}
return invalidOutput(o)
}
func invalidOutput(o string) error {
return fmt.Errorf("%s is not a valid logger output. Must be either %q or %q", o, CONSOLE, JOURNAL)
}