Created
November 22, 2021 07:33
-
-
Save henvic/9b786ad598ab39c35760f68179538948 to your computer and use it in GitHub Desktop.
httpretty logger middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// HTTPLogger for https://github.com/henvic/httpretty | |
// to provide useful debugging info when running: | |
// $ DEBUG=api go run github.com/henvic/pgxtutorial/cmd/pgxtutorial | |
func HTTPLogger() func(http.Handler) http.Handler { | |
if !strings.Contains(os.Getenv("DEBUG"), "api") { | |
return nil | |
} | |
_, noColor := os.LookupEnv("NO_COLOR") // See https://no-color.org/ | |
if !noColor { | |
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 { | |
noColor = true | |
} | |
} | |
logger := &httpretty.Logger{ | |
Time: true, | |
TLS: true, | |
RequestHeader: true, | |
RequestBody: true, | |
ResponseHeader: true, | |
ResponseBody: true, | |
Colors: !noColor, | |
Formatters: []httpretty.Formatter{&httpretty.JSONFormatter{}}, | |
} | |
logger.SetFlusher(httpretty.OnEnd) | |
return logger.Middleware | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment