rotatelogger_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package logx
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. "time"
  7. "git.i2edu.net/i2/go-zero/core/fs"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestDailyRotateRuleMarkRotated(t *testing.T) {
  11. var rule DailyRotateRule
  12. rule.MarkRotated()
  13. assert.Equal(t, getNowDate(), rule.rotatedTime)
  14. }
  15. func TestDailyRotateRuleOutdatedFiles(t *testing.T) {
  16. var rule DailyRotateRule
  17. assert.Empty(t, rule.OutdatedFiles())
  18. rule.days = 1
  19. assert.Empty(t, rule.OutdatedFiles())
  20. rule.gzip = true
  21. assert.Empty(t, rule.OutdatedFiles())
  22. }
  23. func TestDailyRotateRuleShallRotate(t *testing.T) {
  24. var rule DailyRotateRule
  25. rule.rotatedTime = time.Now().Add(time.Hour * 24).Format(dateFormat)
  26. assert.True(t, rule.ShallRotate())
  27. }
  28. func TestRotateLoggerClose(t *testing.T) {
  29. filename, err := fs.TempFilenameWithText("foo")
  30. assert.Nil(t, err)
  31. if len(filename) > 0 {
  32. defer os.Remove(filename)
  33. }
  34. logger, err := NewLogger(filename, new(DailyRotateRule), false)
  35. assert.Nil(t, err)
  36. assert.Nil(t, logger.Close())
  37. }
  38. func TestRotateLoggerGetBackupFilename(t *testing.T) {
  39. filename, err := fs.TempFilenameWithText("foo")
  40. assert.Nil(t, err)
  41. if len(filename) > 0 {
  42. defer os.Remove(filename)
  43. }
  44. logger, err := NewLogger(filename, new(DailyRotateRule), false)
  45. assert.Nil(t, err)
  46. assert.True(t, len(logger.getBackupFilename()) > 0)
  47. logger.backup = ""
  48. assert.True(t, len(logger.getBackupFilename()) > 0)
  49. }
  50. func TestRotateLoggerMayCompressFile(t *testing.T) {
  51. filename, err := fs.TempFilenameWithText("foo")
  52. assert.Nil(t, err)
  53. if len(filename) > 0 {
  54. defer os.Remove(filename)
  55. }
  56. logger, err := NewLogger(filename, new(DailyRotateRule), false)
  57. assert.Nil(t, err)
  58. logger.maybeCompressFile(filename)
  59. _, err = os.Stat(filename)
  60. assert.Nil(t, err)
  61. }
  62. func TestRotateLoggerMayCompressFileTrue(t *testing.T) {
  63. filename, err := fs.TempFilenameWithText("foo")
  64. assert.Nil(t, err)
  65. logger, err := NewLogger(filename, new(DailyRotateRule), true)
  66. assert.Nil(t, err)
  67. if len(filename) > 0 {
  68. defer func() {
  69. os.Remove(filename)
  70. os.Remove(filepath.Base(logger.getBackupFilename()) + ".gz")
  71. }()
  72. }
  73. logger.maybeCompressFile(filename)
  74. _, err = os.Stat(filename)
  75. assert.NotNil(t, err)
  76. }
  77. func TestRotateLoggerRotate(t *testing.T) {
  78. filename, err := fs.TempFilenameWithText("foo")
  79. assert.Nil(t, err)
  80. logger, err := NewLogger(filename, new(DailyRotateRule), true)
  81. assert.Nil(t, err)
  82. if len(filename) > 0 {
  83. defer func() {
  84. os.Remove(filename)
  85. os.Remove(logger.getBackupFilename())
  86. os.Remove(filepath.Base(logger.getBackupFilename()) + ".gz")
  87. }()
  88. }
  89. err = logger.rotate()
  90. assert.Nil(t, err)
  91. }
  92. func TestRotateLoggerWrite(t *testing.T) {
  93. filename, err := fs.TempFilenameWithText("foo")
  94. assert.Nil(t, err)
  95. rule := new(DailyRotateRule)
  96. logger, err := NewLogger(filename, rule, true)
  97. assert.Nil(t, err)
  98. if len(filename) > 0 {
  99. defer func() {
  100. os.Remove(filename)
  101. os.Remove(logger.getBackupFilename())
  102. os.Remove(filepath.Base(logger.getBackupFilename()) + ".gz")
  103. }()
  104. }
  105. logger.write([]byte(`foo`))
  106. rule.rotatedTime = time.Now().Add(-time.Hour * 24).Format(dateFormat)
  107. logger.write([]byte(`bar`))
  108. }