1
0

logger.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright 2014 beego Author. All Rights Reserved.
  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 logs
  15. import (
  16. "fmt"
  17. "io"
  18. "os"
  19. "sync"
  20. "time"
  21. )
  22. type logWriter struct {
  23. sync.Mutex
  24. writer io.Writer
  25. }
  26. func newLogWriter(wr io.Writer) *logWriter {
  27. return &logWriter{writer: wr}
  28. }
  29. func (lg *logWriter) println(when time.Time, msg string) {
  30. lg.Lock()
  31. h, _, _:= formatTimeHeader(when)
  32. lg.writer.Write(append(append(h, msg...), '\n'))
  33. lg.Unlock()
  34. }
  35. type outputMode int
  36. // DiscardNonColorEscSeq supports the divided color escape sequence.
  37. // But non-color escape sequence is not output.
  38. // Please use the OutputNonColorEscSeq If you want to output a non-color
  39. // escape sequences such as ncurses. However, it does not support the divided
  40. // color escape sequence.
  41. const (
  42. _ outputMode = iota
  43. DiscardNonColorEscSeq
  44. OutputNonColorEscSeq
  45. )
  46. // NewAnsiColorWriter creates and initializes a new ansiColorWriter
  47. // using io.Writer w as its initial contents.
  48. // In the console of Windows, which change the foreground and background
  49. // colors of the text by the escape sequence.
  50. // In the console of other systems, which writes to w all text.
  51. func NewAnsiColorWriter(w io.Writer) io.Writer {
  52. return NewModeAnsiColorWriter(w, DiscardNonColorEscSeq)
  53. }
  54. // NewModeAnsiColorWriter create and initializes a new ansiColorWriter
  55. // by specifying the outputMode.
  56. func NewModeAnsiColorWriter(w io.Writer, mode outputMode) io.Writer {
  57. if _, ok := w.(*ansiColorWriter); !ok {
  58. return &ansiColorWriter{
  59. w: w,
  60. mode: mode,
  61. }
  62. }
  63. return w
  64. }
  65. const (
  66. y1 = `0123456789`
  67. y2 = `0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789`
  68. y3 = `0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999`
  69. y4 = `0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789`
  70. mo1 = `000000000111`
  71. mo2 = `123456789012`
  72. d1 = `0000000001111111111222222222233`
  73. d2 = `1234567890123456789012345678901`
  74. h1 = `000000000011111111112222`
  75. h2 = `012345678901234567890123`
  76. mi1 = `000000000011111111112222222222333333333344444444445555555555`
  77. mi2 = `012345678901234567890123456789012345678901234567890123456789`
  78. s1 = `000000000011111111112222222222333333333344444444445555555555`
  79. s2 = `012345678901234567890123456789012345678901234567890123456789`
  80. ns1 = `0123456789`
  81. )
  82. func formatTimeHeader(when time.Time) ([]byte, int, int) {
  83. y, mo, d := when.Date()
  84. h, mi, s := when.Clock()
  85. ns := when.Nanosecond() / 1000000
  86. //len("2006/01/02 15:04:05.123 ")==24
  87. var buf [24]byte
  88. buf[0] = y1[y/1000%10]
  89. buf[1] = y2[y/100]
  90. buf[2] = y3[y-y/100*100]
  91. buf[3] = y4[y-y/100*100]
  92. buf[4] = '/'
  93. buf[5] = mo1[mo-1]
  94. buf[6] = mo2[mo-1]
  95. buf[7] = '/'
  96. buf[8] = d1[d-1]
  97. buf[9] = d2[d-1]
  98. buf[10] = ' '
  99. buf[11] = h1[h]
  100. buf[12] = h2[h]
  101. buf[13] = ':'
  102. buf[14] = mi1[mi]
  103. buf[15] = mi2[mi]
  104. buf[16] = ':'
  105. buf[17] = s1[s]
  106. buf[18] = s2[s]
  107. buf[19] = '.'
  108. buf[20] = ns1[ns/100]
  109. buf[21] = ns1[ns%100/10]
  110. buf[22] = ns1[ns%10]
  111. buf[23] = ' '
  112. return buf[0:], d, h
  113. }
  114. var (
  115. green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
  116. white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
  117. yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109})
  118. red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
  119. blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
  120. magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
  121. cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
  122. w32Green = string([]byte{27, 91, 52, 50, 109})
  123. w32White = string([]byte{27, 91, 52, 55, 109})
  124. w32Yellow = string([]byte{27, 91, 52, 51, 109})
  125. w32Red = string([]byte{27, 91, 52, 49, 109})
  126. w32Blue = string([]byte{27, 91, 52, 52, 109})
  127. w32Magenta = string([]byte{27, 91, 52, 53, 109})
  128. w32Cyan = string([]byte{27, 91, 52, 54, 109})
  129. reset = string([]byte{27, 91, 48, 109})
  130. )
  131. // ColorByStatus return color by http code
  132. // 2xx return Green
  133. // 3xx return White
  134. // 4xx return Yellow
  135. // 5xx return Red
  136. func ColorByStatus(cond bool, code int) string {
  137. switch {
  138. case code >= 200 && code < 300:
  139. return map[bool]string{true: green, false: w32Green}[cond]
  140. case code >= 300 && code < 400:
  141. return map[bool]string{true: white, false: w32White}[cond]
  142. case code >= 400 && code < 500:
  143. return map[bool]string{true: yellow, false: w32Yellow}[cond]
  144. default:
  145. return map[bool]string{true: red, false: w32Red}[cond]
  146. }
  147. }
  148. // ColorByMethod return color by http code
  149. // GET return Blue
  150. // POST return Cyan
  151. // PUT return Yellow
  152. // DELETE return Red
  153. // PATCH return Green
  154. // HEAD return Magenta
  155. // OPTIONS return WHITE
  156. func ColorByMethod(cond bool, method string) string {
  157. switch method {
  158. case "GET":
  159. return map[bool]string{true: blue, false: w32Blue}[cond]
  160. case "POST":
  161. return map[bool]string{true: cyan, false: w32Cyan}[cond]
  162. case "PUT":
  163. return map[bool]string{true: yellow, false: w32Yellow}[cond]
  164. case "DELETE":
  165. return map[bool]string{true: red, false: w32Red}[cond]
  166. case "PATCH":
  167. return map[bool]string{true: green, false: w32Green}[cond]
  168. case "HEAD":
  169. return map[bool]string{true: magenta, false: w32Magenta}[cond]
  170. case "OPTIONS":
  171. return map[bool]string{true: white, false: w32White}[cond]
  172. default:
  173. return reset
  174. }
  175. }
  176. // Guard Mutex to guarantee atomic of W32Debug(string) function
  177. var mu sync.Mutex
  178. // W32Debug Helper method to output colored logs in Windows terminals
  179. func W32Debug(msg string) {
  180. mu.Lock()
  181. defer mu.Unlock()
  182. current := time.Now()
  183. w := NewAnsiColorWriter(os.Stdout)
  184. fmt.Fprintf(w, "[beego] %v %s\n", current.Format("2006/01/02 - 15:04:05"), msg)
  185. }