Skip to content

Commit 726a9a8

Browse files
logging: Fix default access logger (#6251)
* logging: Fix default access logger * Simplify logic, remove retry without port, reject config with port, docs * Nil check
1 parent d00824f commit 726a9a8

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

modules/caddyhttp/app.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,10 @@ func (app *App) Provision(ctx caddy.Context) error {
329329

330330
// Validate ensures the app's configuration is valid.
331331
func (app *App) Validate() error {
332-
// each server must use distinct listener addresses
333332
lnAddrs := make(map[string]string)
333+
334334
for srvName, srv := range app.Servers {
335+
// each server must use distinct listener addresses
335336
for _, addr := range srv.Listen {
336337
listenAddr, err := caddy.ParseNetworkAddress(addr)
337338
if err != nil {
@@ -347,6 +348,15 @@ func (app *App) Validate() error {
347348
lnAddrs[addr] = srvName
348349
}
349350
}
351+
352+
// logger names must not have ports
353+
if srv.Logs != nil {
354+
for host := range srv.Logs.LoggerNames {
355+
if _, _, err := net.SplitHostPort(host); err == nil {
356+
return fmt.Errorf("server %s: logger name must not have a port: %s", srvName, host)
357+
}
358+
}
359+
}
350360
}
351361
return nil
352362
}

modules/caddyhttp/logging.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@ type ServerLogConfig struct {
3737
DefaultLoggerName string `json:"default_logger_name,omitempty"`
3838

3939
// 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
4141
// cause access logs from requests with a Host of example.com to be
4242
// emitted by a logger named "http.log.access.example". If there are
4343
// 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+
//
4450
// For backwards compatibility, if the value is a string, it is treated
4551
// as a single-element array.
4652
LoggerNames map[string]StringArray `json:"logger_names,omitempty"`
@@ -64,10 +70,19 @@ type ServerLogConfig struct {
6470
// wrapLogger wraps logger in one or more logger named
6571
// according to user preferences for the given host.
6672
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)
6881
loggers := make([]*zap.Logger, 0, len(hosts))
6982
for _, loggerName := range hosts {
83+
// no name, use the default logger
7084
if loggerName == "" {
85+
loggers = append(loggers, logger)
7186
continue
7287
}
7388
loggers = append(loggers, logger.Named(loggerName))
@@ -76,23 +91,8 @@ func (slc ServerLogConfig) wrapLogger(logger *zap.Logger, host string) []*zap.Lo
7691
}
7792

7893
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-
9494
// try the exact hostname first
95-
if hosts, ok := tryHost(host); ok {
95+
if hosts, ok := slc.LoggerNames[host]; ok {
9696
return hosts
9797
}
9898

@@ -104,7 +104,7 @@ func (slc ServerLogConfig) getLoggerHosts(host string) []string {
104104
}
105105
labels[i] = "*"
106106
wildcardHost := strings.Join(labels, ".")
107-
if hosts, ok := tryHost(wildcardHost); ok {
107+
if hosts, ok := slc.LoggerNames[wildcardHost]; ok {
108108
return hosts
109109
}
110110
}

modules/caddyhttp/server.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,13 +717,20 @@ func (s *Server) shouldLogRequest(r *http.Request) bool {
717717
// logging is disabled
718718
return false
719719
}
720-
if _, ok := s.Logs.LoggerNames[r.Host]; ok {
720+
721+
// strip off the port if any, logger names are host only
722+
hostWithoutPort, _, err := net.SplitHostPort(r.Host)
723+
if err != nil {
724+
hostWithoutPort = r.Host
725+
}
726+
727+
if _, ok := s.Logs.LoggerNames[hostWithoutPort]; ok {
721728
// this host is mapped to a particular logger name
722729
return true
723730
}
724731
for _, dh := range s.Logs.SkipHosts {
725732
// logging for this particular host is disabled
726-
if certmagic.MatchWildcard(r.Host, dh) {
733+
if certmagic.MatchWildcard(hostWithoutPort, dh) {
727734
return false
728735
}
729736
}

0 commit comments

Comments
 (0)