-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption_syslog_url.go
53 lines (41 loc) · 1.25 KB
/
option_syslog_url.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
package log
import (
"os"
"regexp"
)
type withSyslogDaemonURL string
func (w withSyslogDaemonURL) applySyslog(l *syslogLogger) error {
if w == "" {
l.network, l.addr = "", ""
return nil
}
matches := syslogDaemonURLRegex.FindStringSubmatch(string(w))
if len(matches) == 0 {
return ErrBadSyslogDaemonURL
}
l.network, l.addr = matches[1], matches[2]
return nil
}
func (w withSyslogDaemonURL) applyStdLog(*stdLogger) error {
return ErrIncompatibleOption
}
// WithSyslogDaemonURL specifies the syslog daemon URL for syslog logger.
func WithSyslogDaemonURL(url string) Option {
return withSyslogDaemonURL(url)
}
// WithSyslogDaemonURLFromEnv makes a WithSyslogDaemonURL option based on the specified environment variable env or
// defaultUrl if no environment variable was found.
func WithSyslogDaemonURLFromEnv(env, defaultUrl string) OptionLoader {
return func() (Option, error) {
url := defaultUrl
if value, found := os.LookupEnv(env); found {
if !syslogDaemonURLRegex.MatchString(value) {
return nil, ErrBadSyslogDaemonURL
}
url = value
}
return withSyslogDaemonURL(url), nil
}
}
var syslogDaemonURLRegex = regexp.MustCompile(`^(tcp[46]?|udp[46]?|unix(?:gram|packet)?)://([^:]+(?::\d)?)$`)
var _ Option = withSyslogDaemonURL("")