Skip to content

Instantly share code, notes, and snippets.

@henvic
Created November 22, 2021 07:33
Show Gist options
  • Save henvic/9b786ad598ab39c35760f68179538948 to your computer and use it in GitHub Desktop.
Save henvic/9b786ad598ab39c35760f68179538948 to your computer and use it in GitHub Desktop.
httpretty logger middleware
// 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