logrus.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package log
  2. import (
  3. "github.com/sirupsen/logrus"
  4. prefixed "github.com/x-cray/logrus-prefixed-formatter"
  5. )
  6. type LogrusLogger struct {
  7. log logrus.Ext1FieldLogger
  8. prefix string
  9. fields Fields
  10. }
  11. // Level type
  12. type Level uint32
  13. // These are the different logging levels. You can set the logging level to log
  14. // on your instance of logger, obtained with `logrus.New()`.
  15. const (
  16. // PanicLevel level, highest level of severity. Logs and then calls panic with the
  17. // message passed to Debug, Info, ...
  18. PanicLevel Level = iota
  19. // FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
  20. // logging level is set to Panic.
  21. FatalLevel
  22. // ErrorLevel level. Logs. Used for errors that should definitely be noted.
  23. // Commonly used for hooks to send errors to an error tracking service.
  24. ErrorLevel
  25. // WarnLevel level. Non-critical entries that deserve eyes.
  26. WarnLevel
  27. // InfoLevel level. General operational entries about what's going on inside the
  28. // application.
  29. InfoLevel
  30. // DebugLevel level. Usually only enabled when debugging. Very verbose logging.
  31. DebugLevel
  32. // TraceLevel level. Designates finer-grained informational events than the Debug.
  33. TraceLevel
  34. )
  35. func NewLogrusLogger(logrus logrus.Ext1FieldLogger, prefix string, fields Fields) *LogrusLogger {
  36. return &LogrusLogger{
  37. log: logrus,
  38. prefix: prefix,
  39. fields: fields,
  40. }
  41. }
  42. func NewDefaultLogrusLogger() *LogrusLogger {
  43. logger := logrus.New()
  44. logger.Formatter = &prefixed.TextFormatter{
  45. FullTimestamp: true,
  46. TimestampFormat: "2006-01-02 15:04:05.000",
  47. }
  48. return NewLogrusLogger(logger, "main", nil)
  49. }
  50. func (l *LogrusLogger) Print(args ...interface{}) {
  51. l.prepareEntry().Print(args...)
  52. }
  53. func (l *LogrusLogger) Printf(format string, args ...interface{}) {
  54. l.prepareEntry().Printf(format, args...)
  55. }
  56. func (l *LogrusLogger) Trace(args ...interface{}) {
  57. l.prepareEntry().Trace(args...)
  58. }
  59. func (l *LogrusLogger) Tracef(format string, args ...interface{}) {
  60. l.prepareEntry().Tracef(format, args...)
  61. }
  62. func (l *LogrusLogger) Debug(args ...interface{}) {
  63. l.prepareEntry().Debug(args...)
  64. }
  65. func (l *LogrusLogger) Debugf(format string, args ...interface{}) {
  66. l.prepareEntry().Debugf(format, args...)
  67. }
  68. func (l *LogrusLogger) Info(args ...interface{}) {
  69. l.prepareEntry().Info(args...)
  70. }
  71. func (l *LogrusLogger) Infof(format string, args ...interface{}) {
  72. l.prepareEntry().Infof(format, args...)
  73. }
  74. func (l *LogrusLogger) Warn(args ...interface{}) {
  75. l.prepareEntry().Warn(args...)
  76. }
  77. func (l *LogrusLogger) Warnf(format string, args ...interface{}) {
  78. l.prepareEntry().Warnf(format, args...)
  79. }
  80. func (l *LogrusLogger) Error(args ...interface{}) {
  81. l.prepareEntry().Error(args...)
  82. }
  83. func (l *LogrusLogger) Errorf(format string, args ...interface{}) {
  84. l.prepareEntry().Errorf(format, args...)
  85. }
  86. func (l *LogrusLogger) Fatal(args ...interface{}) {
  87. l.prepareEntry().Fatal(args...)
  88. }
  89. func (l *LogrusLogger) Fatalf(format string, args ...interface{}) {
  90. l.prepareEntry().Fatalf(format, args...)
  91. }
  92. func (l *LogrusLogger) Panic(args ...interface{}) {
  93. l.prepareEntry().Panic(args...)
  94. }
  95. func (l *LogrusLogger) Panicf(format string, args ...interface{}) {
  96. l.prepareEntry().Panicf(format, args...)
  97. }
  98. func (l *LogrusLogger) WithPrefix(prefix string) Logger {
  99. return NewLogrusLogger(l.log, prefix, l.Fields())
  100. }
  101. func (l *LogrusLogger) Prefix() string {
  102. return l.prefix
  103. }
  104. func (l *LogrusLogger) WithFields(fields Fields) Logger {
  105. return NewLogrusLogger(l.log, l.Prefix(), l.Fields().WithFields(fields))
  106. }
  107. func (l *LogrusLogger) Fields() Fields {
  108. return l.fields
  109. }
  110. func (l *LogrusLogger) prepareEntry() *logrus.Entry {
  111. return l.log.
  112. WithFields(logrus.Fields(l.Fields())).
  113. WithField("prefix", l.Prefix())
  114. }
  115. func (l *LogrusLogger) SetLevel(level Level) {
  116. if ll, ok := l.log.(*logrus.Logger); ok {
  117. ll.SetLevel(logrus.Level(level))
  118. }
  119. }