log/修改 Field;

develop
Ge Song 2 years ago
parent d12ff23ff9
commit 8748299a96

@ -16,7 +16,6 @@ import (
type (
Level = logsdk.Level
Field = logsdk.Field
)
const (
@ -29,6 +28,14 @@ const (
LevelTrace = logsdk.LevelTrace
)
// Field 返回键值对
func Field(key string, value any) logsdk.KV {
return logsdk.KV{
Value: value,
Key: key,
}
}
// AddCallerSkip 增加调用 [runtime.Callers] 时的 skip 参数,
// 当通过装饰器等方式封装 Entry 导致增加调用 Entry 方法的深度时使用 AddCallerSkip 调整 skip,
// 直接在需要日志的地方调用 Entry 的方法时不需要 AddCallerSkip.
@ -42,7 +49,7 @@ func WithField(key string, value any) logsdk.Entry {
}
// WithFields 增加键值对
func WithFields(fields ...logsdk.Field) logsdk.Entry {
func WithFields(fields ...logsdk.KV) logsdk.Entry {
return globalLogger.WithFields(fields...)
}

@ -12,7 +12,7 @@ import (
type Entry struct {
logger *Logger
time time.Time
fields []Field
fields []KV
callerSkip int
reportCaller bool
isReportCallerSet bool
@ -39,13 +39,13 @@ func (entry Entry) AddCallerSkip(n int) Entry {
// WithField 增加1组键值对
func (entry Entry) WithField(key string, value any) Entry {
return entry.WithFields(Field{Key: key, Value: value})
return entry.WithFields(KV{Key: key, Value: value})
}
// WithFields 增加键值对
func (entry Entry) WithFields(fields ...Field) Entry {
func (entry Entry) WithFields(fields ...KV) Entry {
newEntry := entry.copy()
newEntry.fields = make([]Field, len(entry.fields)+len(fields))
newEntry.fields = make([]KV, len(entry.fields)+len(fields))
copy(newEntry.fields, entry.fields)
copy(newEntry.fields, fields)
return newEntry
@ -215,6 +215,6 @@ type ReadonlyEntry struct {
Stack []runtimehelper.Frame
Time time.Time
Message string
Fields []Field
Fields []KV
Level Level
}

@ -1,7 +1,15 @@
package logsdk
// Field 是日志记录中的键值对
type Field struct {
// Field 返回键值对
func Field(key string, value any) KV {
return KV{
Value: value,
Key: key,
}
}
// KV 是日志记录中的键值对
type KV struct {
Value any `json:"v"`
Key string `json:"k"`
}

@ -84,6 +84,6 @@ type Entry struct {
Time string `json:"time,omitempty"`
Caller *runtimehelper.Frame `json:"caller,omitempty"`
Stack []runtimehelper.Frame `json:"stack,omitempty"`
Fields []logsdk.Field `json:"fields,omitempty"`
Fields []logsdk.KV `json:"fields,omitempty"`
Level logsdk.Level `json:"level"`
}

Loading…
Cancel
Save