logger.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "log"
  7. "os"
  8. "time"
  9. )
  10. func ErrorLogger() HandlerFunc {
  11. return ErrorLoggerT(ErrorTypeAll)
  12. }
  13. func ErrorLoggerT(typ uint32) HandlerFunc {
  14. return func(c *Context) {
  15. c.Next()
  16. errs := c.Errors.ByType(typ)
  17. if len(errs) > 0 {
  18. // -1 status code = do not change current one
  19. c.JSON(-1, c.Errors)
  20. }
  21. }
  22. }
  23. var (
  24. green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
  25. white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
  26. yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109})
  27. red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
  28. blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
  29. magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
  30. cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
  31. reset = string([]byte{27, 91, 48, 109})
  32. )
  33. func Logger() HandlerFunc {
  34. stdlogger := log.New(os.Stdout, "", 0)
  35. //errlogger := log.New(os.Stderr, "", 0)
  36. return func(c *Context) {
  37. // Start timer
  38. start := time.Now()
  39. // Process request
  40. c.Next()
  41. // save the IP of the requester
  42. requester := c.Request.Header.Get("X-Real-IP")
  43. // if the requester-header is empty, check the forwarded-header
  44. if len(requester) == 0 {
  45. requester = c.Request.Header.Get("X-Forwarded-For")
  46. }
  47. // if the requester is still empty, use the hard-coded address from the socket
  48. if len(requester) == 0 {
  49. requester = c.Request.RemoteAddr
  50. }
  51. var color string
  52. code := c.Writer.Status()
  53. switch {
  54. case code >= 200 && code <= 299:
  55. color = green
  56. case code >= 300 && code <= 399:
  57. color = white
  58. case code >= 400 && code <= 499:
  59. color = yellow
  60. default:
  61. color = red
  62. }
  63. var methodColor string
  64. method := c.Request.Method
  65. switch {
  66. case method == "GET":
  67. methodColor = blue
  68. case method == "POST":
  69. methodColor = cyan
  70. case method == "PUT":
  71. methodColor = yellow
  72. case method == "DELETE":
  73. methodColor = red
  74. case method == "PATCH":
  75. methodColor = green
  76. case method == "HEAD":
  77. methodColor = magenta
  78. case method == "OPTIONS":
  79. methodColor = white
  80. }
  81. end := time.Now()
  82. latency := end.Sub(start)
  83. stdlogger.Printf("[GIN] %v |%s %3d %s| %12v | %s |%s %s %-7s %s\n%s",
  84. end.Format("2006/01/02 - 15:04:05"),
  85. color, code, reset,
  86. latency,
  87. requester,
  88. methodColor, reset, method,
  89. c.Request.URL.Path,
  90. c.Errors.String(),
  91. )
  92. }
  93. }