fix: helper output unintentional adhesion&contrib no supposed kv len boundary

pull/2839/head
songzhibin97 2 years ago
parent 96480c11ee
commit 3e534dc2c3
  1. 5
      contrib/log/aliyun/aliyun.go
  2. 2
      contrib/log/fluent/fluent.go
  3. 4
      contrib/log/logrus/logrus.go
  4. 3
      contrib/log/tencent/tencent.go
  5. 52
      log/helper.go

@ -82,8 +82,11 @@ func (a *aliyunLog) Close() error {
} }
func (a *aliyunLog) Log(level log.Level, keyvals ...interface{}) error { func (a *aliyunLog) Log(level log.Level, keyvals ...interface{}) error {
contents := make([]*sls.LogContent, 0, len(keyvals)/2+1)
contents := make([]*sls.LogContent, 0, len(keyvals)/2+1)
if (len(keyvals) & 1) == 1 {
keyvals = append(keyvals, "KEYVALS UNPAIRED")
}
contents = append(contents, &sls.LogContent{ contents = append(contents, &sls.LogContent{
Key: newString(level.Key()), Key: newString(level.Key()),
Value: newString(level.String()), Value: newString(level.String()),

@ -155,7 +155,7 @@ func (l *Logger) Log(level log.Level, keyvals ...interface{}) error {
if len(keyvals) == 0 { if len(keyvals) == 0 {
return nil return nil
} }
if len(keyvals)%2 != 0 { if (len(keyvals) & 1) == 1 {
keyvals = append(keyvals, "KEYVALS UNPAIRED") keyvals = append(keyvals, "KEYVALS UNPAIRED")
} }

@ -47,8 +47,8 @@ func (l *Logger) Log(level log.Level, keyvals ...interface{}) (err error) {
if len(keyvals) == 0 { if len(keyvals) == 0 {
return nil return nil
} }
if len(keyvals)%2 != 0 { if (len(keyvals) & 1) == 1 {
keyvals = append(keyvals, "") keyvals = append(keyvals, "KEYVALS UNPAIRED")
} }
for i := 0; i < len(keyvals); i += 2 { for i := 0; i < len(keyvals); i += 2 {
key, ok := keyvals[i].(string) key, ok := keyvals[i].(string)

@ -71,6 +71,9 @@ func (log *tencentLog) Close() error {
func (log *tencentLog) Log(level log.Level, keyvals ...interface{}) error { func (log *tencentLog) Log(level log.Level, keyvals ...interface{}) error {
contents := make([]*cls.Log_Content, 0, len(keyvals)/2+1) contents := make([]*cls.Log_Content, 0, len(keyvals)/2+1)
if (len(keyvals) & 1) == 1 {
keyvals = append(keyvals, "KEYVALS UNPAIRED")
}
contents = append(contents, &cls.Log_Content{ contents = append(contents, &cls.Log_Content{
Key: newString(level.Key()), Key: newString(level.Key()),

@ -14,10 +14,8 @@ 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.
@ -27,27 +25,11 @@ 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)
@ -59,10 +41,8 @@ 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,
} }
} }
@ -73,12 +53,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, h.sprint(a...)) _ = h.logger.Log(LevelDebug, append([]interface{}{h.msgKey}, 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, h.sprintf(format, a...)) _ = h.logger.Log(LevelDebug, h.msgKey, fmt.Sprintf(format, a...))
} }
// Debugw logs a message at debug level. // Debugw logs a message at debug level.
@ -88,12 +68,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, h.sprint(a...)) _ = h.logger.Log(LevelInfo, append([]interface{}{h.msgKey}, 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, h.sprintf(format, a...)) _ = h.logger.Log(LevelInfo, h.msgKey, fmt.Sprintf(format, a...))
} }
// Infow logs a message at info level. // Infow logs a message at info level.
@ -103,12 +83,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, h.sprint(a...)) _ = h.logger.Log(LevelWarn, append([]interface{}{h.msgKey}, 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, h.sprintf(format, a...)) _ = h.logger.Log(LevelWarn, h.msgKey, fmt.Sprintf(format, a...))
} }
// Warnw logs a message at warnf level. // Warnw logs a message at warnf level.
@ -118,12 +98,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, h.sprint(a...)) _ = h.logger.Log(LevelError, append([]interface{}{h.msgKey}, 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, h.sprintf(format, a...)) _ = h.logger.Log(LevelError, h.msgKey, fmt.Sprintf(format, a...))
} }
// Errorw logs a message at error level. // Errorw logs a message at error level.
@ -133,13 +113,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, h.sprint(a...)) _ = h.logger.Log(LevelFatal, append([]interface{}{h.msgKey}, 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, h.sprintf(format, a...)) _ = h.logger.Log(LevelFatal, h.msgKey, fmt.Sprintf(format, a...))
os.Exit(1) os.Exit(1)
} }

Loading…
Cancel
Save