logger.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 engines
  5. //type consoleColorModeValue int
  6. //
  7. //const (
  8. // autoColor consoleColorModeValue = iota
  9. // disableColor
  10. // forceColor
  11. //)
  12. //
  13. const (
  14. //green = "\033[97;42m"
  15. //white = "\033[90;47m"
  16. //yellow = "\033[90;43m"
  17. //red = "\033[97;41m"
  18. //blue = "\033[97;44m"
  19. //magenta = "\033[97;45m"
  20. //cyan = "\033[97;46m"
  21. reset = "\033[0m"
  22. )
  23. //
  24. //var consoleColorMode = autoColor
  25. // LoggerConfig defines the config for Logger middleware.
  26. //type LoggerConfig struct {
  27. // // Optional. Default value is gin.defaultLogFormatter
  28. // Formatter LogFormatter
  29. //
  30. // // Output is a writer where logs are written.
  31. // // Optional. Default value is gin.DefaultWriter.
  32. // Output io.Writer
  33. //
  34. // // SkipPaths is a url path array which logs are not written.
  35. // // Optional.
  36. // SkipPaths []string
  37. //}
  38. //
  39. //// LogFormatter gives the signature of the formatter function passed to LoggerWithFormatter
  40. //type LogFormatter func(params LogFormatterParams) string
  41. //
  42. //// LogFormatterParams is the structure any formatter will be handed when time to log comes
  43. //type LogFormatterParams struct {
  44. // Request *http.Request
  45. //
  46. // // TimeStamp shows the time after the server returns a response.
  47. // TimeStamp time.Time
  48. // // StatusCode is HTTP response code.
  49. // StatusCode int
  50. // // Latency is how much time the server cost to process a certain request.
  51. // Latency time.Duration
  52. // // ClientIP equals Context's ClientIP method.
  53. // ClientIP string
  54. // // Method is the HTTP method given to the request.
  55. // Method string
  56. // // Path is a path the client requests.
  57. // Path string
  58. // // ErrorMessage is set if error has occurred in processing the request.
  59. // ErrorMessage string
  60. // // isTerm shows whether does gin's output descriptor refers to a terminal.
  61. // isTerm bool
  62. // // BodySize is the size of the Response Body
  63. // BodySize int
  64. // // Keys are the keys set on the request's context.
  65. // Keys map[string]interface{}
  66. //}
  67. //
  68. //// StatusCodeColor is the ANSI color for appropriately logging http status code to a terminal.
  69. //func (p *LogFormatterParams) StatusCodeColor() string {
  70. // code := p.StatusCode
  71. //
  72. // switch {
  73. // case code >= http.StatusOK && code < http.StatusMultipleChoices:
  74. // return green
  75. // case code >= http.StatusMultipleChoices && code < http.StatusBadRequest:
  76. // return white
  77. // case code >= http.StatusBadRequest && code < http.StatusInternalServerError:
  78. // return yellow
  79. // default:
  80. // return red
  81. // }
  82. //}
  83. //
  84. //// MethodColor is the ANSI color for appropriately logging http method to a terminal.
  85. //func (p *LogFormatterParams) MethodColor() string {
  86. // method := p.Method
  87. //
  88. // switch method {
  89. // case "GET":
  90. // return blue
  91. // case "POST":
  92. // return cyan
  93. // case "PUT":
  94. // return yellow
  95. // case "DELETE":
  96. // return red
  97. // case "PATCH":
  98. // return green
  99. // case "HEAD":
  100. // return magenta
  101. // case "OPTIONS":
  102. // return white
  103. // default:
  104. // return reset
  105. // }
  106. //}
  107. //
  108. //// ResetColor resets all escape attributes.
  109. //func (p *LogFormatterParams) ResetColor() string {
  110. // return reset
  111. //}
  112. //
  113. //// IsOutputColor indicates whether can colors be outputted to the log.
  114. //func (p *LogFormatterParams) IsOutputColor() bool {
  115. // return consoleColorMode == forceColor || (consoleColorMode == autoColor && p.isTerm)
  116. //}
  117. //
  118. //// defaultLogFormatter is the default log format function Logger middleware uses.
  119. //var defaultLogFormatter = func(param LogFormatterParams) string {
  120. // var statusColor, methodColor, resetColor string
  121. // if param.IsOutputColor() {
  122. // statusColor = param.StatusCodeColor()
  123. // methodColor = param.MethodColor()
  124. // resetColor = param.ResetColor()
  125. // }
  126. //
  127. // if param.Latency > time.Minute {
  128. // // Truncate in a golang < 1.8 safe way
  129. // param.Latency = param.Latency - param.Latency%time.Second
  130. // }
  131. // return fmt.Sprintf("[GAUSS] %v |%s %3d %s| %13v | %15s |%s %-7s %s %s\n%s",
  132. // param.TimeStamp.Format("2006/01/02 - 15:04:05"),
  133. // statusColor, param.StatusCode, resetColor,
  134. // param.Latency,
  135. // param.ClientIP,
  136. // methodColor, param.Method, resetColor,
  137. // param.Path,
  138. // param.ErrorMessage,
  139. // )
  140. //}
  141. //
  142. //// DisableConsoleColor disables color output in the console.
  143. //func DisableConsoleColor() {
  144. // consoleColorMode = disableColor
  145. //}
  146. //
  147. //// ForceConsoleColor force color output in the console.
  148. //func ForceConsoleColor() {
  149. // consoleColorMode = forceColor
  150. //}
  151. //
  152. //// ErrorLogger returns a handlerfunc for any error type.
  153. //func ErrorLogger() HandlerFunc {
  154. // return ErrorLoggerT(ErrorTypeAny)
  155. //}
  156. //
  157. //// ErrorLoggerT returns a handlerfunc for a given error type.
  158. //func ErrorLoggerT(typ ErrorType) HandlerFunc {
  159. // return func(c *Context) {
  160. // c.Next()
  161. // errors := c.Errors.ByType(typ)
  162. // if len(errors) > 0 {
  163. // c.JSON(-1, errors)
  164. // }
  165. // }
  166. //}
  167. //
  168. //// Logger instances a Logger middleware that will write the logs to gin.DefaultWriter.
  169. //// By default gin.DefaultWriter = os.Stdout.
  170. ////func Logger() HandlerFunc {
  171. //// return LoggerWithConfig(LoggerConfig{})
  172. ////}
  173. //
  174. //// LoggerWithFormatter instance a Logger middleware with the specified log format function.
  175. //func LoggerWithFormatter(f LogFormatter) HandlerFunc {
  176. // return LoggerWithConfig(LoggerConfig{
  177. // Formatter: f,
  178. // })
  179. //}
  180. //
  181. //// LoggerWithWriter instance a Logger middleware with the specified writer buffer.
  182. //// Example: os.Stdout, a file opened in write mode, a socket...
  183. //func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc {
  184. // return LoggerWithConfig(LoggerConfig{
  185. // Output: out,
  186. // SkipPaths: notlogged,
  187. // })
  188. //}
  189. //
  190. //// LoggerWithConfig instance a Logger middleware with config.
  191. //func LoggerWithConfig(conf LoggerConfig) HandlerFunc {
  192. // formatter := conf.Formatter
  193. // if formatter == nil {
  194. // formatter = defaultLogFormatter
  195. // }
  196. //
  197. // out := conf.Output
  198. // if out == nil {
  199. // out = DefaultWriter
  200. // }
  201. //
  202. // notlogged := conf.SkipPaths
  203. //
  204. // isTerm := true
  205. //
  206. // if w, ok := out.(*os.File); !ok || os.Getenv("TERM") == "dumb" ||
  207. // (!isatty.IsTerminal(w.Fd()) && !isatty.IsCygwinTerminal(w.Fd())) {
  208. // isTerm = false
  209. // }
  210. //
  211. // var skip map[string]struct{}
  212. //
  213. // if length := len(notlogged); length > 0 {
  214. // skip = make(map[string]struct{}, length)
  215. //
  216. // for _, path := range notlogged {
  217. // skip[path] = struct{}{}
  218. // }
  219. // }
  220. //
  221. // return func(c *Context) {
  222. // // Start timer
  223. // start := time.Now()
  224. // path := c.Request.URL.Path
  225. // raw := c.Request.URL.RawQuery
  226. //
  227. // // Process request
  228. // c.Next()
  229. //
  230. // // Log only when path is not being skipped
  231. // if _, ok := skip[path]; !ok {
  232. // param := LogFormatterParams{
  233. // Request: c.Request,
  234. // isTerm: isTerm,
  235. // Keys: c.Keys,
  236. // }
  237. //
  238. // // Stop timer
  239. // param.TimeStamp = time.Now()
  240. // param.Latency = param.TimeStamp.Sub(start)
  241. //
  242. // param.ClientIP = c.ClientIP()
  243. // param.Method = c.Request.Method
  244. // param.StatusCode = c.Writer.Status()
  245. // param.ErrorMessage = c.Errors.ByType(ErrorTypePrivate).String()
  246. //
  247. // param.BodySize = c.Writer.Size()
  248. //
  249. // if raw != "" {
  250. // path = path + "?" + raw
  251. // }
  252. //
  253. // param.Path = path
  254. //
  255. // fmt.Fprint(out, formatter(param))
  256. // }
  257. // }
  258. //}