log.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package utils
  2. import (
  3. "encoding/json"
  4. "github.com/ghettovoice/gosip/log"
  5. "go.uber.org/zap"
  6. "go.uber.org/zap/zapcore"
  7. m7slog "m7s.live/engine/v4/log"
  8. )
  9. type ZapLogger struct {
  10. log *m7slog.Logger
  11. prefix string
  12. fields log.Fields
  13. sugared *zap.SugaredLogger
  14. level log.Level
  15. }
  16. func NewZapLogger(log *m7slog.Logger, prefix string, fields log.Fields) (z *ZapLogger) {
  17. z = &ZapLogger{
  18. log: log,
  19. prefix: prefix,
  20. fields: fields,
  21. }
  22. z.sugared = z.prepareEntry()
  23. return
  24. }
  25. func (l *ZapLogger) Print(args ...interface{}) {
  26. if l.level >= log.InfoLevel {
  27. l.sugared.Info(args...)
  28. }
  29. }
  30. func (l *ZapLogger) Printf(format string, args ...interface{}) {
  31. if l.level >= log.InfoLevel {
  32. l.sugared.Infof(format, args...)
  33. }
  34. }
  35. func (l *ZapLogger) Trace(args ...interface{}) {
  36. if l.level >= log.TraceLevel {
  37. l.sugared.Debug(args...)
  38. }
  39. }
  40. func (l *ZapLogger) Tracef(format string, args ...interface{}) {
  41. if l.level >= log.TraceLevel {
  42. l.sugared.Debugf(format, args...)
  43. }
  44. }
  45. func (l *ZapLogger) Debug(args ...interface{}) {
  46. if l.level >= log.DebugLevel {
  47. l.sugared.Debug(args...)
  48. }
  49. }
  50. func (l *ZapLogger) Debugf(format string, args ...interface{}) {
  51. if l.level >= log.DebugLevel {
  52. l.sugared.Debugf(format, args...)
  53. }
  54. }
  55. func (l *ZapLogger) Info(args ...interface{}) {
  56. if l.level >= log.InfoLevel {
  57. l.sugared.Info(args...)
  58. }
  59. }
  60. func (l *ZapLogger) Infof(format string, args ...interface{}) {
  61. if l.level >= log.InfoLevel {
  62. l.sugared.Infof(format, args...)
  63. }
  64. }
  65. func (l *ZapLogger) Warn(args ...interface{}) {
  66. if l.level >= log.WarnLevel {
  67. l.sugared.Warn(args...)
  68. }
  69. }
  70. func (l *ZapLogger) Warnf(format string, args ...interface{}) {
  71. if l.level >= log.WarnLevel {
  72. l.sugared.Warnf(format, args...)
  73. }
  74. }
  75. func (l *ZapLogger) Error(args ...interface{}) {
  76. if l.level >= log.ErrorLevel {
  77. l.sugared.Error(args...)
  78. }
  79. }
  80. func (l *ZapLogger) Errorf(format string, args ...interface{}) {
  81. if l.level >= log.ErrorLevel {
  82. l.sugared.Errorf(format, args...)
  83. }
  84. }
  85. func (l *ZapLogger) Fatal(args ...interface{}) {
  86. if l.level >= log.FatalLevel {
  87. l.sugared.Fatal(args...)
  88. }
  89. }
  90. func (l *ZapLogger) Fatalf(format string, args ...interface{}) {
  91. if l.level >= log.FatalLevel {
  92. l.sugared.Fatalf(format, args...)
  93. }
  94. }
  95. func (l *ZapLogger) Panic(args ...interface{}) {
  96. if l.level >= log.PanicLevel {
  97. l.sugared.Panic(args...)
  98. }
  99. }
  100. func (l *ZapLogger) Panicf(format string, args ...interface{}) {
  101. if l.level >= log.PanicLevel {
  102. l.sugared.Panicf(format, args...)
  103. }
  104. }
  105. func (l *ZapLogger) WithPrefix(prefix string) log.Logger {
  106. return NewZapLogger(l.log, prefix, l.Fields())
  107. }
  108. func (l *ZapLogger) Prefix() string {
  109. return l.prefix
  110. }
  111. func (l *ZapLogger) WithFields(fields map[string]interface{}) log.Logger {
  112. return NewZapLogger(l.log, l.Prefix(), l.Fields().WithFields(fields))
  113. }
  114. func (l *ZapLogger) Fields() log.Fields {
  115. return l.fields
  116. }
  117. func (l *ZapLogger) prepareEntry() *zap.SugaredLogger {
  118. newlog := l.log.With(zap.String("prefix", l.Prefix()))
  119. if l.fields != nil {
  120. fields := make([]zapcore.Field, len(l.fields))
  121. idx := 0
  122. for k, v := range l.fields {
  123. s, _ := json.Marshal(v)
  124. fields[idx] = zap.String(k, string(s))
  125. idx++
  126. }
  127. newlog = newlog.With(fields...)
  128. }
  129. return newlog.Sugar()
  130. }
  131. func (l *ZapLogger) SetLevel(level uint32) {
  132. l.level = log.Level(level)
  133. }