Skip to content

Commit 99ffe93

Browse files
logging: Fix skip_hosts with wildcards (caddyserver#5102)
Fix caddyserver#4859
1 parent e07a267 commit 99ffe93

File tree

5 files changed

+23
-38
lines changed

5 files changed

+23
-38
lines changed

caddyconfig/httpcaddyfile/builtins.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ func parseLogHelper(h Helper, globalLogNames map[string]struct{}) ([]ConfigValue
735735
// reference the default logger. See the
736736
// setupNewDefault function in the logging
737737
// package for where this is configured.
738-
globalLogName = "default"
738+
globalLogName = caddy.DefaultLoggerName
739739
}
740740

741741
// Verify this name is unused.

caddyconfig/httpcaddyfile/httptype.go

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
219219
if ncl.name == "" {
220220
return
221221
}
222-
if ncl.name == "default" {
222+
if ncl.name == caddy.DefaultLoggerName {
223223
hasDefaultLog = true
224224
}
225225
if _, ok := options["debug"]; ok && ncl.log.Level == "" {
@@ -240,7 +240,7 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
240240
// configure it with any applicable options
241241
if _, ok := options["debug"]; ok {
242242
customLogs = append(customLogs, namedCustomLog{
243-
name: "default",
243+
name: caddy.DefaultLoggerName,
244244
log: &caddy.CustomLog{Level: zap.DebugLevel.CapitalString()},
245245
})
246246
}
@@ -299,11 +299,11 @@ func (st ServerType) Setup(inputServerBlocks []caddyfile.ServerBlock,
299299
// most users seem to prefer not writing access logs
300300
// to the default log when they are directed to a
301301
// file or have any other special customization
302-
if ncl.name != "default" && len(ncl.log.Include) > 0 {
303-
defaultLog, ok := cfg.Logging.Logs["default"]
302+
if ncl.name != caddy.DefaultLoggerName && len(ncl.log.Include) > 0 {
303+
defaultLog, ok := cfg.Logging.Logs[caddy.DefaultLoggerName]
304304
if !ok {
305305
defaultLog = new(caddy.CustomLog)
306-
cfg.Logging.Logs["default"] = defaultLog
306+
cfg.Logging.Logs[caddy.DefaultLoggerName] = defaultLog
307307
}
308308
defaultLog.Exclude = append(defaultLog.Exclude, ncl.log.Include...)
309309
}
@@ -518,15 +518,6 @@ func (st *ServerType) serversFromPairings(
518518
var hasCatchAllTLSConnPolicy, addressQualifiesForTLS bool
519519
autoHTTPSWillAddConnPolicy := autoHTTPS != "off"
520520

521-
// if a catch-all server block (one which accepts all hostnames) exists in this pairing,
522-
// we need to know that so that we can configure logs properly (see #3878)
523-
var catchAllSblockExists bool
524-
for _, sblock := range p.serverBlocks {
525-
if len(sblock.hostsFromKeys(false)) == 0 {
526-
catchAllSblockExists = true
527-
}
528-
}
529-
530521
// if needed, the ServerLogConfig is initialized beforehand so
531522
// that all server blocks can populate it with data, even when not
532523
// coming with a log directive
@@ -658,18 +649,10 @@ func (st *ServerType) serversFromPairings(
658649
} else {
659650
// map each host to the user's desired logger name
660651
for _, h := range sblockLogHosts {
661-
// if the custom logger name is non-empty, add it to the map;
662-
// otherwise, only map to an empty logger name if this or
663-
// another site block on this server has a catch-all host (in
664-
// which case only requests with mapped hostnames will be
665-
// access-logged, so it'll be necessary to add them to the
666-
// map even if they use default logger)
667-
if ncl.name != "" || catchAllSblockExists {
668-
if srv.Logs.LoggerNames == nil {
669-
srv.Logs.LoggerNames = make(map[string]string)
670-
}
671-
srv.Logs.LoggerNames[h] = ncl.name
652+
if srv.Logs.LoggerNames == nil {
653+
srv.Logs.LoggerNames = make(map[string]string)
672654
}
655+
srv.Logs.LoggerNames[h] = ncl.name
673656
}
674657
}
675658
}

caddytest/integration/caddyfile_adapt/log_skip_hosts.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ example.com {
6262
}
6363
],
6464
"logs": {
65+
"logger_names": {
66+
"one.example.com": ""
67+
},
6568
"skip_hosts": [
6669
"three.example.com",
6770
"two.example.com",

logging.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (logging *Logging) openLogs(ctx Context) error {
105105
// then set up any other custom logs
106106
for name, l := range logging.Logs {
107107
// the default log is already set up
108-
if name == "default" {
108+
if name == DefaultLoggerName {
109109
continue
110110
}
111111

@@ -138,7 +138,7 @@ func (logging *Logging) setupNewDefault(ctx Context) error {
138138

139139
// extract the user-defined default log, if any
140140
newDefault := new(defaultCustomLog)
141-
if userDefault, ok := logging.Logs["default"]; ok {
141+
if userDefault, ok := logging.Logs[DefaultLoggerName]; ok {
142142
newDefault.CustomLog = userDefault
143143
} else {
144144
// if none, make one with our own default settings
@@ -147,7 +147,7 @@ func (logging *Logging) setupNewDefault(ctx Context) error {
147147
if err != nil {
148148
return fmt.Errorf("setting up default Caddy log: %v", err)
149149
}
150-
logging.Logs["default"] = newDefault.CustomLog
150+
logging.Logs[DefaultLoggerName] = newDefault.CustomLog
151151
}
152152

153153
// set up this new log
@@ -702,6 +702,8 @@ var (
702702

703703
var writers = NewUsagePool()
704704

705+
const DefaultLoggerName = "default"
706+
705707
// Interface guards
706708
var (
707709
_ io.WriteCloser = (*notClosable)(nil)

modules/caddyhttp/server.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -639,21 +639,18 @@ func (s *Server) shouldLogRequest(r *http.Request) bool {
639639
// logging is disabled
640640
return false
641641
}
642+
if _, ok := s.Logs.LoggerNames[r.Host]; ok {
643+
// this host is mapped to a particular logger name
644+
return true
645+
}
642646
for _, dh := range s.Logs.SkipHosts {
643647
// logging for this particular host is disabled
644648
if certmagic.MatchWildcard(r.Host, dh) {
645649
return false
646650
}
647651
}
648-
if _, ok := s.Logs.LoggerNames[r.Host]; ok {
649-
// this host is mapped to a particular logger name
650-
return true
651-
}
652-
if s.Logs.SkipUnmappedHosts {
653-
// this host is not mapped and thus must not be logged
654-
return false
655-
}
656-
return true
652+
// if configured, this host is not mapped and thus must not be logged
653+
return !s.Logs.SkipUnmappedHosts
657654
}
658655

659656
// protocol returns true if the protocol proto is configured/enabled.

0 commit comments

Comments
 (0)