You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/log/helper.go

151 lines
3.7 KiB

4 years ago
package log
import (
"context"
4 years ago
"fmt"
"os"
4 years ago
)
// DefaultMessageKey default message key.
var DefaultMessageKey = "msg"
// Option is Helper option.
type Option func(*Helper)
4 years ago
// Helper is a logger helper.
type Helper struct {
logger Logger
msgKey string
sprint func(...interface{}) string
sprintf func(format string, a ...interface{}) string
}
// WithMessageKey with message key.
func WithMessageKey(k string) Option {
return func(opts *Helper) {
opts.msgKey = k
}
4 years ago
}
// 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
}
}
4 years ago
// NewHelper new a logger helper.
func NewHelper(logger Logger, opts ...Option) *Helper {
options := &Helper{
msgKey: DefaultMessageKey, // default message key
logger: logger,
sprint: fmt.Sprint,
sprintf: fmt.Sprintf,
4 years ago
}
for _, o := range opts {
o(options)
}
return options
4 years ago
}
// WithContext returns a shallow copy of h with its context changed
// 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),
sprint: h.sprint,
sprintf: h.sprintf,
}
}
4 years ago
// Log Print log by level and keyvals.
func (h *Helper) Log(level Level, keyvals ...interface{}) {
_ = h.logger.Log(level, keyvals...)
}
4 years ago
// Debug logs a message at debug level.
func (h *Helper) Debug(a ...interface{}) {
_ = h.logger.Log(LevelDebug, h.msgKey, h.sprint(a...))
4 years ago
}
// Debugf logs a message at debug level.
func (h *Helper) Debugf(format string, a ...interface{}) {
_ = h.logger.Log(LevelDebug, h.msgKey, h.sprintf(format, a...))
4 years ago
}
// Debugw logs a message at debug level.
func (h *Helper) Debugw(keyvals ...interface{}) {
_ = h.logger.Log(LevelDebug, keyvals...)
4 years ago
}
// Info logs a message at info level.
func (h *Helper) Info(a ...interface{}) {
_ = h.logger.Log(LevelInfo, h.msgKey, h.sprint(a...))
4 years ago
}
// Infof logs a message at info level.
func (h *Helper) Infof(format string, a ...interface{}) {
_ = h.logger.Log(LevelInfo, h.msgKey, h.sprintf(format, a...))
4 years ago
}
// Infow logs a message at info level.
func (h *Helper) Infow(keyvals ...interface{}) {
_ = h.logger.Log(LevelInfo, keyvals...)
4 years ago
}
// Warn logs a message at warn level.
func (h *Helper) Warn(a ...interface{}) {
_ = h.logger.Log(LevelWarn, h.msgKey, h.sprint(a...))
4 years ago
}
// Warnf logs a message at warnf level.
func (h *Helper) Warnf(format string, a ...interface{}) {
_ = h.logger.Log(LevelWarn, h.msgKey, h.sprintf(format, a...))
4 years ago
}
// Warnw logs a message at warnf level.
func (h *Helper) Warnw(keyvals ...interface{}) {
_ = h.logger.Log(LevelWarn, keyvals...)
4 years ago
}
// Error logs a message at error level.
func (h *Helper) Error(a ...interface{}) {
_ = h.logger.Log(LevelError, h.msgKey, h.sprint(a...))
4 years ago
}
// Errorf logs a message at error level.
func (h *Helper) Errorf(format string, a ...interface{}) {
_ = h.logger.Log(LevelError, h.msgKey, h.sprintf(format, a...))
4 years ago
}
// Errorw logs a message at error level.
func (h *Helper) Errorw(keyvals ...interface{}) {
_ = h.logger.Log(LevelError, keyvals...)
4 years ago
}
// Fatal logs a message at fatal level.
func (h *Helper) Fatal(a ...interface{}) {
_ = 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, h.sprintf(format, a...))
os.Exit(1)
}
// Fatalw logs a message at fatal level.
func (h *Helper) Fatalw(keyvals ...interface{}) {
_ = h.logger.Log(LevelFatal, keyvals...)
os.Exit(1)
}