MCPcopy
hub / github.com/caddyserver/caddy / wrapLogger

Method wrapLogger

modules/caddyhttp/logging.go:93–133  ·  view source on GitHub ↗

wrapLogger wraps logger in one or more logger named according to user preferences for the given host.

(logger *zap.Logger, req *http.Request)

Source from the content-addressed store, hash-verified

91// wrapLogger wraps logger in one or more logger named
92// according to user preferences for the given host.
93func (slc ServerLogConfig) wrapLogger(logger *zap.Logger, req *http.Request) []*zap.Logger {
94 // using the `log_name` directive or the `access_logger_names` variable,
95 // the logger names can be overridden for the current request
96 if names := GetVar(req.Context(), AccessLoggerNameVarKey); names != nil {
97 if namesSlice, ok := names.([]any); ok {
98 loggers := make([]*zap.Logger, 0, len(namesSlice))
99 for _, loggerName := range namesSlice {
100 // no name, use the default logger
101 if loggerName == "" {
102 loggers = append(loggers, logger)
103 continue
104 }
105 // make a logger with the given name
106 loggers = append(loggers, logger.Named(loggerName.(string)))
107 }
108 return loggers
109 }
110 }
111
112 // get the hostname from the request, with the port number stripped
113 host, _, err := net.SplitHostPort(req.Host)
114 if err != nil {
115 host = req.Host
116 }
117
118 // get the logger names for this host from the config
119 hosts := slc.getLoggerHosts(host)
120
121 // make a list of named loggers, or the default logger
122 loggers := make([]*zap.Logger, 0, len(hosts))
123 for _, loggerName := range hosts {
124 // no name, use the default logger
125 if loggerName == "" {
126 loggers = append(loggers, logger)
127 continue
128 }
129 // make a logger with the given name
130 loggers = append(loggers, logger.Named(loggerName))
131 }
132 return loggers
133}
134
135func (slc ServerLogConfig) getLoggerHosts(host string) []string {
136 // try the exact hostname first

Callers 2

ServeHTTPMethod · 0.80
logRequestMethod · 0.80

Calls 2

getLoggerHostsMethod · 0.95
GetVarFunction · 0.85

Tested by

no test coverage detected