|
|
|
@ -14,8 +14,10 @@ type Option func(*Helper) |
|
|
|
|
|
|
|
|
|
// Helper is a logger helper.
|
|
|
|
|
type Helper struct { |
|
|
|
|
logger Logger |
|
|
|
|
msgKey string |
|
|
|
|
logger Logger |
|
|
|
|
msgKey string |
|
|
|
|
sprint func(...interface{}) string |
|
|
|
|
sprintf func(format string, a ...interface{}) string |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// WithMessageKey with message key.
|
|
|
|
@ -25,11 +27,27 @@ func WithMessageKey(k string) Option { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// WithSprint with sprint
|
|
|
|
|
func WithSprint(sprint func(...interface{}) string) Option { |
|
|
|
|
return func(opts *Helper) { |
|
|
|
|
opts.sprint = sprint |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// WithSprintf with sprintf
|
|
|
|
|
func WithSprintf(sprintf func(format string, a ...interface{}) string) Option { |
|
|
|
|
return func(opts *Helper) { |
|
|
|
|
opts.sprintf = sprintf |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// NewHelper new a logger helper.
|
|
|
|
|
func NewHelper(logger Logger, opts ...Option) *Helper { |
|
|
|
|
options := &Helper{ |
|
|
|
|
msgKey: DefaultMessageKey, // default message key
|
|
|
|
|
logger: logger, |
|
|
|
|
msgKey: DefaultMessageKey, // default message key
|
|
|
|
|
logger: logger, |
|
|
|
|
sprint: fmt.Sprint, |
|
|
|
|
sprintf: fmt.Sprintf, |
|
|
|
|
} |
|
|
|
|
for _, o := range opts { |
|
|
|
|
o(options) |
|
|
|
@ -41,8 +59,10 @@ func NewHelper(logger Logger, opts ...Option) *Helper { |
|
|
|
|
// to ctx. The provided ctx must be non-nil.
|
|
|
|
|
func (h *Helper) WithContext(ctx context.Context) *Helper { |
|
|
|
|
return &Helper{ |
|
|
|
|
msgKey: h.msgKey, |
|
|
|
|
logger: WithContext(ctx, h.logger), |
|
|
|
|
msgKey: h.msgKey, |
|
|
|
|
logger: WithContext(ctx, h.logger), |
|
|
|
|
sprint: h.sprint, |
|
|
|
|
sprintf: h.sprintf, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -53,12 +73,12 @@ func (h *Helper) Log(level Level, keyvals ...interface{}) { |
|
|
|
|
|
|
|
|
|
// Debug logs a message at debug level.
|
|
|
|
|
func (h *Helper) Debug(a ...interface{}) { |
|
|
|
|
_ = h.logger.Log(LevelDebug, h.msgKey, fmt.Sprint(a...)) |
|
|
|
|
_ = h.logger.Log(LevelDebug, h.msgKey, h.sprint(a...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Debugf logs a message at debug level.
|
|
|
|
|
func (h *Helper) Debugf(format string, a ...interface{}) { |
|
|
|
|
_ = h.logger.Log(LevelDebug, h.msgKey, fmt.Sprintf(format, a...)) |
|
|
|
|
_ = h.logger.Log(LevelDebug, h.msgKey, h.sprintf(format, a...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Debugw logs a message at debug level.
|
|
|
|
@ -68,12 +88,12 @@ func (h *Helper) Debugw(keyvals ...interface{}) { |
|
|
|
|
|
|
|
|
|
// Info logs a message at info level.
|
|
|
|
|
func (h *Helper) Info(a ...interface{}) { |
|
|
|
|
_ = h.logger.Log(LevelInfo, h.msgKey, fmt.Sprint(a...)) |
|
|
|
|
_ = h.logger.Log(LevelInfo, h.msgKey, h.sprint(a...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Infof logs a message at info level.
|
|
|
|
|
func (h *Helper) Infof(format string, a ...interface{}) { |
|
|
|
|
_ = h.logger.Log(LevelInfo, h.msgKey, fmt.Sprintf(format, a...)) |
|
|
|
|
_ = h.logger.Log(LevelInfo, h.msgKey, h.sprintf(format, a...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Infow logs a message at info level.
|
|
|
|
@ -83,12 +103,12 @@ func (h *Helper) Infow(keyvals ...interface{}) { |
|
|
|
|
|
|
|
|
|
// Warn logs a message at warn level.
|
|
|
|
|
func (h *Helper) Warn(a ...interface{}) { |
|
|
|
|
_ = h.logger.Log(LevelWarn, h.msgKey, fmt.Sprint(a...)) |
|
|
|
|
_ = h.logger.Log(LevelWarn, h.msgKey, h.sprint(a...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Warnf logs a message at warnf level.
|
|
|
|
|
func (h *Helper) Warnf(format string, a ...interface{}) { |
|
|
|
|
_ = h.logger.Log(LevelWarn, h.msgKey, fmt.Sprintf(format, a...)) |
|
|
|
|
_ = h.logger.Log(LevelWarn, h.msgKey, h.sprintf(format, a...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Warnw logs a message at warnf level.
|
|
|
|
@ -98,12 +118,12 @@ func (h *Helper) Warnw(keyvals ...interface{}) { |
|
|
|
|
|
|
|
|
|
// Error logs a message at error level.
|
|
|
|
|
func (h *Helper) Error(a ...interface{}) { |
|
|
|
|
_ = h.logger.Log(LevelError, h.msgKey, fmt.Sprint(a...)) |
|
|
|
|
_ = h.logger.Log(LevelError, h.msgKey, h.sprint(a...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Errorf logs a message at error level.
|
|
|
|
|
func (h *Helper) Errorf(format string, a ...interface{}) { |
|
|
|
|
_ = h.logger.Log(LevelError, h.msgKey, fmt.Sprintf(format, a...)) |
|
|
|
|
_ = h.logger.Log(LevelError, h.msgKey, h.sprintf(format, a...)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Errorw logs a message at error level.
|
|
|
|
@ -113,13 +133,13 @@ func (h *Helper) Errorw(keyvals ...interface{}) { |
|
|
|
|
|
|
|
|
|
// Fatal logs a message at fatal level.
|
|
|
|
|
func (h *Helper) Fatal(a ...interface{}) { |
|
|
|
|
_ = h.logger.Log(LevelFatal, h.msgKey, fmt.Sprint(a...)) |
|
|
|
|
_ = h.logger.Log(LevelFatal, h.msgKey, h.sprint(a...)) |
|
|
|
|
os.Exit(1) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Fatalf logs a message at fatal level.
|
|
|
|
|
func (h *Helper) Fatalf(format string, a ...interface{}) { |
|
|
|
|
_ = h.logger.Log(LevelFatal, h.msgKey, fmt.Sprintf(format, a...)) |
|
|
|
|
_ = h.logger.Log(LevelFatal, h.msgKey, h.sprintf(format, a...)) |
|
|
|
|
os.Exit(1) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|