Skip to content

Commit

Permalink
clean up logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Raajheer1 committed May 24, 2024
1 parent e380260 commit 1531b3a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 35 deletions.
39 changes: 5 additions & 34 deletions pkg/go-chi/middleware/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,11 @@ type RequestLoggerEntry struct {
}

func (l *RequestLoggerEntry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{}) {
responseLog := map[string]interface{}{
"status": status,
"bytes": bytes,
"elapsed": elapsed.String(), // in milliseconds
"content-type": header.Get("Content-Type"),
"body": string(extra.([]byte)),
}

l.Logger.WithLevel(statusLevel(status)).Fields(map[string]interface{}{
"httpResponse": responseLog,
"status": status,
"elapsed": fmt.Sprintf("%dms", elapsed.Milliseconds()),
}).Msgf("")
}

func (l *RequestLoggerEntry) Panic(v interface{}, stack []byte) {
stacktrace := "#"

Expand All @@ -114,31 +106,10 @@ func (l *RequestLoggerEntry) Panic(v interface{}, stack []byte) {
}

func requestLogFields(r *http.Request) map[string]interface{} {
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
requestURL := fmt.Sprintf("%s://%s%s", scheme, r.Host, r.RequestURI)

requestFields := map[string]interface{}{
"requestURL": requestURL,
"requestMethod": r.Method,
"requestPath": r.URL.Path,
"remoteIP": r.RemoteAddr,
"proto": r.Proto,
}
if reqID := middleware.GetReqID(r.Context()); reqID != "" {
requestFields["requestID"] = reqID
}

requestFields["scheme"] = scheme

if len(r.Header) > 0 {
requestFields["header"] = headerLogField(r.Header)
}

return map[string]interface{}{
"httpRequest": requestFields,
"remoteIP": r.RemoteAddr,
"requestMethod": r.Method,
"requestURL": r.URL.Path,
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/go-chi/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func New(cfg *config.Config) *chi.Mux {
r := chi.NewRouter()

r.Use(logger.Logger(logging.ZL.With().Str("component", "access").Logger()))
r.Use(logger.Logger(logging.ZL.With().Str("component", "Chi").Logger()))
r.Use(middleware.Recoverer)
r.Use(middleware.RealIP)

Expand Down

0 comments on commit 1531b3a

Please sign in to comment.