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 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. "printf": func(l mtglib.Logger) { l.Printf("hello") },
  38. "debug": func(l mtglib.Logger) { l.Debug("hello") },
  39. "info-error": func(l mtglib.Logger) { l.InfoError("hello", io.EOF) },
  40. "warn-error": func(l mtglib.Logger) { l.WarningError("hello", io.EOF) },
  41. "debug-error": func(l mtglib.Logger) { l.DebugError("hello", io.EOF) },
  42. }
  43. for k, v := range testData {
  44. name := k
  45. callback := v
  46. level := strings.TrimSuffix(name, "-error")
  47. suite.T().Run(name, func(t *testing.T) {
  48. buf := &bytes.Buffer{}
  49. log := logger.NewZeroLogger(zerolog.New(buf).With().Timestamp().Logger())
  50. callback(log.Named("name").BindInt("intparam", 1).BindStr("strparam", name))
  51. msg := &zeroLoggerLogMessage{}
  52. assert.NoError(t, json.Unmarshal(buf.Bytes(), msg))
  53. timestamp := time.Unix(msg.Timestamp/1000, (msg.Timestamp%1000)*1_000_000)
  54. assert.WithinDuration(t, time.Now(), timestamp, 100*time.Millisecond)
  55. if level == "printf" {
  56. level = "debug"
  57. }
  58. assert.Equal(t, level, msg.Level)
  59. assert.Equal(t, name, msg.StrParam)
  60. assert.EqualValues(t, 1, msg.IntParam)
  61. assert.Equal(t, "name", msg.Logger)
  62. assert.Equal(t, "hello", msg.Message)
  63. if level != name && name != "printf" {
  64. assert.Equal(t, io.EOF.Error(), msg.Error)
  65. } else {
  66. assert.Empty(t, msg.Error)
  67. }
  68. })
  69. }
  70. }
  71. func (suite *ZeroLoggerTestSuite) TestIndependence() {
  72. buf := &bytes.Buffer{}
  73. log := logger.NewZeroLogger(zerolog.New(buf).With().Timestamp().Logger())
  74. log1 := log.Named("1")
  75. log2 := log.Named("2")
  76. log12 := log1.Named("2")
  77. log1.BindInt("param", 1).Info("hello")
  78. log1Output := buf.String()
  79. buf.Reset()
  80. log2.BindInt("lalala", 2).Info("hello")
  81. log2Output := buf.String()
  82. buf.Reset()
  83. log12.BindStr("tttt", "qqq").Info("hello")
  84. log12Output := buf.String()
  85. suite.NotContains("lalala", log1Output)
  86. suite.NotContains("tttt", log1Output)
  87. suite.NotContains("param", log2Output)
  88. suite.NotContains("tttt", log1Output)
  89. suite.NotContains("param", log12Output)
  90. suite.NotContains("lalala", log12Output)
  91. }
  92. func TestZeroLogger(t *testing.T) { //nolint: paralleltest
  93. suite.Run(t, &ZeroLoggerTestSuite{})
  94. }