feat: log helper support sprint option (#2616)

* feat:add sprint option

* fix: with helper need sprint

---------

Co-authored-by: zhoushijie <shijie.zhou@yunzhanghu.com>
pull/2731/head
woniu317 2 years ago committed by GitHub
parent f03f5f8988
commit d0847cd462
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 52
      log/helper.go

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

Loading…
Cancel
Save