diff options
author | Francis Lavoie <[email protected]> | 2024-04-16 18:26:18 -0400 |
---|---|---|
committer | GitHub <[email protected]> | 2024-04-16 22:26:18 +0000 |
commit | 70953e873a559553e97c985a13db897ab9f573ff (patch) | |
tree | cade4037d41f21a0fb16ca879f098d6d010ea010 /modules/caddyhttp/server.go | |
parent | eafc875ea9f6238fce24b23af47a1d7162fd27a5 (diff) | |
download | caddy-70953e873a559553e97c985a13db897ab9f573ff.tar.gz caddy-70953e873a559553e97c985a13db897ab9f573ff.zip |
caddyhttp: Support multiple logger names per host (#6088)
* caddyhttp: Support multiple logger names per host
* Lint
* Add adapt test
* Implement "string or array" parsing, keep original `logger_names`
* Rewrite adapter test to be more representative of the usecase
Diffstat (limited to 'modules/caddyhttp/server.go')
-rw-r--r-- | modules/caddyhttp/server.go | 49 |
1 files changed, 29 insertions, 20 deletions
diff --git a/modules/caddyhttp/server.go b/modules/caddyhttp/server.go index c7e5a5f61..0e88ef26d 100644 --- a/modules/caddyhttp/server.go +++ b/modules/caddyhttp/server.go @@ -361,11 +361,11 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { cloneURL(origReq.URL, r.URL) // prepare the error log - logger := errLog + errLog = errLog.With(zap.Duration("duration", duration)) + errLoggers := []*zap.Logger{errLog} if s.Logs != nil { - logger = s.Logs.wrapLogger(logger, r.Host) + errLoggers = s.Logs.wrapLogger(errLog, r.Host) } - logger = logger.With(zap.Duration("duration", duration)) // get the values that will be used to log the error errStatus, errMsg, errFields := errLogValues(err) @@ -379,7 +379,9 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { if err2 == nil { // user's error route handled the error response // successfully, so now just log the error - logger.Debug(errMsg, errFields...) + for _, logger := range errLoggers { + logger.Debug(errMsg, errFields...) + } } else { // well... this is awkward errFields = append([]zapcore.Field{ @@ -387,7 +389,9 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { zap.Namespace("first_error"), zap.String("msg", errMsg), }, errFields...) - logger.Error("error handling handler error", errFields...) + for _, logger := range errLoggers { + logger.Error("error handling handler error", errFields...) + } if handlerErr, ok := err.(HandlerError); ok { w.WriteHeader(handlerErr.StatusCode) } else { @@ -395,10 +399,12 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } } else { - if errStatus >= 500 { - logger.Error(errMsg, errFields...) - } else { - logger.Debug(errMsg, errFields...) + for _, logger := range errLoggers { + if errStatus >= 500 { + logger.Error(errMsg, errFields...) + } else { + logger.Debug(errMsg, errFields...) + } } w.WriteHeader(errStatus) } @@ -735,16 +741,6 @@ func (s *Server) logRequest( repl.Set("http.response.duration", duration) repl.Set("http.response.duration_ms", duration.Seconds()*1e3) // multiply seconds to preserve decimal (see #4666) - logger := accLog - if s.Logs != nil { - logger = s.Logs.wrapLogger(logger, r.Host) - } - - log := logger.Info - if wrec.Status() >= 400 { - log = logger.Error - } - userID, _ := repl.GetString("http.auth.user.id") reqBodyLength := 0 @@ -768,7 +764,20 @@ func (s *Server) logRequest( })) fields = append(fields, extra.fields...) - log("handled request", fields...) + loggers := []*zap.Logger{accLog} + if s.Logs != nil { + loggers = s.Logs.wrapLogger(accLog, r.Host) + } + + // wrapping may return multiple loggers, so we log to all of them + for _, logger := range loggers { + logAtLevel := logger.Info + if wrec.Status() >= 400 { + logAtLevel = logger.Error + } + + logAtLevel("handled request", fields...) + } } // protocol returns true if the protocol proto is configured/enabled. |