Skip to content

Commit

Permalink
logging: Fix default access logger
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed Apr 21, 2024
1 parent d00824f commit a1a5ec6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
10 changes: 9 additions & 1 deletion modules/caddyhttp/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,18 @@ type ServerLogConfig struct {
// wrapLogger wraps logger in one or more logger named
// according to user preferences for the given host.
func (slc ServerLogConfig) wrapLogger(logger *zap.Logger, host string) []*zap.Logger {
hosts := slc.getLoggerHosts(host)
// strip off the port if any
hostWithoutPort, _, err := net.SplitHostPort(host)
if err != nil {
hostWithoutPort = host
}

hosts := slc.getLoggerHosts(hostWithoutPort)
loggers := make([]*zap.Logger, 0, len(hosts))
for _, loggerName := range hosts {
// no name, use the default logger
if loggerName == "" {
loggers = append(loggers, logger)
continue
}
loggers = append(loggers, logger.Named(loggerName))
Expand Down
11 changes: 9 additions & 2 deletions modules/caddyhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,13 +717,20 @@ func (s *Server) shouldLogRequest(r *http.Request) bool {
// logging is disabled
return false
}
if _, ok := s.Logs.LoggerNames[r.Host]; ok {

// strip off the port if any, logger names are host only
hostWithoutPort, _, err := net.SplitHostPort(r.Host)
if err != nil {
hostWithoutPort = r.Host
}

if _, ok := s.Logs.LoggerNames[hostWithoutPort]; ok {
// this host is mapped to a particular logger name
return true
}
for _, dh := range s.Logs.SkipHosts {
// logging for this particular host is disabled
if certmagic.MatchWildcard(r.Host, dh) {
if certmagic.MatchWildcard(hostWithoutPort, dh) {
return false
}
}
Expand Down

0 comments on commit a1a5ec6

Please sign in to comment.