Skip to content

Commit

Permalink
add method to set log level for logging with request
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-stuart committed Feb 9, 2022
1 parent 5fd699a commit 3422262
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ func (l Log) logWithRequest(r *http.Request) logging.Interface {
return user.LogWith(r.Context(), localLog)
}

func (l Log) logWithRequestAtLevelf(r *http.Request, level string, format string, args ...interface{}) {
switch level {
case "debug":
l.logWithRequest(r).Debugf(format, args...)
case "info":
l.logWithRequest(r).Infof(format, args...)
}
}

// Wrap implements Middleware
func (l Log) Wrap(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -53,33 +62,26 @@ func (l Log) Wrap(next http.Handler) http.Handler {
wrapped := newBadResponseLoggingWriter(w, &buf)
next.ServeHTTP(wrapped, r)

level := "debug"
if l.LogRequestsAtInfoLevel {
level = "info"
}

statusCode, writeErr := wrapped.statusCode, wrapped.writeError

if writeErr != nil {
if errors.Is(writeErr, context.Canceled) {
if l.LogRequestsAtInfoLevel {
l.logWithRequest(r).Infof("%s %s %s, request cancelled: %s ws: %v; %s", r.Method, uri, time.Since(begin), writeErr, IsWSHandshakeRequest(r), headers)
} else {
l.logWithRequest(r).Debugf("%s %s %s, request cancelled: %s ws: %v; %s", r.Method, uri, time.Since(begin), writeErr, IsWSHandshakeRequest(r), headers)
}
l.logWithRequestAtLevelf(r, level, "%s %s %s, request cancelled: %s ws: %v; %s", r.Method, uri, time.Since(begin), writeErr, IsWSHandshakeRequest(r), headers)
} else {
l.logWithRequest(r).Warnf("%s %s %s, error: %s ws: %v; %s", r.Method, uri, time.Since(begin), writeErr, IsWSHandshakeRequest(r), headers)
}

return
}
if 100 <= statusCode && statusCode < 500 || statusCode == http.StatusBadGateway || statusCode == http.StatusServiceUnavailable {
if l.LogRequestsAtInfoLevel {
l.logWithRequest(r).Infof("%s %s (%d) %s", r.Method, uri, statusCode, time.Since(begin))
} else {
l.logWithRequest(r).Debugf("%s %s (%d) %s", r.Method, uri, statusCode, time.Since(begin))
}
l.logWithRequestAtLevelf(r, level, "%s %s (%d) %s", r.Method, uri, statusCode, time.Since(begin))
if l.LogRequestHeaders && headers != nil {
if l.LogRequestsAtInfoLevel {
l.logWithRequest(r).Infof("ws: %v; %s", IsWSHandshakeRequest(r), string(headers))
} else {
l.logWithRequest(r).Debugf("ws: %v; %s", IsWSHandshakeRequest(r), string(headers))
}
l.logWithRequestAtLevelf(r, level, "ws: %v; %s", IsWSHandshakeRequest(r), string(headers))
}
} else {
l.logWithRequest(r).Warnf("%s %s (%d) %s Response: %q ws: %v; %s",
Expand Down

0 comments on commit 3422262

Please sign in to comment.