log.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package middleware
  15. import (
  16. "time"
  17. gin "github.com/gin-gonic/gin"
  18. "yunion.io/x/log"
  19. )
  20. var (
  21. green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
  22. white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
  23. yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109})
  24. red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
  25. blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
  26. magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
  27. cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
  28. reset = string([]byte{27, 91, 48, 109})
  29. )
  30. // ErrorLogger returns an ErrorLoggerT with parameter gin.ErrorTypeAny
  31. func ErrorLogger() gin.HandlerFunc {
  32. return ErrorLoggerT(gin.ErrorTypeAny)
  33. }
  34. // ErrorLoggerT returns an ErrorLoggerT middleware with the given
  35. // type gin.ErrorType.
  36. func ErrorLoggerT(typ gin.ErrorType) gin.HandlerFunc {
  37. return func(c *gin.Context) {
  38. c.Next()
  39. if !c.Writer.Written() {
  40. json := c.Errors.ByType(typ).JSON()
  41. if json != nil {
  42. c.JSON(-1, json)
  43. }
  44. }
  45. }
  46. }
  47. // Logger prints a logline for each request and measures the time to
  48. // process for a call. It formats the log entries similar to
  49. // http://godoc.org/github.com/gin-gonic/gin#Logger does.
  50. //
  51. // Example:
  52. //
  53. // router := gin.New()
  54. // router.Use(ginglog.Logger())
  55. func Logger() gin.HandlerFunc {
  56. return func(c *gin.Context) {
  57. t := time.Now()
  58. // process request
  59. c.Next()
  60. latency := time.Since(t)
  61. clientIP := c.ClientIP()
  62. method := c.Request.Method
  63. statusCode := c.Writer.Status()
  64. // statusColor := colorForStatus(statusCode)
  65. // methodColor := colorForMethod(method)
  66. path := c.Request.URL.Path
  67. switch {
  68. case statusCode >= 400 && statusCode <= 499:
  69. {
  70. log.Warningf("[GIN] | %3d | %12v | %s | %-7s %s\n%s",
  71. // statusColor,
  72. statusCode,
  73. // reset,
  74. latency,
  75. clientIP,
  76. // methodColor,
  77. // reset,
  78. method,
  79. path,
  80. c.Errors.String(),
  81. )
  82. }
  83. case statusCode >= 500:
  84. {
  85. log.Errorf("[GIN] | %3d | %12v | %s | %-7s %s\n%s",
  86. // statusColor,
  87. statusCode,
  88. // reset,
  89. latency,
  90. clientIP,
  91. // methodColor,
  92. // reset,
  93. method,
  94. path,
  95. c.Errors.String(),
  96. )
  97. }
  98. default:
  99. log.V(2).Infof("[GIN] | %3d | %12v | %s | %-7s %s\n%s",
  100. // statusColor,
  101. statusCode,
  102. // reset,
  103. latency,
  104. clientIP,
  105. // methodColor,
  106. // reset,
  107. method,
  108. path,
  109. c.Errors.String(),
  110. )
  111. }
  112. }
  113. }
  114. func colorForStatus(code int) string {
  115. switch {
  116. case code >= 200 && code <= 299:
  117. return green
  118. case code >= 300 && code <= 399:
  119. return white
  120. case code >= 400 && code <= 499:
  121. return yellow
  122. default:
  123. return red
  124. }
  125. }
  126. func colorForMethod(method string) string {
  127. switch {
  128. case method == "GET":
  129. return blue
  130. case method == "POST":
  131. return cyan
  132. case method == "PUT":
  133. return yellow
  134. case method == "DELETE":
  135. return red
  136. case method == "PATCH":
  137. return green
  138. case method == "HEAD":
  139. return magenta
  140. case method == "OPTIONS":
  141. return white
  142. default:
  143. return reset
  144. }
  145. }