log_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2020 The Gogs 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 conf
  5. import (
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "gopkg.in/ini.v1"
  10. log "unknwon.dev/clog/v2"
  11. )
  12. func Test_initLogConf(t *testing.T) {
  13. t.Run("missing configuration section", func(t *testing.T) {
  14. f, err := ini.Load([]byte(`
  15. [log]
  16. MODE = console
  17. `))
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. got, hasConsole, err := initLogConf(f, false)
  22. assert.NotNil(t, err)
  23. assert.Equal(t, `missing configuration section [log.console] for "console" logger`, err.Error())
  24. assert.False(t, hasConsole)
  25. assert.Nil(t, got)
  26. })
  27. t.Run("no console logger", func(t *testing.T) {
  28. f, err := ini.Load([]byte(`
  29. [log]
  30. MODE = file
  31. [log.file]
  32. `))
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. got, hasConsole, err := initLogConf(f, false)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. assert.False(t, hasConsole)
  41. assert.NotNil(t, got)
  42. })
  43. f, err := ini.Load([]byte(`
  44. [log]
  45. ROOT_PATH = log
  46. MODE = console, file, slack, discord
  47. BUFFER_LEN = 50
  48. LEVEL = trace
  49. [log.console]
  50. BUFFER_LEN = 10
  51. [log.file]
  52. LEVEL = INFO
  53. LOG_ROTATE = true
  54. DAILY_ROTATE = true
  55. MAX_SIZE_SHIFT = 20
  56. MAX_LINES = 1000
  57. MAX_DAYS = 3
  58. [log.slack]
  59. LEVEL = Warn
  60. URL = https://slack.com
  61. [log.discord]
  62. LEVEL = error
  63. URL = https://discordapp.com
  64. USERNAME = yoyo
  65. `))
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. got, hasConsole, err := initLogConf(f, false)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. logConf := &logConf{
  74. RootPath: filepath.Join(WorkDir(), "log"),
  75. Modes: []string{
  76. log.DefaultConsoleName,
  77. log.DefaultFileName,
  78. log.DefaultSlackName,
  79. log.DefaultDiscordName,
  80. },
  81. Configs: []*loggerConf{
  82. {
  83. Buffer: 10,
  84. Config: log.ConsoleConfig{
  85. Level: log.LevelTrace,
  86. },
  87. }, {
  88. Buffer: 50,
  89. Config: log.FileConfig{
  90. Level: log.LevelInfo,
  91. Filename: filepath.Join(WorkDir(), "log", "gogs.log"),
  92. FileRotationConfig: log.FileRotationConfig{
  93. Rotate: true,
  94. Daily: true,
  95. MaxSize: 1 << 20,
  96. MaxLines: 1000,
  97. MaxDays: 3,
  98. },
  99. },
  100. }, {
  101. Buffer: 50,
  102. Config: log.SlackConfig{
  103. Level: log.LevelWarn,
  104. URL: "https://slack.com",
  105. },
  106. }, {
  107. Buffer: 50,
  108. Config: log.DiscordConfig{
  109. Level: log.LevelError,
  110. URL: "https://discordapp.com",
  111. Username: "yoyo",
  112. },
  113. },
  114. },
  115. }
  116. assert.True(t, hasConsole)
  117. assert.Equal(t, logConf, got)
  118. }