@marcel91200 / caddy Mirror · read-only / commits / 0fe98038
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 14, 05:15 UTC

caddyhttp: Fix logging name associations by adding a default

Matthew Holt committed Apr 8, 2020 at 14:39 UTC 0fe98038b64f359f431b7590a9e9fc6266a5690a
1 file changed +15 -9
modules/caddyhttp/server.go
+15 -9
@@ -170,13 +170,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
170 repl.Set("http.response.latency", latency)
171
172 logger := accLog
173 - if s.Logs != nil && s.Logs.LoggerNames != nil {
174 - if loggerName, ok := s.Logs.LoggerNames[r.Host]; ok {
175 - logger = logger.Named(loggerName)
176 - } else {
177 - // see if there's a default log name to attach to
178 - logger = logger.Named(s.Logs.LoggerNames[""])
179 - }
173 + if loggerName := s.Logs.getLoggerName(r.Host); loggerName != "" {
174 + logger = logger.Named(loggerName)
175 }
176
177 log := logger.Info
@@ -205,8 +200,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
200 if err != nil {
201 // prepare the error log
202 logger := errLog
208 - if s.Logs != nil && s.Logs.LoggerNames != nil {
209 - logger = logger.Named(s.Logs.LoggerNames[r.Host])
203 + if loggerName := s.Logs.getLoggerName(r.Host); loggerName != "" {
204 + logger = logger.Named(loggerName)
205 }
206
207 // get the values that will be used to log the error
@@ -372,6 +367,10 @@ func (*HTTPErrorConfig) WithError(r *http.Request, err error) *http.Request {
367
368 // ServerLogConfig describes a server's logging configuration.
369 type ServerLogConfig struct {
370 + // The logger name for all logs emitted by this server unless
371 + // the hostname is found in the LoggerNames (logger_names) map.
372 + LoggerName string `json:"log_name,omitempty"`
373 +
374 // LoggerNames maps request hostnames to a custom logger name.
375 // For example, a mapping of "example.com" to "example" would
376 // cause access logs from requests with a Host of example.com
@@ -379,6 +378,13 @@ type ServerLogConfig struct {
378 LoggerNames map[string]string `json:"logger_names,omitempty"`
379 }
380
381 +func (slc ServerLogConfig) getLoggerName(host string) string {
382 + if loggerName, ok := slc.LoggerNames[host]; ok {
383 + return loggerName
384 + }
385 + return slc.LoggerName
386 +}
387 +
388 // errLogValues inspects err and returns the status code
389 // to use, the error log message, and any extra fields.
390 // If err is a HandlerError, the returned values will