1
0

helper.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package logger
  2. import (
  3. "os"
  4. )
  5. type Helper struct {
  6. Logger
  7. fields map[string]interface{}
  8. }
  9. func NewHelper(log Logger) *Helper {
  10. return &Helper{Logger: log}
  11. }
  12. func (h *Helper) Info(args ...interface{}) {
  13. if !h.Logger.Options().Level.Enabled(InfoLevel) {
  14. return
  15. }
  16. h.Logger.Fields(h.fields).Log(InfoLevel, args...)
  17. }
  18. func (h *Helper) Infof(template string, args ...interface{}) {
  19. if !h.Logger.Options().Level.Enabled(InfoLevel) {
  20. return
  21. }
  22. h.Logger.Fields(h.fields).Logf(InfoLevel, template, args...)
  23. }
  24. func (h *Helper) Trace(args ...interface{}) {
  25. if !h.Logger.Options().Level.Enabled(TraceLevel) {
  26. return
  27. }
  28. h.Logger.Fields(h.fields).Log(TraceLevel, args...)
  29. }
  30. func (h *Helper) Tracef(template string, args ...interface{}) {
  31. if !h.Logger.Options().Level.Enabled(TraceLevel) {
  32. return
  33. }
  34. h.Logger.Fields(h.fields).Logf(TraceLevel, template, args...)
  35. }
  36. func (h *Helper) Debug(args ...interface{}) {
  37. if !h.Logger.Options().Level.Enabled(DebugLevel) {
  38. return
  39. }
  40. h.Logger.Fields(h.fields).Log(DebugLevel, args...)
  41. }
  42. func (h *Helper) Debugf(template string, args ...interface{}) {
  43. if !h.Logger.Options().Level.Enabled(DebugLevel) {
  44. return
  45. }
  46. h.Logger.Fields(h.fields).Logf(DebugLevel, template, args...)
  47. }
  48. func (h *Helper) Warn(args ...interface{}) {
  49. if !h.Logger.Options().Level.Enabled(WarnLevel) {
  50. return
  51. }
  52. h.Logger.Fields(h.fields).Log(WarnLevel, args...)
  53. }
  54. func (h *Helper) Warnf(template string, args ...interface{}) {
  55. if !h.Logger.Options().Level.Enabled(WarnLevel) {
  56. return
  57. }
  58. h.Logger.Fields(h.fields).Logf(WarnLevel, template, args...)
  59. }
  60. func (h *Helper) Error(args ...interface{}) {
  61. if !h.Logger.Options().Level.Enabled(ErrorLevel) {
  62. return
  63. }
  64. h.Logger.Fields(h.fields).Log(ErrorLevel, args...)
  65. }
  66. func (h *Helper) Errorf(template string, args ...interface{}) {
  67. if !h.Logger.Options().Level.Enabled(ErrorLevel) {
  68. return
  69. }
  70. h.Logger.Fields(h.fields).Logf(ErrorLevel, template, args...)
  71. }
  72. func (h *Helper) Fatal(args ...interface{}) {
  73. if !h.Logger.Options().Level.Enabled(FatalLevel) {
  74. return
  75. }
  76. h.Logger.Fields(h.fields).Log(FatalLevel, args...)
  77. os.Exit(1)
  78. }
  79. func (h *Helper) Fatalf(template string, args ...interface{}) {
  80. if !h.Logger.Options().Level.Enabled(FatalLevel) {
  81. return
  82. }
  83. h.Logger.Fields(h.fields).Logf(FatalLevel, template, args...)
  84. os.Exit(1)
  85. }
  86. func (h *Helper) WithError(err error) *Helper {
  87. fields := copyFields(h.fields)
  88. fields["error"] = err
  89. return &Helper{Logger: h.Logger, fields: fields}
  90. }
  91. func (h *Helper) WithFields(fields map[string]interface{}) *Helper {
  92. nfields := copyFields(fields)
  93. for k, v := range h.fields {
  94. nfields[k] = v
  95. }
  96. return &Helper{Logger: h.Logger, fields: nfields}
  97. }