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

Method logRequest

modules/caddyhttp/server.go:843–912  ·  view source on GitHub ↗

logRequest logs the request to access logs, unless skipped.

(
	accLog *zap.Logger, r *http.Request, wrec ResponseRecorder, duration *time.Duration,
	repl *caddy.Replacer, bodyReader *lengthReader, shouldLogCredentials bool,
)

Source from the content-addressed store, hash-verified

841
842// logRequest logs the request to access logs, unless skipped.
843func (s *Server) logRequest(
844 accLog *zap.Logger, r *http.Request, wrec ResponseRecorder, duration *time.Duration,
845 repl *caddy.Replacer, bodyReader *lengthReader, shouldLogCredentials bool,
846) {
847 ctx := r.Context()
848
849 // this request may be flagged as omitted from the logs
850 if skip, ok := GetVar(ctx, LogSkipVar).(bool); ok && skip {
851 return
852 }
853
854 status := wrec.Status()
855 size := wrec.Size()
856
857 repl.Set("http.response.status", status) // will be 0 if no response is written by us (Go will write 200 to client)
858 repl.Set("http.response.size", size)
859 repl.Set("http.response.duration", duration)
860 repl.Set("http.response.duration_ms", duration.Seconds()*1e3) // multiply seconds to preserve decimal (see #4666)
861
862 loggers := []*zap.Logger{accLog}
863 if s.Logs != nil {
864 loggers = s.Logs.wrapLogger(accLog, r)
865 }
866
867 message := "handled request"
868 if nop, ok := GetVar(ctx, "unhandled").(bool); ok && nop {
869 message = "NOP"
870 }
871
872 logLevel := zapcore.InfoLevel
873 if status >= 500 {
874 logLevel = zapcore.ErrorLevel
875 }
876
877 var fields []zapcore.Field
878 for _, logger := range loggers {
879 c := logger.Check(logLevel, message)
880 if c == nil {
881 continue
882 }
883
884 if fields == nil {
885 userID, _ := repl.GetString("http.auth.user.id")
886
887 reqBodyLength := 0
888 if bodyReader != nil {
889 reqBodyLength = bodyReader.Length
890 }
891
892 extra := ctx.Value(ExtraLogFieldsCtxKey).(*ExtraLogFields)
893
894 fieldCount := 6
895 fields = make([]zapcore.Field, 0, fieldCount+len(extra.fields))
896 fields = append(fields,
897 zap.Int("bytes_read", reqBodyLength),
898 zap.String("user_id", userID),
899 zap.Duration("duration", *duration),
900 zap.Int("size", size),

Calls 13

GetVarFunction · 0.85
wrapLoggerMethod · 0.80
GetStringMethod · 0.80
DurationMethod · 0.80
StatusMethod · 0.65
SizeMethod · 0.65
SetMethod · 0.45
CheckMethod · 0.45
ValueMethod · 0.45
IntMethod · 0.45
StringMethod · 0.45
HeaderMethod · 0.45