mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-16 21:56:40 -05:00
First try an exact lookup like before, but if it fails, strip the port and try again. example.com:1234 should still use a logger keyed for example.com if there is no key example.com:1234.
This commit is contained in:
parent
61b7002d26
commit
21c00a3cd2
1 changed files with 19 additions and 3 deletions
|
@ -453,11 +453,27 @@ func (slc ServerLogConfig) wrapLogger(logger *zap.Logger, host string) *zap.Logg
|
|||
}
|
||||
|
||||
func (slc ServerLogConfig) getLoggerName(host string) string {
|
||||
if loggerName, ok := slc.LoggerNames[host]; ok {
|
||||
tryHost := func(key string) (string, bool) {
|
||||
// first try exact match
|
||||
if loggerName, ok := slc.LoggerNames[key]; ok {
|
||||
return loggerName, ok
|
||||
}
|
||||
// strip port and try again (i.e. Host header of "example.com:1234" should
|
||||
// match "example.com" if there is no "example.com:1234" in the map)
|
||||
hostOnly, _, err := net.SplitHostPort(key)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
loggerName, ok := slc.LoggerNames[hostOnly]
|
||||
return loggerName, ok
|
||||
}
|
||||
|
||||
// try the exact hostname first
|
||||
if loggerName, ok := tryHost(host); ok {
|
||||
return loggerName
|
||||
}
|
||||
|
||||
// Try matching wildcard domains if other non-specific loggers exist
|
||||
// try matching wildcard domains if other non-specific loggers exist
|
||||
labels := strings.Split(host, ".")
|
||||
for i := range labels {
|
||||
if labels[i] == "" {
|
||||
|
@ -465,7 +481,7 @@ func (slc ServerLogConfig) getLoggerName(host string) string {
|
|||
}
|
||||
labels[i] = "*"
|
||||
wildcardHost := strings.Join(labels, ".")
|
||||
if loggerName, ok := slc.LoggerNames[wildcardHost]; ok {
|
||||
if loggerName, ok := tryHost(wildcardHost); ok {
|
||||
return loggerName
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue