logger.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package logger
  2. import (
  3. "context"
  4. "io"
  5. "os"
  6. "github.com/rs/zerolog"
  7. )
  8. // Logger is a wrapper for a zerolog Logger
  9. type Logger struct {
  10. logger *zerolog.Logger
  11. }
  12. // New constructs a new Logger which logs via an os.File struct
  13. func New(isDebug bool, file *os.File) *Logger {
  14. logLevel := zerolog.InfoLevel
  15. if isDebug {
  16. logLevel = zerolog.DebugLevel
  17. }
  18. zerolog.SetGlobalLevel(logLevel)
  19. logger := zerolog.New(file).With().Timestamp().Logger()
  20. return &Logger{logger: &logger}
  21. }
  22. // NewConsole uses os.Stdout construct a Logger
  23. func NewConsole(isDebug bool) *Logger {
  24. return New(isDebug, os.Stdout)
  25. }
  26. // NewErrorConsole uses os.Stderr construct a Logger
  27. func NewErrorConsole(isDebug bool) *Logger {
  28. return New(isDebug, os.Stderr)
  29. }
  30. // Output duplicates the global logger and sets w as its output.
  31. func (l *Logger) Output(w io.Writer) zerolog.Logger {
  32. return l.logger.Output(w)
  33. }
  34. // With creates a child logger with the field added to its context.
  35. func (l *Logger) With() zerolog.Context {
  36. return l.logger.With()
  37. }
  38. // Level creates a child logger with the minimum accepted level set to level.
  39. func (l *Logger) Level(level zerolog.Level) zerolog.Logger {
  40. return l.logger.Level(level)
  41. }
  42. // Sample returns a logger with the s sampler.
  43. func (l *Logger) Sample(s zerolog.Sampler) zerolog.Logger {
  44. return l.logger.Sample(s)
  45. }
  46. // Hook returns a logger with the h Hook.
  47. func (l *Logger) Hook(h zerolog.Hook) zerolog.Logger {
  48. return l.logger.Hook(h)
  49. }
  50. // Debug starts a new message with debug level.
  51. //
  52. // You must call Msg on the returned event in order to send the event.
  53. func (l *Logger) Debug() *zerolog.Event {
  54. return l.logger.Debug()
  55. }
  56. // Info starts a new message with info level.
  57. //
  58. // You must call Msg on the returned event in order to send the event.
  59. func (l *Logger) Info() *zerolog.Event {
  60. return l.logger.Info()
  61. }
  62. // Warn starts a new message with warn level.
  63. //
  64. // You must call Msg on the returned event in order to send the event.
  65. func (l *Logger) Warn() *zerolog.Event {
  66. return l.logger.Warn()
  67. }
  68. // Error starts a new message with error level.
  69. //
  70. // You must call Msg on the returned event in order to send the event.
  71. func (l *Logger) Error() *zerolog.Event {
  72. return l.logger.Error()
  73. }
  74. // Fatal starts a new message with fatal level. The os.Exit(1) function
  75. // is called by the Msg method.
  76. //
  77. // You must call Msg on the returned event in order to send the event.
  78. func (l *Logger) Fatal() *zerolog.Event {
  79. return l.logger.Fatal()
  80. }
  81. // Panic starts a new message with panic level. The message is also sent
  82. // to the panic function.
  83. //
  84. // You must call Msg on the returned event in order to send the event.
  85. func (l *Logger) Panic() *zerolog.Event {
  86. return l.logger.Panic()
  87. }
  88. // WithLevel starts a new message with level.
  89. //
  90. // You must call Msg on the returned event in order to send the event.
  91. func (l *Logger) WithLevel(level zerolog.Level) *zerolog.Event {
  92. return l.logger.WithLevel(level)
  93. }
  94. // Log starts a new message with no level. Setting zerolog.GlobalLevel to
  95. // zerolog.Disabled will still disable events produced by this method.
  96. //
  97. // You must call Msg on the returned event in order to send the event.
  98. func (l *Logger) Log() *zerolog.Event {
  99. return l.logger.Log()
  100. }
  101. // Print sends a log event using debug level and no extra field.
  102. // Arguments are handled in the manner of fmt.Print.
  103. func (l *Logger) Print(v ...interface{}) {
  104. l.logger.Print(v...)
  105. }
  106. // Printf sends a log event using debug level and no extra field.
  107. // Arguments are handled in the manner of fmt.Printf.
  108. func (l *Logger) Printf(format string, v ...interface{}) {
  109. l.logger.Printf(format, v...)
  110. }
  111. // Ctx returns the Logger associated with the ctx. If no logger
  112. // is associated, a disabled logger is returned.
  113. func (l *Logger) Ctx(ctx context.Context) *Logger {
  114. return &Logger{logger: zerolog.Ctx(ctx)}
  115. }