logger.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. )
  81. func formatTimeHeader(when time.Time) ([]byte, int) {
  82. y, mo, d := when.Date()
  83. h, mi, s := when.Clock()
  84. //len("2006/01/02 15:04:05 ")==20
  85. var buf [20]byte
  86. buf[0] = y1[y/1000%10]
  87. buf[1] = y2[y/100]
  88. buf[2] = y3[y-y/100*100]
  89. buf[3] = y4[y-y/100*100]
  90. buf[4] = '/'
  91. buf[5] = mo1[mo-1]
  92. buf[6] = mo2[mo-1]
  93. buf[7] = '/'
  94. buf[8] = d1[d-1]
  95. buf[9] = d2[d-1]
  96. buf[10] = ' '
  97. buf[11] = h1[h]
  98. buf[12] = h2[h]
  99. buf[13] = ':'
  100. buf[14] = mi1[mi]
  101. buf[15] = mi2[mi]
  102. buf[16] = ':'
  103. buf[17] = s1[s]
  104. buf[18] = s2[s]
  105. buf[19] = ' '
  106. return buf[0:], d
  107. }
  108. var (
  109. green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
  110. white = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
  111. yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109})
  112. red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
  113. blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
  114. magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
  115. cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
  116. w32Green = string([]byte{27, 91, 52, 50, 109})
  117. w32White = string([]byte{27, 91, 52, 55, 109})
  118. w32Yellow = string([]byte{27, 91, 52, 51, 109})
  119. w32Red = string([]byte{27, 91, 52, 49, 109})
  120. w32Blue = string([]byte{27, 91, 52, 52, 109})
  121. w32Magenta = string([]byte{27, 91, 52, 53, 109})
  122. w32Cyan = string([]byte{27, 91, 52, 54, 109})
  123. reset = string([]byte{27, 91, 48, 109})
  124. )
  125. func ColorByStatus(cond bool, code int) string {
  126. switch {
  127. case code >= 200 && code < 300:
  128. return map[bool]string{true: green, false: w32Green}[cond]
  129. case code >= 300 && code < 400:
  130. return map[bool]string{true: white, false: w32White}[cond]
  131. case code >= 400 && code < 500:
  132. return map[bool]string{true: yellow, false: w32Yellow}[cond]
  133. default:
  134. return map[bool]string{true: red, false: w32Red}[cond]
  135. }
  136. }
  137. func ColorByMethod(cond bool, method string) string {
  138. switch method {
  139. case "GET":
  140. return map[bool]string{true: blue, false: w32Blue}[cond]
  141. case "POST":
  142. return map[bool]string{true: cyan, false: w32Cyan}[cond]
  143. case "PUT":
  144. return map[bool]string{true: yellow, false: w32Yellow}[cond]
  145. case "DELETE":
  146. return map[bool]string{true: red, false: w32Red}[cond]
  147. case "PATCH":
  148. return map[bool]string{true: green, false: w32Green}[cond]
  149. case "HEAD":
  150. return map[bool]string{true: magenta, false: w32Magenta}[cond]
  151. case "OPTIONS":
  152. return map[bool]string{true: white, false: w32White}[cond]
  153. default:
  154. return reset
  155. }
  156. }
  157. // Guard Mutex to guarantee atomicity of W32Debug(string) function
  158. var mu sync.Mutex
  159. // Helper method to output colored logs in Windows terminals
  160. func W32Debug(msg string) {
  161. mu.Lock()
  162. defer mu.Unlock()
  163. current := time.Now()
  164. w := NewAnsiColorWriter(os.Stdout)
  165. fmt.Fprintf(w, "[beego] %v %s\n", current.Format("2006/01/02 - 15:04:05"), msg)
  166. }