另客网go项目公用的代码库

logger.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. "net/http"
  9. "os"
  10. "time"
  11. "github.com/mattn/go-isatty"
  12. )
  13. var (
  14. green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
  15. white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
  16. yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109})
  17. red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
  18. blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
  19. magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
  20. cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
  21. reset = string([]byte{27, 91, 48, 109})
  22. disableColor = false
  23. )
  24. // DisableConsoleColor disables color output in the console.
  25. func DisableConsoleColor() {
  26. disableColor = true
  27. }
  28. // ErrorLogger returns a handlerfunc for any error type.
  29. func ErrorLogger() HandlerFunc {
  30. return ErrorLoggerT(ErrorTypeAny)
  31. }
  32. // ErrorLoggerT returns a handlerfunc for a given error type.
  33. func ErrorLoggerT(typ ErrorType) HandlerFunc {
  34. return func(c *Context) {
  35. c.Next()
  36. errors := c.Errors.ByType(typ)
  37. if len(errors) > 0 {
  38. c.JSON(-1, errors)
  39. }
  40. }
  41. }
  42. // Logger instances a Logger middleware that will write the logs to gin.DefaultWriter.
  43. // By default gin.DefaultWriter = os.Stdout.
  44. func Logger() HandlerFunc {
  45. return LoggerWithWriter(DefaultWriter)
  46. }
  47. // LoggerWithWriter instance a Logger middleware with the specified writter buffer.
  48. // Example: os.Stdout, a file opened in write mode, a socket...
  49. func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
  50. isTerm := true
  51. if w, ok := out.(*os.File); !ok ||
  52. (os.Getenv("TERM") == "dumb" || (!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd()))) ||
  53. disableColor {
  54. isTerm = false
  55. }
  56. var skip map[string]struct{}
  57. if length := len(notlogged); length > 0 {
  58. skip = make(map[string]struct{}, length)
  59. for _, path := range notlogged {
  60. skip[path] = struct{}{}
  61. }
  62. }
  63. return func(c *Context) {
  64. // Start timer
  65. start := time.Now()
  66. path := c.Request.URL.Path
  67. raw := c.Request.URL.RawQuery
  68. // Process request
  69. c.Next()
  70. // Log only when path is not being skipped
  71. if _, ok := skip[path]; !ok {
  72. // Stop timer
  73. end := time.Now()
  74. latency := end.Sub(start)
  75. clientIP := c.ClientIP()
  76. method := c.Request.Method
  77. statusCode := c.Writer.Status()
  78. var statusColor, methodColor, resetColor string
  79. if isTerm {
  80. statusColor = colorForStatus(statusCode)
  81. methodColor = colorForMethod(method)
  82. resetColor = reset
  83. }
  84. comment := c.Errors.ByType(ErrorTypePrivate).String()
  85. if raw != "" {
  86. path = path + "?" + raw
  87. }
  88. fmt.Fprintf(out, "[GIN] %v |%s %3d %s| %13v | %15s |%s %-7s %s %s\n%s",
  89. end.Format("2006/01/02 - 15:04:05"),
  90. statusColor, statusCode, resetColor,
  91. latency,
  92. clientIP,
  93. methodColor, method, resetColor,
  94. path,
  95. comment,
  96. )
  97. }
  98. }
  99. }
  100. func colorForStatus(code int) string {
  101. switch {
  102. case code >= http.StatusOK && code < http.StatusMultipleChoices:
  103. return green
  104. case code >= http.StatusMultipleChoices && code < http.StatusBadRequest:
  105. return white
  106. case code >= http.StatusBadRequest && code < http.StatusInternalServerError:
  107. return yellow
  108. default:
  109. return red
  110. }
  111. }
  112. func colorForMethod(method string) string {
  113. switch method {
  114. case "GET":
  115. return blue
  116. case "POST":
  117. return cyan
  118. case "PUT":
  119. return yellow
  120. case "DELETE":
  121. return red
  122. case "PATCH":
  123. return green
  124. case "HEAD":
  125. return magenta
  126. case "OPTIONS":
  127. return white
  128. default:
  129. return reset
  130. }
  131. }