You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
5.9 KiB

Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
  1. // Copyright 2019 The Gitea Authors. 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 log
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "net"
  9. "strings"
  10. "sync"
  11. "testing"
  12. "time"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func listenReadAndClose(t *testing.T, l net.Listener, expected string) {
  16. conn, err := l.Accept()
  17. assert.NoError(t, err)
  18. defer conn.Close()
  19. written, err := ioutil.ReadAll(conn)
  20. assert.NoError(t, err)
  21. assert.Equal(t, expected, string(written))
  22. }
  23. func TestConnLogger(t *testing.T) {
  24. var written []byte
  25. protocol := "tcp"
  26. address := ":3099"
  27. l, err := net.Listen(protocol, address)
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. defer l.Close()
  32. prefix := "TestPrefix "
  33. level := INFO
  34. flags := LstdFlags | LUTC | Lfuncname
  35. logger := NewConn()
  36. connLogger := logger.(*ConnLogger)
  37. logger.Init(fmt.Sprintf("{\"prefix\":\"%s\",\"level\":\"%s\",\"flags\":%d,\"reconnectOnMsg\":%t,\"reconnect\":%t,\"net\":\"%s\",\"addr\":\"%s\"}", prefix, level.String(), flags, true, true, protocol, address))
  38. assert.Equal(t, flags, connLogger.Flags)
  39. assert.Equal(t, level, connLogger.Level)
  40. assert.Equal(t, level, logger.GetLevel())
  41. location, _ := time.LoadLocation("EST")
  42. date := time.Date(2019, time.January, 13, 22, 3, 30, 15, location)
  43. dateString := date.UTC().Format("2006/01/02 15:04:05")
  44. event := Event{
  45. level: INFO,
  46. msg: "TEST MSG",
  47. caller: "CALLER",
  48. filename: "FULL/FILENAME",
  49. line: 1,
  50. time: date,
  51. }
  52. expected := fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
  53. var wg sync.WaitGroup
  54. wg.Add(2)
  55. go func() {
  56. defer wg.Done()
  57. listenReadAndClose(t, l, expected)
  58. }()
  59. go func() {
  60. defer wg.Done()
  61. err := logger.LogEvent(&event)
  62. assert.NoError(t, err)
  63. }()
  64. wg.Wait()
  65. written = written[:0]
  66. event.level = WARN
  67. expected = fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
  68. wg.Add(2)
  69. go func() {
  70. defer wg.Done()
  71. listenReadAndClose(t, l, expected)
  72. }()
  73. go func() {
  74. defer wg.Done()
  75. err := logger.LogEvent(&event)
  76. assert.NoError(t, err)
  77. }()
  78. wg.Wait()
  79. logger.Close()
  80. }
  81. func TestConnLoggerBadConfig(t *testing.T) {
  82. logger := NewConn()
  83. err := logger.Init("{")
  84. assert.Equal(t, "unexpected end of JSON input", err.Error())
  85. logger.Close()
  86. }
  87. func TestConnLoggerCloseBeforeSend(t *testing.T) {
  88. protocol := "tcp"
  89. address := ":3099"
  90. prefix := "TestPrefix "
  91. level := INFO
  92. flags := LstdFlags | LUTC | Lfuncname
  93. logger := NewConn()
  94. logger.Init(fmt.Sprintf("{\"prefix\":\"%s\",\"level\":\"%s\",\"flags\":%d,\"reconnectOnMsg\":%t,\"reconnect\":%t,\"net\":\"%s\",\"addr\":\"%s\"}", prefix, level.String(), flags, false, false, protocol, address))
  95. logger.Close()
  96. }
  97. func TestConnLoggerFailConnect(t *testing.T) {
  98. protocol := "tcp"
  99. address := ":3099"
  100. prefix := "TestPrefix "
  101. level := INFO
  102. flags := LstdFlags | LUTC | Lfuncname
  103. logger := NewConn()
  104. logger.Init(fmt.Sprintf("{\"prefix\":\"%s\",\"level\":\"%s\",\"flags\":%d,\"reconnectOnMsg\":%t,\"reconnect\":%t,\"net\":\"%s\",\"addr\":\"%s\"}", prefix, level.String(), flags, false, false, protocol, address))
  105. assert.Equal(t, level, logger.GetLevel())
  106. location, _ := time.LoadLocation("EST")
  107. date := time.Date(2019, time.January, 13, 22, 3, 30, 15, location)
  108. //dateString := date.UTC().Format("2006/01/02 15:04:05")
  109. event := Event{
  110. level: INFO,
  111. msg: "TEST MSG",
  112. caller: "CALLER",
  113. filename: "FULL/FILENAME",
  114. line: 1,
  115. time: date,
  116. }
  117. err := logger.LogEvent(&event)
  118. assert.Error(t, err)
  119. logger.Close()
  120. }
  121. func TestConnLoggerClose(t *testing.T) {
  122. var written []byte
  123. protocol := "tcp"
  124. address := ":3099"
  125. l, err := net.Listen(protocol, address)
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. defer l.Close()
  130. prefix := "TestPrefix "
  131. level := INFO
  132. flags := LstdFlags | LUTC | Lfuncname
  133. logger := NewConn()
  134. connLogger := logger.(*ConnLogger)
  135. logger.Init(fmt.Sprintf("{\"prefix\":\"%s\",\"level\":\"%s\",\"flags\":%d,\"reconnectOnMsg\":%t,\"reconnect\":%t,\"net\":\"%s\",\"addr\":\"%s\"}", prefix, level.String(), flags, false, false, protocol, address))
  136. assert.Equal(t, flags, connLogger.Flags)
  137. assert.Equal(t, level, connLogger.Level)
  138. assert.Equal(t, level, logger.GetLevel())
  139. location, _ := time.LoadLocation("EST")
  140. date := time.Date(2019, time.January, 13, 22, 3, 30, 15, location)
  141. dateString := date.UTC().Format("2006/01/02 15:04:05")
  142. event := Event{
  143. level: INFO,
  144. msg: "TEST MSG",
  145. caller: "CALLER",
  146. filename: "FULL/FILENAME",
  147. line: 1,
  148. time: date,
  149. }
  150. expected := fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
  151. var wg sync.WaitGroup
  152. wg.Add(2)
  153. go func() {
  154. defer wg.Done()
  155. err := logger.LogEvent(&event)
  156. assert.NoError(t, err)
  157. logger.Close()
  158. }()
  159. go func() {
  160. defer wg.Done()
  161. listenReadAndClose(t, l, expected)
  162. }()
  163. wg.Wait()
  164. logger = NewConn()
  165. connLogger = logger.(*ConnLogger)
  166. logger.Init(fmt.Sprintf("{\"prefix\":\"%s\",\"level\":\"%s\",\"flags\":%d,\"reconnectOnMsg\":%t,\"reconnect\":%t,\"net\":\"%s\",\"addr\":\"%s\"}", prefix, level.String(), flags, false, true, protocol, address))
  167. assert.Equal(t, flags, connLogger.Flags)
  168. assert.Equal(t, level, connLogger.Level)
  169. assert.Equal(t, level, logger.GetLevel())
  170. written = written[:0]
  171. event.level = WARN
  172. expected = fmt.Sprintf("%s%s %s:%d:%s [%c] %s\n", prefix, dateString, event.filename, event.line, event.caller, strings.ToUpper(event.level.String())[0], event.msg)
  173. wg.Add(2)
  174. go func() {
  175. defer wg.Done()
  176. listenReadAndClose(t, l, expected)
  177. }()
  178. go func() {
  179. defer wg.Done()
  180. err := logger.LogEvent(&event)
  181. assert.NoError(t, err)
  182. logger.Close()
  183. }()
  184. wg.Wait()
  185. logger.Flush()
  186. logger.Close()
  187. }