caddyhttp: Fix logging name associations by adding a default

This commit is contained in:
Matthew Holt 2020-04-08 14:39:20 -06:00
parent 6e4c688ea7
commit 0fe98038b6
No known key found for this signature in database
GPG Key ID: 2A349DD577D586A5
1 changed files with 15 additions and 9 deletions

View File

@ -170,13 +170,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
repl.Set("http.response.latency", latency)
logger := accLog
if s.Logs != nil && s.Logs.LoggerNames != nil {
if loggerName, ok := s.Logs.LoggerNames[r.Host]; ok {
if loggerName := s.Logs.getLoggerName(r.Host); loggerName != "" {
logger = logger.Named(loggerName)
} else {
// see if there's a default log name to attach to
logger = logger.Named(s.Logs.LoggerNames[""])
}
}
log := logger.Info
@ -205,8 +200,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err != nil {
// prepare the error log
logger := errLog
if s.Logs != nil && s.Logs.LoggerNames != nil {
logger = logger.Named(s.Logs.LoggerNames[r.Host])
if loggerName := s.Logs.getLoggerName(r.Host); loggerName != "" {
logger = logger.Named(loggerName)
}
// 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 {
// ServerLogConfig describes a server's logging configuration.
type ServerLogConfig struct {
// The logger name for all logs emitted by this server unless
// the hostname is found in the LoggerNames (logger_names) map.
LoggerName string `json:"log_name,omitempty"`
// LoggerNames maps request hostnames to a custom logger name.
// For example, a mapping of "example.com" to "example" would
// cause access logs from requests with a Host of example.com
@ -379,6 +378,13 @@ type ServerLogConfig struct {
LoggerNames map[string]string `json:"logger_names,omitempty"`
}
func (slc ServerLogConfig) getLoggerName(host string) string {
if loggerName, ok := slc.LoggerNames[host]; ok {
return loggerName
}
return slc.LoggerName
}
// errLogValues inspects err and returns the status code
// to use, the error log message, and any extra fields.
// If err is a HandlerError, the returned values will