logger.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "fmt"
  7. "io"
  8. "time"
  9. )
  10. var (
  11. green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
  12. white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
  13. yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109})
  14. red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
  15. blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
  16. magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
  17. cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
  18. reset = string([]byte{27, 91, 48, 109})
  19. )
  20. func ErrorLogger() HandlerFunc {
  21. return ErrorLoggerT(ErrorTypeAny)
  22. }
  23. func ErrorLoggerT(typ ErrorType) HandlerFunc {
  24. return func(c *Context) {
  25. c.Next()
  26. errors := c.Errors.ByType(typ)
  27. if len(errors) > 0 {
  28. c.JSON(-1, errors)
  29. }
  30. }
  31. }
  32. // Logger instances a Logger middleware that will write the logs to gin.DefaultWriter
  33. // By default gin.DefaultWriter = os.Stdout
  34. func Logger() HandlerFunc {
  35. return LoggerWithWriter(DefaultWriter)
  36. }
  37. // LoggerWithWriter instance a Logger middleware with the specified writter buffer.
  38. // Example: os.Stdout, a file opened in write mode, a socket...
  39. func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
  40. var skip map[string]struct{}
  41. if length := len(notlogged); length > 0 {
  42. skip = make(map[string]struct{}, length)
  43. for _, path := range notlogged {
  44. skip[path] = struct{}{}
  45. }
  46. }
  47. return func(c *Context) {
  48. // Start timer
  49. start := time.Now()
  50. path := c.Request.URL.Path
  51. // Process request
  52. c.Next()
  53. // Log only when path is not being skipped
  54. if _, ok := skip[path]; !ok {
  55. // Stop timer
  56. end := time.Now()
  57. latency := end.Sub(start)
  58. clientIP := c.ClientIP()
  59. method := c.Request.Method
  60. statusCode := c.Writer.Status()
  61. statusColor := colorForStatus(statusCode)
  62. methodColor := colorForMethod(method)
  63. comment := c.Errors.ByType(ErrorTypePrivate).String()
  64. fmt.Fprintf(out, "[GIN] %v |%s %3d %s| %13v | %s |%s %s %-7s %s\n%s",
  65. end.Format("2006/01/02 - 15:04:05"),
  66. statusColor, statusCode, reset,
  67. latency,
  68. clientIP,
  69. methodColor, reset, method,
  70. path,
  71. comment,
  72. )
  73. }
  74. }
  75. }
  76. func colorForStatus(code int) string {
  77. switch {
  78. case code >= 200 && code < 300:
  79. return green
  80. case code >= 300 && code < 400:
  81. return white
  82. case code >= 400 && code < 500:
  83. return yellow
  84. default:
  85. return red
  86. }
  87. }
  88. func colorForMethod(method string) string {
  89. switch method {
  90. case "GET":
  91. return blue
  92. case "POST":
  93. return cyan
  94. case "PUT":
  95. return yellow
  96. case "DELETE":
  97. return red
  98. case "PATCH":
  99. return green
  100. case "HEAD":
  101. return magenta
  102. case "OPTIONS":
  103. return white
  104. default:
  105. return reset
  106. }
  107. }