Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

zerolog_test.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package logger_test
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "strings"
  7. "testing"
  8. "time"
  9. "github.com/9seconds/mtg/v2/logger"
  10. "github.com/9seconds/mtg/v2/mtglib"
  11. "github.com/rs/zerolog"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/suite"
  14. )
  15. type zeroLoggerLogMessage struct {
  16. Timestamp int64 `json:"timestamp"`
  17. Level string `json:"level"`
  18. StrParam string `json:"strparam"`
  19. IntParam int `json:"intparam"`
  20. Logger string `json:"logger"`
  21. Error string `json:"error"`
  22. Message string `json:"message"`
  23. }
  24. type ZeroLoggerTestSuite struct {
  25. suite.Suite
  26. }
  27. func (suite *ZeroLoggerTestSuite) SetupSuite() {
  28. zerolog.SetGlobalLevel(zerolog.TraceLevel)
  29. zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
  30. zerolog.TimestampFieldName = "timestamp"
  31. zerolog.LevelFieldName = "level"
  32. }
  33. func (suite *ZeroLoggerTestSuite) TestLog() {
  34. testData := map[string]func(mtglib.Logger){
  35. "info": func(l mtglib.Logger) { l.Info("hello") },
  36. "warn": func(l mtglib.Logger) { l.Warning("hello") },
  37. "debug": func(l mtglib.Logger) { l.Debug("hello") },
  38. "info-error": func(l mtglib.Logger) { l.InfoError("hello", io.EOF) },
  39. "warn-error": func(l mtglib.Logger) { l.WarningError("hello", io.EOF) },
  40. "debug-error": func(l mtglib.Logger) { l.DebugError("hello", io.EOF) },
  41. }
  42. for k, v := range testData {
  43. name := k
  44. callback := v
  45. level := strings.TrimSuffix(name, "-error")
  46. suite.T().Run(name, func(t *testing.T) {
  47. buf := &bytes.Buffer{}
  48. log := logger.NewZeroLogger(zerolog.New(buf).With().Timestamp().Logger())
  49. callback(log.Named("name").BindInt("intparam", 1).BindStr("strparam", name))
  50. msg := &zeroLoggerLogMessage{}
  51. assert.NoError(t, json.Unmarshal(buf.Bytes(), msg))
  52. timestamp := time.Unix(msg.Timestamp/1000, (msg.Timestamp%1000)*1_000_000)
  53. assert.WithinDuration(t, time.Now(), timestamp, 100*time.Millisecond)
  54. assert.Equal(t, level, msg.Level)
  55. assert.Equal(t, name, msg.StrParam)
  56. assert.EqualValues(t, 1, msg.IntParam)
  57. assert.Equal(t, "name", msg.Logger)
  58. assert.Equal(t, "hello", msg.Message)
  59. if level != name {
  60. assert.Equal(t, io.EOF.Error(), msg.Error)
  61. } else {
  62. assert.Empty(t, msg.Error)
  63. }
  64. })
  65. }
  66. }
  67. func (suite *ZeroLoggerTestSuite) TestIndependence() {
  68. buf := &bytes.Buffer{}
  69. log := logger.NewZeroLogger(zerolog.New(buf).With().Timestamp().Logger())
  70. log1 := log.Named("1")
  71. log2 := log.Named("2")
  72. log12 := log1.Named("2")
  73. log1.BindInt("param", 1).Info("hello")
  74. log1Output := buf.String()
  75. buf.Reset()
  76. log2.BindInt("lalala", 2).Info("hello")
  77. log2Output := buf.String()
  78. buf.Reset()
  79. log12.BindStr("tttt", "qqq").Info("hello")
  80. log12Output := buf.String()
  81. suite.NotContains("lalala", log1Output)
  82. suite.NotContains("tttt", log1Output)
  83. suite.NotContains("param", log2Output)
  84. suite.NotContains("tttt", log1Output)
  85. suite.NotContains("param", log12Output)
  86. suite.NotContains("lalala", log12Output)
  87. }
  88. func TestZeroLogger(t *testing.T) { // nolint: paralleltest
  89. suite.Run(t, &ZeroLoggerTestSuite{})
  90. }