@marcel91200 / caddy Mirror · read-only / commits / 726a9a8f
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 13:15 UTC

logging: Fix default access logger (#6251)

* logging: Fix default access logger * Simplify logic, remove retry without port, reject config with port, docs * Nil check

Francis Lavoie committed Apr 22, 2024 at 08:33 UTC 726a9a8fde60e4d72282b24fb553573fb388607c
3 files changed +39 -22
modules/caddyhttp/app.go
+11 -1
@@ -329,9 +329,10 @@ func (app *App) Provision(ctx caddy.Context) error {
329
330 // Validate ensures the app's configuration is valid.
331 func (app *App) Validate() error {
332 - // each server must use distinct listener addresses
332 lnAddrs := make(map[string]string)
333 +
334 for srvName, srv := range app.Servers {
335 + // each server must use distinct listener addresses
336 for _, addr := range srv.Listen {
337 listenAddr, err := caddy.ParseNetworkAddress(addr)
338 if err != nil {
@@ -347,6 +348,15 @@ func (app *App) Validate() error {
348 lnAddrs[addr] = srvName
349 }
350 }
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 + }
360 }
361 return nil
362 }
modules/caddyhttp/logging.go
+19 -19
@@ -37,10 +37,16 @@ type ServerLogConfig struct {
37 DefaultLoggerName string `json:"default_logger_name,omitempty"`
38
39 // 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
41 // cause access logs from requests with a Host of example.com to be
42 // emitted by a logger named "http.log.access.example". If there are
43 // 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 + //
50 // For backwards compatibility, if the value is a string, it is treated
51 // as a single-element array.
52 LoggerNames map[string]StringArray `json:"logger_names,omitempty"`
@@ -64,10 +70,19 @@ type ServerLogConfig struct {
70 // wrapLogger wraps logger in one or more logger named
71 // according to user preferences for the given host.
72 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)
81 loggers := make([]*zap.Logger, 0, len(hosts))
82 for _, loggerName := range hosts {
83 + // no name, use the default logger
84 if loggerName == "" {
85 + loggers = append(loggers, logger)
86 continue
87 }
88 loggers = append(loggers, logger.Named(loggerName))
@@ -76,23 +91,8 @@ func (slc ServerLogConfig) wrapLogger(logger *zap.Logger, host string) []*zap.Lo
91 }
92
93 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 -
94 // try the exact hostname first
95 - if hosts, ok := tryHost(host); ok {
95 + if hosts, ok := slc.LoggerNames[host]; ok {
96 return hosts
97 }
98
@@ -104,7 +104,7 @@ func (slc ServerLogConfig) getLoggerHosts(host string) []string {
104 }
105 labels[i] = "*"
106 wildcardHost := strings.Join(labels, ".")
107 - if hosts, ok := tryHost(wildcardHost); ok {
107 + if hosts, ok := slc.LoggerNames[wildcardHost]; ok {
108 return hosts
109 }
110 }
modules/caddyhttp/server.go
+9 -2
@@ -717,13 +717,20 @@ func (s *Server) shouldLogRequest(r *http.Request) bool {
717 // logging is disabled
718 return false
719 }
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 {
728 // this host is mapped to a particular logger name
729 return true
730 }
731 for _, dh := range s.Logs.SkipHosts {
732 // logging for this particular host is disabled
726 - if certmagic.MatchWildcard(r.Host, dh) {
733 + if certmagic.MatchWildcard(hostWithoutPort, dh) {
734 return false
735 }
736 }