logger.go 2.4 KB

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