update slog/logger #351
@ -14,6 +14,8 @@ var (
|
|||||||
DefaultLogger Logger = NewLogger()
|
DefaultLogger Logger = NewLogger()
|
||||||
// DefaultLevel used by logger
|
// DefaultLevel used by logger
|
||||||
DefaultLevel = InfoLevel
|
DefaultLevel = InfoLevel
|
||||||
|
// defaultCallerSkipCount used by logger
|
||||||
|
defaultCallerSkipCount = 3
|
||||||
)
|
)
|
||||||
|
|
||||||
// Logger is a generic logging interface
|
// Logger is a generic logging interface
|
||||||
|
@ -23,6 +23,8 @@ type Options struct {
|
|||||||
Name string
|
Name string
|
||||||
// Fields holds additional metadata
|
// Fields holds additional metadata
|
||||||
Fields []interface{}
|
Fields []interface{}
|
||||||
|
// callerSkipCount number of frmaes to skip
|
||||||
|
CallerSkipCount int
|
||||||
// ContextAttrFuncs contains funcs that executed before log func on context
|
// ContextAttrFuncs contains funcs that executed before log func on context
|
||||||
ContextAttrFuncs []ContextAttrFunc
|
ContextAttrFuncs []ContextAttrFunc
|
||||||
// TimeKey is the key used for the time of the log call
|
// TimeKey is the key used for the time of the log call
|
||||||
@ -52,6 +54,7 @@ type Options struct {
|
|||||||
// NewOptions creates new options struct
|
// NewOptions creates new options struct
|
||||||
func NewOptions(opts ...Option) Options {
|
func NewOptions(opts ...Option) Options {
|
||||||
options := Options{
|
options := Options{
|
||||||
|
CallerSkipCount: defaultCallerSkipCount,
|
||||||
Level: DefaultLevel,
|
Level: DefaultLevel,
|
||||||
Fields: make([]interface{}, 0, 6),
|
Fields: make([]interface{}, 0, 6),
|
||||||
Out: os.Stderr,
|
Out: os.Stderr,
|
||||||
@ -184,3 +187,12 @@ func WithMicroKeys() Option {
|
|||||||
o.ErrorKey = "error"
|
o.ErrorKey = "error"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithAddCallerSkipCount add skip count for copy logger
|
||||||
|
func WithAddCallerSkipCount(n int) Option {
|
||||||
|
return func(o *Options) {
|
||||||
|
if n > 0 {
|
||||||
|
o.CallerSkipCount += n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -16,8 +16,6 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
badKey = "!BADKEY"
|
badKey = "!BADKEY"
|
||||||
// DefaultCallerSkipCount used by logger
|
|
||||||
defaultCallerSkipCount = 3
|
|
||||||
)
|
)
|
||||||
const emptyMSg = "!EMPTYMSG"
|
const emptyMSg = "!EMPTYMSG"
|
||||||
|
|
||||||
@ -227,7 +225,7 @@ func (s *slogLogger) printLog(ctx context.Context, lvl logger.Level, msg string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
var pcs [1]uintptr
|
var pcs [1]uintptr
|
||||||
runtime.Callers(defaultCallerSkipCount, pcs[:]) // skip [Callers, printLog, LogLvlMethod]
|
runtime.Callers(s.opts.CallerSkipCount, pcs[:]) // skip [Callers, printLog, LogLvlMethod]
|
||||||
r := slog.NewRecord(s.opts.TimeFunc(), loggerToSlogLevel(lvl), msg, pcs[0])
|
r := slog.NewRecord(s.opts.TimeFunc(), loggerToSlogLevel(lvl), msg, pcs[0])
|
||||||
r.Add(attrs...)
|
r.Add(attrs...)
|
||||||
_ = s.handler.Handle(ctx, r)
|
_ = s.handler.Handle(ctx, r)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user