-
Notifications
You must be signed in to change notification settings - Fork 18
/
getters.go
128 lines (117 loc) · 3.32 KB
/
getters.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
package config
import (
"os"
"path/filepath"
"time"
"github.com/rs/zerolog"
)
var (
CompatibilityPolicies = map[string]CompatibilityPolicy{
"strict": Strict,
"loose": Loose,
}
TerminationPolicies = map[string]TerminationPolicy{
"continue": Continue,
"stop": Stop,
}
logOutputs = map[string]LogOutput{
"console": Console,
"stdout": Stdout,
"stderr": Stderr,
"file": File,
"syslog": Syslog,
"rsyslog": RSyslog,
}
TimeFormats = map[string]string{
"": zerolog.TimeFormatUnix,
"unix": zerolog.TimeFormatUnix,
"unixms": zerolog.TimeFormatUnixMs,
"unixmicro": zerolog.TimeFormatUnixMicro,
"unixnano": zerolog.TimeFormatUnixNano,
}
ConsoleTimeFormats = map[string]string{
"Layout": time.Layout,
"ANSIC": time.ANSIC,
"UnixDate": time.UnixDate,
"RubyDate": time.RubyDate,
"RFC822": time.RFC822,
"RFC822Z": time.RFC822Z,
"RFC850": time.RFC850,
"RFC1123": time.RFC1123,
"RFC1123Z": time.RFC1123Z,
"RFC3339": time.RFC3339,
"RFC3339Nano": time.RFC3339Nano,
"Kitchen": time.Kitchen,
"Stamp": time.Stamp,
"StampMilli": time.StampMilli,
"StampMicro": time.StampMicro,
"StampNano": time.StampNano,
}
LogLevels = map[string]zerolog.Level{
"trace": zerolog.TraceLevel,
"debug": zerolog.DebugLevel,
"info": zerolog.InfoLevel,
"warn": zerolog.WarnLevel,
"error": zerolog.ErrorLevel,
"fatal": zerolog.FatalLevel,
"panic": zerolog.PanicLevel,
"disabled": zerolog.Disabled,
}
)
// GetOutput returns the logger output from config file.
func (l Logger) GetOutput() []LogOutput {
var outputs []LogOutput
for _, output := range l.Output {
if logOutput, ok := logOutputs[output]; ok {
outputs = append(outputs, logOutput)
} else {
outputs = append(outputs, Console)
}
}
if len(outputs) == 0 {
outputs = append(outputs, Console)
}
return outputs
}
// GetPlugins returns the plugins from config file.
func (p PluginConfig) GetPlugins(name ...string) []Plugin {
var plugins []Plugin
for _, plugin := range p.Plugins {
for _, n := range name {
if plugin.Name == n {
plugins = append(plugins, plugin)
}
}
}
return plugins
}
// GetDefaultConfigFilePath returns the path of the default config file.
func GetDefaultConfigFilePath(filename string) string {
// Try to find the config file in the current directory.
path := filepath.Join("./", filename)
if _, err := os.Stat(path); !os.IsNotExist(err) {
return path
}
// Try to find the config file in the /etc directory.
path = filepath.Join("/etc/", filename)
if _, err := os.Stat(path); !os.IsNotExist(err) {
return path
}
// The fallback is the current directory.
return filepath.Join("./", filename)
}
// Filter returns a filtered global config based on the group name.
func (gc GlobalConfig) Filter(groupName string) *GlobalConfig {
if _, ok := gc.Servers[groupName]; !ok {
return nil
}
return &GlobalConfig{
Loggers: map[string]*Logger{groupName: gc.Loggers[groupName]},
Clients: map[string]*Client{groupName: gc.Clients[groupName]},
Pools: map[string]*Pool{groupName: gc.Pools[groupName]},
Proxies: map[string]*Proxy{groupName: gc.Proxies[groupName]},
Servers: map[string]*Server{groupName: gc.Servers[groupName]},
Metrics: map[string]*Metrics{groupName: gc.Metrics[groupName]},
API: gc.API,
}
}