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.

154 lines
4.0 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. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func baseConsoleTest(t *testing.T, logger *Logger) (chan []byte, chan bool) {
  11. written := make(chan []byte)
  12. closed := make(chan bool)
  13. c := CallbackWriteCloser{
  14. callback: func(p []byte, close bool) {
  15. written <- p
  16. closed <- close
  17. },
  18. }
  19. m := logger.MultiChannelledLog
  20. channelledLog := m.GetEventLogger("console")
  21. assert.NotEmpty(t, channelledLog)
  22. realChanLog, ok := channelledLog.(*ChannelledLog)
  23. assert.Equal(t, true, ok)
  24. realCL, ok := realChanLog.loggerProvider.(*ConsoleLogger)
  25. assert.Equal(t, true, ok)
  26. assert.Equal(t, INFO, realCL.Level)
  27. realCL.out = c
  28. format := "test: %s"
  29. args := []interface{}{"A"}
  30. logger.Log(0, INFO, format, args...)
  31. line := <-written
  32. assert.Contains(t, string(line), fmt.Sprintf(format, args...))
  33. assert.Equal(t, false, <-closed)
  34. format = "test2: %s"
  35. logger.Warn(format, args...)
  36. line = <-written
  37. assert.Contains(t, string(line), fmt.Sprintf(format, args...))
  38. assert.Equal(t, false, <-closed)
  39. format = "testerror: %s"
  40. logger.Error(format, args...)
  41. line = <-written
  42. assert.Contains(t, string(line), fmt.Sprintf(format, args...))
  43. assert.Equal(t, false, <-closed)
  44. return written, closed
  45. }
  46. func TestNewLoggerUnexported(t *testing.T) {
  47. level := INFO
  48. logger := newLogger("UNEXPORTED", 0)
  49. err := logger.SetLogger("console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()))
  50. assert.NoError(t, err)
  51. out := logger.MultiChannelledLog.GetEventLogger("console")
  52. assert.NotEmpty(t, out)
  53. chanlog, ok := out.(*ChannelledLog)
  54. assert.Equal(t, true, ok)
  55. assert.Equal(t, "console", chanlog.provider)
  56. assert.Equal(t, INFO, logger.GetLevel())
  57. baseConsoleTest(t, logger)
  58. }
  59. func TestNewLoggger(t *testing.T) {
  60. level := INFO
  61. logger := NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()))
  62. assert.Equal(t, INFO, GetLevel())
  63. assert.Equal(t, false, IsTrace())
  64. assert.Equal(t, false, IsDebug())
  65. assert.Equal(t, true, IsInfo())
  66. assert.Equal(t, true, IsWarn())
  67. assert.Equal(t, true, IsError())
  68. written, closed := baseConsoleTest(t, logger)
  69. format := "test: %s"
  70. args := []interface{}{"A"}
  71. Log(0, INFO, format, args...)
  72. line := <-written
  73. assert.Contains(t, string(line), fmt.Sprintf(format, args...))
  74. assert.Equal(t, false, <-closed)
  75. Info(format, args...)
  76. line = <-written
  77. assert.Contains(t, string(line), fmt.Sprintf(format, args...))
  78. assert.Equal(t, false, <-closed)
  79. go DelLogger("console")
  80. line = <-written
  81. assert.Equal(t, "", string(line))
  82. assert.Equal(t, true, <-closed)
  83. }
  84. func TestNewLogggerRecreate(t *testing.T) {
  85. level := INFO
  86. NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()))
  87. assert.Equal(t, INFO, GetLevel())
  88. assert.Equal(t, false, IsTrace())
  89. assert.Equal(t, false, IsDebug())
  90. assert.Equal(t, true, IsInfo())
  91. assert.Equal(t, true, IsWarn())
  92. assert.Equal(t, true, IsError())
  93. format := "test: %s"
  94. args := []interface{}{"A"}
  95. Log(0, INFO, format, args...)
  96. NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()))
  97. assert.Equal(t, INFO, GetLevel())
  98. assert.Equal(t, false, IsTrace())
  99. assert.Equal(t, false, IsDebug())
  100. assert.Equal(t, true, IsInfo())
  101. assert.Equal(t, true, IsWarn())
  102. assert.Equal(t, true, IsError())
  103. Log(0, INFO, format, args...)
  104. assert.Panics(t, func() {
  105. NewLogger(0, "console", "console", fmt.Sprintf(`{"level":"%s"`, level.String()))
  106. })
  107. go DelLogger("console")
  108. // We should be able to redelete without a problem
  109. go DelLogger("console")
  110. }
  111. func TestNewNamedLogger(t *testing.T) {
  112. level := INFO
  113. err := NewNamedLogger("test", 0, "console", "console", fmt.Sprintf(`{"level":"%s"}`, level.String()))
  114. assert.NoError(t, err)
  115. logger := NamedLoggers["test"]
  116. assert.Equal(t, level, logger.GetLevel())
  117. written, closed := baseConsoleTest(t, logger)
  118. go DelNamedLogger("test")
  119. line := <-written
  120. assert.Equal(t, "", string(line))
  121. assert.Equal(t, true, <-closed)
  122. }