log_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package test
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. "github.com/ydb-platform/ydb/library/go/core/log"
  6. "github.com/ydb-platform/ydb/library/go/core/log/nop"
  7. "github.com/ydb-platform/ydb/library/go/core/log/zap"
  8. uzap "go.uber.org/zap"
  9. )
  10. var (
  11. msg = "msg"
  12. msgfmt = "%s %s"
  13. msgfmtargs = []interface{}{"hello", "world"}
  14. key = "key"
  15. value = "value"
  16. withKey = "withKey"
  17. withValue = "withValue"
  18. )
  19. var loggersToTest = []struct {
  20. name string
  21. factory func(level log.Level) (log.Logger, error)
  22. }{
  23. {
  24. name: "Zap",
  25. factory: func(level log.Level) (log.Logger, error) {
  26. cfg := zap.JSONConfig(level)
  27. // Disable output
  28. cfg.OutputPaths = []string{}
  29. cfg.ErrorOutputPaths = []string{}
  30. return zap.New(cfg)
  31. },
  32. },
  33. {
  34. name: "ZapNop",
  35. factory: func(level log.Level) (log.Logger, error) {
  36. return &zap.Logger{
  37. L: uzap.NewNop(),
  38. }, nil
  39. },
  40. },
  41. {
  42. name: "Nop",
  43. factory: func(level log.Level) (log.Logger, error) {
  44. return &nop.Logger{}, nil
  45. },
  46. },
  47. }
  48. func TestLoggers(t *testing.T) {
  49. for _, loggerInput := range loggersToTest {
  50. for _, level := range log.Levels() {
  51. t.Run("Construct "+loggerInput.name+level.String(), func(t *testing.T) {
  52. logger, err := loggerInput.factory(level)
  53. require.NoError(t, err)
  54. require.NotNil(t, logger)
  55. lfmt := logger.Fmt()
  56. require.NotNil(t, lfmt)
  57. l := lfmt.Structured()
  58. require.NotNil(t, l)
  59. require.Equal(t, logger, l)
  60. })
  61. t.Run("With "+loggerInput.name+level.String(), func(t *testing.T) {
  62. logger, err := loggerInput.factory(level)
  63. require.NoError(t, err)
  64. require.NotNil(t, logger)
  65. withField := log.String(withKey, withValue)
  66. loggerWith := log.With(logger, withField)
  67. require.NotNil(t, loggerWith)
  68. })
  69. t.Run("AddCallerSkip "+loggerInput.name+level.String(), func(t *testing.T) {
  70. logger, err := loggerInput.factory(level)
  71. require.NoError(t, err)
  72. require.NotNil(t, logger)
  73. loggerCallerSkip := log.AddCallerSkip(logger, 1)
  74. require.NotNil(t, loggerCallerSkip)
  75. })
  76. // TODO: validate log output
  77. t.Run("Logger "+loggerInput.name+level.String(), func(t *testing.T) {
  78. logger, err := loggerInput.factory(level)
  79. require.NoError(t, err)
  80. require.NotNil(t, logger)
  81. logger.Trace(msg, log.String(key, value))
  82. logger.Debug(msg, log.String(key, value))
  83. logger.Info(msg, log.String(key, value))
  84. logger.Warn(msg, log.String(key, value))
  85. logger.Error(msg, log.String(key, value))
  86. // TODO: test fatal
  87. })
  88. // TODO: validate log output
  89. t.Run("LoggerFMT "+loggerInput.name+level.String(), func(t *testing.T) {
  90. logger, err := loggerInput.factory(level)
  91. require.NoError(t, err)
  92. require.NotNil(t, logger)
  93. lfmt := logger.Fmt()
  94. require.NotNil(t, lfmt)
  95. lfmt.Tracef(msgfmt, msgfmtargs...)
  96. lfmt.Debugf(msgfmt, msgfmtargs...)
  97. lfmt.Infof(msgfmt, msgfmtargs...)
  98. lfmt.Warnf(msgfmt, msgfmtargs...)
  99. lfmt.Errorf(msgfmt, msgfmtargs...)
  100. // TODO: test fatal
  101. })
  102. }
  103. }
  104. }