default.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package log
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "log"
  7. "os"
  8. "sync"
  9. "github.com/valyala/bytebufferpool"
  10. )
  11. var _ AllLogger = (*defaultLogger)(nil)
  12. type defaultLogger struct {
  13. stdlog *log.Logger
  14. level Level
  15. depth int
  16. }
  17. // privateLog logs a message at a given level log the default logger.
  18. // when the level is fatal, it will exit the program.
  19. func (l *defaultLogger) privateLog(lv Level, fmtArgs []interface{}) {
  20. if l.level > lv {
  21. return
  22. }
  23. level := lv.toString()
  24. buf := bytebufferpool.Get()
  25. _, _ = buf.WriteString(level) //nolint:errcheck // It is fine to ignore the error
  26. _, _ = buf.WriteString(fmt.Sprint(fmtArgs...)) //nolint:errcheck // It is fine to ignore the error
  27. _ = l.stdlog.Output(l.depth, buf.String()) //nolint:errcheck // It is fine to ignore the error
  28. buf.Reset()
  29. bytebufferpool.Put(buf)
  30. if lv == LevelFatal {
  31. os.Exit(1) //nolint:revive // we want to exit the program when Fatal is called
  32. }
  33. }
  34. // privateLog logs a message at a given level log the default logger.
  35. // when the level is fatal, it will exit the program.
  36. func (l *defaultLogger) privateLogf(lv Level, format string, fmtArgs []interface{}) {
  37. if l.level > lv {
  38. return
  39. }
  40. level := lv.toString()
  41. buf := bytebufferpool.Get()
  42. _, _ = buf.WriteString(level) //nolint:errcheck // It is fine to ignore the error
  43. if len(fmtArgs) > 0 {
  44. _, _ = fmt.Fprintf(buf, format, fmtArgs...)
  45. } else {
  46. _, _ = fmt.Fprint(buf, fmtArgs...)
  47. }
  48. _ = l.stdlog.Output(l.depth, buf.String()) //nolint:errcheck // It is fine to ignore the error
  49. buf.Reset()
  50. bytebufferpool.Put(buf)
  51. if lv == LevelFatal {
  52. os.Exit(1) //nolint:revive // we want to exit the program when Fatal is called
  53. }
  54. }
  55. // privateLogw logs a message at a given level log the default logger.
  56. // when the level is fatal, it will exit the program.
  57. func (l *defaultLogger) privateLogw(lv Level, format string, keysAndValues []interface{}) {
  58. if l.level > lv {
  59. return
  60. }
  61. level := lv.toString()
  62. buf := bytebufferpool.Get()
  63. _, _ = buf.WriteString(level) //nolint:errcheck // It is fine to ignore the error
  64. // Write format privateLog buffer
  65. if format != "" {
  66. _, _ = buf.WriteString(format) //nolint:errcheck // It is fine to ignore the error
  67. }
  68. var once sync.Once
  69. isFirst := true
  70. // Write keys and values privateLog buffer
  71. if len(keysAndValues) > 0 {
  72. if (len(keysAndValues) & 1) == 1 {
  73. keysAndValues = append(keysAndValues, "KEYVALS UNPAIRED")
  74. }
  75. for i := 0; i < len(keysAndValues); i += 2 {
  76. if format == "" && isFirst {
  77. once.Do(func() {
  78. _, _ = fmt.Fprintf(buf, "%s=%v", keysAndValues[i], keysAndValues[i+1])
  79. isFirst = false
  80. })
  81. continue
  82. }
  83. _, _ = fmt.Fprintf(buf, " %s=%v", keysAndValues[i], keysAndValues[i+1])
  84. }
  85. }
  86. _ = l.stdlog.Output(l.depth, buf.String()) //nolint:errcheck // It is fine to ignore the error
  87. buf.Reset()
  88. bytebufferpool.Put(buf)
  89. if lv == LevelFatal {
  90. os.Exit(1) //nolint:revive // we want to exit the program when Fatal is called
  91. }
  92. }
  93. func (l *defaultLogger) Trace(v ...interface{}) {
  94. l.privateLog(LevelTrace, v)
  95. }
  96. func (l *defaultLogger) Debug(v ...interface{}) {
  97. l.privateLog(LevelDebug, v)
  98. }
  99. func (l *defaultLogger) Info(v ...interface{}) {
  100. l.privateLog(LevelInfo, v)
  101. }
  102. func (l *defaultLogger) Warn(v ...interface{}) {
  103. l.privateLog(LevelWarn, v)
  104. }
  105. func (l *defaultLogger) Error(v ...interface{}) {
  106. l.privateLog(LevelError, v)
  107. }
  108. func (l *defaultLogger) Fatal(v ...interface{}) {
  109. l.privateLog(LevelFatal, v)
  110. }
  111. func (l *defaultLogger) Panic(v ...interface{}) {
  112. l.privateLog(LevelPanic, v)
  113. }
  114. func (l *defaultLogger) Tracef(format string, v ...interface{}) {
  115. l.privateLogf(LevelTrace, format, v)
  116. }
  117. func (l *defaultLogger) Debugf(format string, v ...interface{}) {
  118. l.privateLogf(LevelDebug, format, v)
  119. }
  120. func (l *defaultLogger) Infof(format string, v ...interface{}) {
  121. l.privateLogf(LevelInfo, format, v)
  122. }
  123. func (l *defaultLogger) Warnf(format string, v ...interface{}) {
  124. l.privateLogf(LevelWarn, format, v)
  125. }
  126. func (l *defaultLogger) Errorf(format string, v ...interface{}) {
  127. l.privateLogf(LevelError, format, v)
  128. }
  129. func (l *defaultLogger) Fatalf(format string, v ...interface{}) {
  130. l.privateLogf(LevelFatal, format, v)
  131. }
  132. func (l *defaultLogger) Panicf(format string, v ...interface{}) {
  133. l.privateLogf(LevelPanic, format, v)
  134. }
  135. func (l *defaultLogger) Tracew(msg string, keysAndValues ...interface{}) {
  136. l.privateLogw(LevelTrace, msg, keysAndValues)
  137. }
  138. func (l *defaultLogger) Debugw(msg string, keysAndValues ...interface{}) {
  139. l.privateLogw(LevelDebug, msg, keysAndValues)
  140. }
  141. func (l *defaultLogger) Infow(msg string, keysAndValues ...interface{}) {
  142. l.privateLogw(LevelInfo, msg, keysAndValues)
  143. }
  144. func (l *defaultLogger) Warnw(msg string, keysAndValues ...interface{}) {
  145. l.privateLogw(LevelWarn, msg, keysAndValues)
  146. }
  147. func (l *defaultLogger) Errorw(msg string, keysAndValues ...interface{}) {
  148. l.privateLogw(LevelError, msg, keysAndValues)
  149. }
  150. func (l *defaultLogger) Fatalw(msg string, keysAndValues ...interface{}) {
  151. l.privateLogw(LevelFatal, msg, keysAndValues)
  152. }
  153. func (l *defaultLogger) Panicw(msg string, keysAndValues ...interface{}) {
  154. l.privateLogw(LevelPanic, msg, keysAndValues)
  155. }
  156. func (l *defaultLogger) WithContext(_ context.Context) CommonLogger {
  157. return &defaultLogger{
  158. stdlog: l.stdlog,
  159. level: l.level,
  160. depth: l.depth - 1,
  161. }
  162. }
  163. func (l *defaultLogger) SetLevel(level Level) {
  164. l.level = level
  165. }
  166. func (l *defaultLogger) SetOutput(writer io.Writer) {
  167. l.stdlog.SetOutput(writer)
  168. }
  169. // DefaultLogger returns the default logger.
  170. func DefaultLogger() AllLogger {
  171. return logger
  172. }