基于 `zap` 包封装。除了实现 `Go` 日志包的基本功能外,还实现了很多高级功能 本包基于`github.com/tkestack/tke/pkg/util/log`裁剪
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.

165 lines
5.1 KiB

2 years ago
  1. // Package distribution 实现了兼容 logrus/std log/prometheus 的logger
  2. package distribution
  3. import (
  4. logruslogger "cynking.com/pkg/log/logrus"
  5. "fmt"
  6. "github.com/sirupsen/logrus"
  7. "go.uber.org/zap"
  8. )
  9. // Logger is a logger which compatible to logrus/std log/prometheus.
  10. // it implements Print() Println() Printf() Dbug() Debugln() Debugf() Info() Infoln() Infof() Warn() Warnln() Warnf()
  11. // Error() Errorln() Errorf() Fatal() Fataln() Fatalf() Panic() Panicln() Panicf() With() WithField() WithFields().
  12. type Logger struct {
  13. logger *zap.Logger
  14. logrusLogger *logrus.Logger
  15. }
  16. // NewLogger create the field logger object by giving zap logger.
  17. func NewLogger(logger *zap.Logger) *Logger {
  18. return &Logger{
  19. logger: logger,
  20. logrusLogger: logruslogger.NewLogger(logger),
  21. }
  22. }
  23. // Print logs a message at level Print on the compatibleLogger.
  24. func (l *Logger) Print(args ...interface{}) {
  25. l.logger.Info(fmt.Sprint(args...))
  26. }
  27. // Println logs a message at level Print on the compatibleLogger.
  28. func (l *Logger) Println(args ...interface{}) {
  29. l.logger.Info(fmt.Sprint(args...))
  30. }
  31. // Printf logs a message at level Print on the compatibleLogger.
  32. func (l *Logger) Printf(format string, args ...interface{}) {
  33. l.logger.Info(fmt.Sprintf(format, args...))
  34. }
  35. // Trace logs a message at level Trace on the compatibleLogger.
  36. func (l *Logger) Trace(args ...interface{}) {
  37. l.logger.Debug(fmt.Sprint(args...))
  38. }
  39. // Traceln logs a message at level Trace on the compatibleLogger.
  40. func (l *Logger) Traceln(args ...interface{}) {
  41. l.logger.Debug(fmt.Sprint(args...))
  42. }
  43. // Tracef logs a message at level Trace on the compatibleLogger.
  44. func (l *Logger) Tracef(format string, args ...interface{}) {
  45. l.logger.Debug(fmt.Sprintf(format, args...))
  46. }
  47. // Debug logs a message at level Debug on the compatibleLogger.
  48. func (l *Logger) Debug(args ...interface{}) {
  49. l.logger.Debug(fmt.Sprint(args...))
  50. }
  51. // Debugln logs a message at level Debug on the compatibleLogger.
  52. func (l *Logger) Debugln(args ...interface{}) {
  53. l.logger.Debug(fmt.Sprint(args...))
  54. }
  55. // Debugf logs a message at level Debug on the compatibleLogger.
  56. func (l *Logger) Debugf(format string, args ...interface{}) {
  57. l.logger.Debug(fmt.Sprintf(format, args...))
  58. }
  59. // Info logs a message at level Info on the compatibleLogger.
  60. func (l *Logger) Info(args ...interface{}) {
  61. l.logger.Info(fmt.Sprint(args...))
  62. }
  63. // Infoln logs a message at level Info on the compatibleLogger.
  64. func (l *Logger) Infoln(args ...interface{}) {
  65. l.logger.Info(fmt.Sprint(args...))
  66. }
  67. // Infof logs a message at level Info on the compatibleLogger.
  68. func (l *Logger) Infof(format string, args ...interface{}) {
  69. l.logger.Info(fmt.Sprintf(format, args...))
  70. }
  71. // Warn logs a message at level Warn on the compatibleLogger.
  72. func (l *Logger) Warn(args ...interface{}) {
  73. l.logger.Warn(fmt.Sprint(args...))
  74. }
  75. // Warnln logs a message at level Warn on the compatibleLogger.
  76. func (l *Logger) Warnln(args ...interface{}) {
  77. l.logger.Warn(fmt.Sprint(args...))
  78. }
  79. // Warnf logs a message at level Warn on the compatibleLogger.
  80. func (l *Logger) Warnf(format string, args ...interface{}) {
  81. l.logger.Warn(fmt.Sprintf(format, args...))
  82. }
  83. // Warning logs a message at level Warn on the compatibleLogger.
  84. func (l *Logger) Warning(args ...interface{}) {
  85. l.logger.Warn(fmt.Sprint(args...))
  86. }
  87. // Warningln logs a message at level Warning on the compatibleLogger.
  88. func (l *Logger) Warningln(args ...interface{}) {
  89. l.logger.Warn(fmt.Sprint(args...))
  90. }
  91. // Warningf logs a message at level Warning on the compatibleLogger.
  92. func (l *Logger) Warningf(format string, args ...interface{}) {
  93. l.logger.Warn(fmt.Sprintf(format, args...))
  94. }
  95. // Error logs a message at level Error on the compatibleLogger.
  96. func (l *Logger) Error(args ...interface{}) {
  97. l.logger.Error(fmt.Sprint(args...))
  98. }
  99. // Errorln logs a message at level Error on the compatibleLogger.
  100. func (l *Logger) Errorln(args ...interface{}) {
  101. l.logger.Error(fmt.Sprint(args...))
  102. }
  103. // Errorf logs a message at level Error on the compatibleLogger.
  104. func (l *Logger) Errorf(format string, args ...interface{}) {
  105. l.logger.Error(fmt.Sprintf(format, args...))
  106. }
  107. // Fatal logs a message at level Fatal on the compatibleLogger.
  108. func (l *Logger) Fatal(args ...interface{}) {
  109. l.logger.Fatal(fmt.Sprint(args...))
  110. }
  111. // Fatalln logs a message at level Fatal on the compatibleLogger.
  112. func (l *Logger) Fatalln(args ...interface{}) {
  113. l.logger.Fatal(fmt.Sprint(args...))
  114. }
  115. // Fatalf logs a message at level Fatal on the compatibleLogger.
  116. func (l *Logger) Fatalf(format string, args ...interface{}) {
  117. l.logger.Fatal(fmt.Sprintf(format, args...))
  118. }
  119. // Panic logs a message at level Painc on the compatibleLogger.
  120. func (l *Logger) Panic(args ...interface{}) {
  121. l.logger.Panic(fmt.Sprint(args...))
  122. }
  123. // Panicln logs a message at level Painc on the compatibleLogger.
  124. func (l *Logger) Panicln(args ...interface{}) {
  125. l.logger.Panic(fmt.Sprint(args...))
  126. }
  127. // Panicf logs a message at level Painc on the compatibleLogger.
  128. func (l *Logger) Panicf(format string, args ...interface{}) {
  129. l.logger.Panic(fmt.Sprintf(format, args...))
  130. }
  131. // WithError return a logger with an error field.
  132. func (l *Logger) WithError(err error) *logrus.Entry {
  133. return logrus.NewEntry(l.logrusLogger).WithError(err)
  134. }