logger.go 2.8 KB

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