log.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package api
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/fatih/color"
  6. "github.com/go-resty/resty/v2"
  7. )
  8. func logRequest(req *resty.Request) {
  9. fmt.Println(color.GreenString("Request:"))
  10. fmt.Printf("%s %s\n", color.CyanString(req.Method), color.YellowString(req.URL))
  11. fmt.Println(color.GreenString("Headers:"))
  12. for k, v := range req.Header {
  13. redacted := false
  14. for _, rh := range RedactedHeaders {
  15. if strings.EqualFold(strings.ToLower(k), strings.ToLower(rh)) {
  16. redacted = true
  17. break
  18. }
  19. }
  20. if redacted {
  21. fmt.Printf("%s: %s\n", color.CyanString(k), color.RedString("REDACTED"))
  22. } else {
  23. if len(v) == 1 {
  24. fmt.Printf("%s: %s\n", color.CyanString(k), color.YellowString(v[0]))
  25. } else {
  26. fmt.Printf("%s: %s\n", color.CyanString(k), color.YellowString(strings.Join(v, ",")))
  27. }
  28. }
  29. }
  30. }
  31. func logResponse(resp *resty.Response) {
  32. fmt.Println(color.GreenString("Response:"))
  33. if resp.StatusCode() < 200 || resp.StatusCode() >= 300 {
  34. fmt.Printf("%s %s\n", color.CyanString(resp.Proto()), color.RedString(resp.Status()))
  35. } else {
  36. fmt.Printf("%s %s\n", color.CyanString(resp.Proto()), color.YellowString(resp.Status()))
  37. }
  38. fmt.Printf("Time Duration: %s\n", resp.Time())
  39. fmt.Println(color.GreenString("Headers:"))
  40. for k, v := range resp.Header() {
  41. redacted := false
  42. for _, rh := range RedactedHeaders {
  43. if strings.EqualFold(strings.ToLower(k), strings.ToLower(rh)) {
  44. redacted = true
  45. break
  46. }
  47. }
  48. if redacted {
  49. fmt.Printf("%s: %s\n", color.CyanString(k), color.RedString("REDACTED"))
  50. } else {
  51. fmt.Printf("%s: %s\n", color.CyanString(k), color.YellowString(strings.Join(v, ",")))
  52. }
  53. }
  54. }