@@ -37,10 +37,16 @@ type ServerLogConfig struct {
37
37
DefaultLoggerName string `json:"default_logger_name,omitempty"`
38
38
39
39
// LoggerNames maps request hostnames to one or more custom logger
40
- // names. For example, a mapping of "example.com" to "example" would
40
+ // names. For example, a mapping of ` "example.com": [ "example"]` would
41
41
// cause access logs from requests with a Host of example.com to be
42
42
// emitted by a logger named "http.log.access.example". If there are
43
43
// multiple logger names, then the log will be emitted to all of them.
44
+ // If the logger name is an empty, the default logger is used, i.e.
45
+ // the logger "http.log.access".
46
+ //
47
+ // Keys must be hostnames (without ports), and may contain wildcards
48
+ // to match subdomains. The value is an array of logger names.
49
+ //
44
50
// For backwards compatibility, if the value is a string, it is treated
45
51
// as a single-element array.
46
52
LoggerNames map [string ]StringArray `json:"logger_names,omitempty"`
@@ -64,10 +70,19 @@ type ServerLogConfig struct {
64
70
// wrapLogger wraps logger in one or more logger named
65
71
// according to user preferences for the given host.
66
72
func (slc ServerLogConfig ) wrapLogger (logger * zap.Logger , host string ) []* zap.Logger {
67
- hosts := slc .getLoggerHosts (host )
73
+ // logger config should always be only
74
+ // the hostname, without the port
75
+ hostWithoutPort , _ , err := net .SplitHostPort (host )
76
+ if err != nil {
77
+ hostWithoutPort = host
78
+ }
79
+
80
+ hosts := slc .getLoggerHosts (hostWithoutPort )
68
81
loggers := make ([]* zap.Logger , 0 , len (hosts ))
69
82
for _ , loggerName := range hosts {
83
+ // no name, use the default logger
70
84
if loggerName == "" {
85
+ loggers = append (loggers , logger )
71
86
continue
72
87
}
73
88
loggers = append (loggers , logger .Named (loggerName ))
@@ -76,23 +91,8 @@ func (slc ServerLogConfig) wrapLogger(logger *zap.Logger, host string) []*zap.Lo
76
91
}
77
92
78
93
func (slc ServerLogConfig ) getLoggerHosts (host string ) []string {
79
- tryHost := func (key string ) ([]string , bool ) {
80
- // first try exact match
81
- if hosts , ok := slc .LoggerNames [key ]; ok {
82
- return hosts , ok
83
- }
84
- // strip port and try again (i.e. Host header of "example.com:1234" should
85
- // match "example.com" if there is no "example.com:1234" in the map)
86
- hostOnly , _ , err := net .SplitHostPort (key )
87
- if err != nil {
88
- return []string {}, false
89
- }
90
- hosts , ok := slc .LoggerNames [hostOnly ]
91
- return hosts , ok
92
- }
93
-
94
94
// try the exact hostname first
95
- if hosts , ok := tryHost ( host ) ; ok {
95
+ if hosts , ok := slc . LoggerNames [ host ] ; ok {
96
96
return hosts
97
97
}
98
98
@@ -104,7 +104,7 @@ func (slc ServerLogConfig) getLoggerHosts(host string) []string {
104
104
}
105
105
labels [i ] = "*"
106
106
wildcardHost := strings .Join (labels , "." )
107
- if hosts , ok := tryHost ( wildcardHost ) ; ok {
107
+ if hosts , ok := slc . LoggerNames [ wildcardHost ] ; ok {
108
108
return hosts
109
109
}
110
110
}
0 commit comments