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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package logger
  2. import (
  3. "fmt"
  4. "github.com/9seconds/mtg/v2/mtglib"
  5. "github.com/rs/zerolog"
  6. )
  7. const loggerFieldName = "logger"
  8. type zeroLogContextVarType uint8
  9. const (
  10. zeroLogContextVarTypeUnknown zeroLogContextVarType = iota
  11. zeroLogContextVarTypeStr
  12. zeroLogContextVarTypeInt
  13. zeroLogContextVarTypeJSON
  14. )
  15. type zeroLogContext struct {
  16. name string
  17. log *zerolog.Logger
  18. ctxVarType zeroLogContextVarType
  19. ctxVarName string
  20. ctxVarStr string
  21. ctxVarInt int
  22. parent *zeroLogContext
  23. }
  24. func (z *zeroLogContext) Named(name string) mtglib.Logger {
  25. loggerName := z.name
  26. if loggerName == "" {
  27. loggerName = name
  28. } else {
  29. loggerName += "." + name
  30. }
  31. return &zeroLogContext{
  32. name: loggerName,
  33. log: z.log,
  34. parent: z,
  35. }
  36. }
  37. func (z *zeroLogContext) BindInt(name string, value int) mtglib.Logger {
  38. return &zeroLogContext{
  39. name: z.name,
  40. log: z.log,
  41. ctxVarType: zeroLogContextVarTypeInt,
  42. ctxVarInt: value,
  43. ctxVarName: name,
  44. parent: z,
  45. }
  46. }
  47. func (z *zeroLogContext) BindStr(name, value string) mtglib.Logger {
  48. return &zeroLogContext{
  49. name: z.name,
  50. log: z.log,
  51. ctxVarType: zeroLogContextVarTypeStr,
  52. ctxVarStr: value,
  53. ctxVarName: name,
  54. parent: z,
  55. }
  56. }
  57. func (z *zeroLogContext) BindJSON(name, value string) mtglib.Logger {
  58. return &zeroLogContext{
  59. name: z.name,
  60. log: z.log,
  61. ctxVarType: zeroLogContextVarTypeJSON,
  62. ctxVarName: name,
  63. ctxVarStr: value,
  64. parent: z,
  65. }
  66. }
  67. func (z *zeroLogContext) Printf(format string, args ...any) {
  68. z.Debug(fmt.Sprintf(format, args...))
  69. }
  70. func (z *zeroLogContext) Info(msg string) {
  71. z.InfoError(msg, nil)
  72. }
  73. func (z *zeroLogContext) Warning(msg string) {
  74. z.WarningError(msg, nil)
  75. }
  76. func (z *zeroLogContext) Debug(msg string) {
  77. z.DebugError(msg, nil)
  78. }
  79. func (z *zeroLogContext) InfoError(msg string, err error) {
  80. z.emitLog(z.log.Info(), msg, err)
  81. }
  82. func (z *zeroLogContext) WarningError(msg string, err error) {
  83. z.emitLog(z.log.Warn(), msg, err)
  84. }
  85. func (z *zeroLogContext) DebugError(msg string, err error) {
  86. z.emitLog(z.log.Debug(), msg, err)
  87. }
  88. func (z *zeroLogContext) emitLog(evt *zerolog.Event, msg string, err error) {
  89. z.attachCtx(evt)
  90. for current := z.parent; current != nil; current = current.parent {
  91. current.attachCtx(evt)
  92. }
  93. evt.Str(loggerFieldName, z.name).Err(err).Msg(msg)
  94. }
  95. func (z *zeroLogContext) attachCtx(evt *zerolog.Event) {
  96. switch z.ctxVarType {
  97. case zeroLogContextVarTypeStr:
  98. evt.Str(z.ctxVarName, z.ctxVarStr)
  99. case zeroLogContextVarTypeInt:
  100. evt.Int(z.ctxVarName, z.ctxVarInt)
  101. case zeroLogContextVarTypeJSON:
  102. evt.RawJSON(z.ctxVarName, []byte(z.ctxVarStr))
  103. case zeroLogContextVarTypeUnknown:
  104. }
  105. }
  106. // NewZeroLogger returns a logger which is using rs/zerolog library.
  107. func NewZeroLogger(log zerolog.Logger) mtglib.Logger {
  108. return &zeroLogContext{
  109. log: &log,
  110. }
  111. }