|
|
|
@ -6,16 +6,31 @@ import ( |
|
|
|
|
"os" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// Option is Helper option.
|
|
|
|
|
type Option func(*Helper) |
|
|
|
|
|
|
|
|
|
// Helper is a logger helper.
|
|
|
|
|
type Helper struct { |
|
|
|
|
logger Logger |
|
|
|
|
msgKey string |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func WithMessageKey(k string) Option { |
|
|
|
|
return func(opts *Helper) { |
|
|
|
|
opts.msgKey = k |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// NewHelper new a logger helper.
|
|
|
|
|
func NewHelper(logger Logger) *Helper { |
|
|
|
|
return &Helper{ |
|
|
|
|
func NewHelper(logger Logger, opts ...Option) *Helper { |
|
|
|
|
options := &Helper{ |
|
|
|
|
msgKey: "msg", // default message key
|
|
|
|
|
logger: logger, |
|
|
|
|
} |
|
|
|
|
for _, o := range opts { |
|
|
|
|
o(options) |
|
|
|
|
} |
|
|
|
|
return options |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// WithContext returns a shallow copy of h with its context changed
|
|
|
|
@ -33,12 +48,12 @@ func (h *Helper) Log(level Level, keyvals ...interface{}) { |
|
|
|
|
|
|
|
|
|
// Debug logs a message at debug level.
|
|
|
|
|
func (h *Helper) Debug(a ...interface{}) { |
|
|
|
|
h.Log(LevelDebug, "msg", fmt.Sprint(a...)) |
|
|
|
|
h.Log(LevelDebug, h.msgKey, fmt.Sprint(a...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Debugf logs a message at debug level.
|
|
|
|
|
func (h *Helper) Debugf(format string, a ...interface{}) { |
|
|
|
|
h.Log(LevelDebug, "msg", fmt.Sprintf(format, a...)) |
|
|
|
|
h.Log(LevelDebug, h.msgKey, fmt.Sprintf(format, a...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Debugw logs a message at debug level.
|
|
|
|
@ -48,12 +63,12 @@ func (h *Helper) Debugw(keyvals ...interface{}) { |
|
|
|
|
|
|
|
|
|
// Info logs a message at info level.
|
|
|
|
|
func (h *Helper) Info(a ...interface{}) { |
|
|
|
|
h.Log(LevelInfo, "msg", fmt.Sprint(a...)) |
|
|
|
|
h.Log(LevelInfo, h.msgKey, fmt.Sprint(a...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Infof logs a message at info level.
|
|
|
|
|
func (h *Helper) Infof(format string, a ...interface{}) { |
|
|
|
|
h.Log(LevelInfo, "msg", fmt.Sprintf(format, a...)) |
|
|
|
|
h.Log(LevelInfo, h.msgKey, fmt.Sprintf(format, a...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Infow logs a message at info level.
|
|
|
|
@ -63,12 +78,12 @@ func (h *Helper) Infow(keyvals ...interface{}) { |
|
|
|
|
|
|
|
|
|
// Warn logs a message at warn level.
|
|
|
|
|
func (h *Helper) Warn(a ...interface{}) { |
|
|
|
|
h.Log(LevelWarn, "msg", fmt.Sprint(a...)) |
|
|
|
|
h.Log(LevelWarn, h.msgKey, fmt.Sprint(a...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Warnf logs a message at warnf level.
|
|
|
|
|
func (h *Helper) Warnf(format string, a ...interface{}) { |
|
|
|
|
h.Log(LevelWarn, "msg", fmt.Sprintf(format, a...)) |
|
|
|
|
h.Log(LevelWarn, h.msgKey, fmt.Sprintf(format, a...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Warnw logs a message at warnf level.
|
|
|
|
@ -78,12 +93,12 @@ func (h *Helper) Warnw(keyvals ...interface{}) { |
|
|
|
|
|
|
|
|
|
// Error logs a message at error level.
|
|
|
|
|
func (h *Helper) Error(a ...interface{}) { |
|
|
|
|
h.Log(LevelError, "msg", fmt.Sprint(a...)) |
|
|
|
|
h.Log(LevelError, h.msgKey, fmt.Sprint(a...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Errorf logs a message at error level.
|
|
|
|
|
func (h *Helper) Errorf(format string, a ...interface{}) { |
|
|
|
|
h.Log(LevelError, "msg", fmt.Sprintf(format, a...)) |
|
|
|
|
h.Log(LevelError, h.msgKey, fmt.Sprintf(format, a...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Errorw logs a message at error level.
|
|
|
|
@ -93,13 +108,13 @@ func (h *Helper) Errorw(keyvals ...interface{}) { |
|
|
|
|
|
|
|
|
|
// Fatal logs a message at fatal level.
|
|
|
|
|
func (h *Helper) Fatal(a ...interface{}) { |
|
|
|
|
h.Log(LevelFatal, "msg", fmt.Sprint(a...)) |
|
|
|
|
h.Log(LevelFatal, h.msgKey, fmt.Sprint(a...)) |
|
|
|
|
os.Exit(1) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Fatalf logs a message at fatal level.
|
|
|
|
|
func (h *Helper) Fatalf(format string, a ...interface{}) { |
|
|
|
|
h.Log(LevelFatal, "msg", fmt.Sprintf(format, a...)) |
|
|
|
|
h.Log(LevelFatal, h.msgKey, fmt.Sprintf(format, a...)) |
|
|
|
|
os.Exit(1) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|