Skip to content

Commit

Permalink
Simplify logic, remove retry without port, reject config with port, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed Apr 21, 2024
1 parent a1a5ec6 commit ae03e14
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
10 changes: 9 additions & 1 deletion modules/caddyhttp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,10 @@ func (app *App) Provision(ctx caddy.Context) error {

// Validate ensures the app's configuration is valid.
func (app *App) Validate() error {
// each server must use distinct listener addresses
lnAddrs := make(map[string]string)

for srvName, srv := range app.Servers {
// each server must use distinct listener addresses
for _, addr := range srv.Listen {
listenAddr, err := caddy.ParseNetworkAddress(addr)
if err != nil {
Expand All @@ -347,6 +348,13 @@ func (app *App) Validate() error {
lnAddrs[addr] = srvName
}
}

// logger names must not have ports
for host := range srv.Logs.LoggerNames {
if _, _, err := net.SplitHostPort(host); err == nil {
return fmt.Errorf("server %s: logger name must not have a port: %s", srvName, host)
}
}
}
return nil
}
Expand Down
30 changes: 11 additions & 19 deletions modules/caddyhttp/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ type ServerLogConfig struct {
DefaultLoggerName string `json:"default_logger_name,omitempty"`

// LoggerNames maps request hostnames to one or more custom logger
// names. For example, a mapping of "example.com" to "example" would
// names. For example, a mapping of `"example.com": ["example"]` would
// cause access logs from requests with a Host of example.com to be
// emitted by a logger named "http.log.access.example". If there are
// multiple logger names, then the log will be emitted to all of them.
// If the logger name is an empty, the default logger is used, i.e.
// the logger "http.log.access".
//
// Keys must be hostnames (without ports), and may contain wildcards
// to match subdomains. The value is an array of logger names.
//
// For backwards compatibility, if the value is a string, it is treated
// as a single-element array.
LoggerNames map[string]StringArray `json:"logger_names,omitempty"`
Expand All @@ -64,7 +70,8 @@ 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 {
// strip off the port if any
// logger config should always be only
// the hostname, without the port
hostWithoutPort, _, err := net.SplitHostPort(host)
if err != nil {
hostWithoutPort = host
Expand All @@ -84,23 +91,8 @@ func (slc ServerLogConfig) wrapLogger(logger *zap.Logger, host string) []*zap.Lo
}

func (slc ServerLogConfig) getLoggerHosts(host string) []string {
tryHost := func(key string) ([]string, bool) {
// first try exact match
if hosts, ok := slc.LoggerNames[key]; ok {
return hosts, 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 []string{}, false
}
hosts, ok := slc.LoggerNames[hostOnly]
return hosts, ok
}

// try the exact hostname first
if hosts, ok := tryHost(host); ok {
if hosts, ok := slc.LoggerNames[host]; ok {
return hosts
}

Expand All @@ -112,7 +104,7 @@ func (slc ServerLogConfig) getLoggerHosts(host string) []string {
}
labels[i] = "*"
wildcardHost := strings.Join(labels, ".")
if hosts, ok := tryHost(wildcardHost); ok {
if hosts, ok := slc.LoggerNames[wildcardHost]; ok {
return hosts
}
}
Expand Down

0 comments on commit ae03e14

Please sign in to comment.