log/修改 Field;

This commit is contained in:
2023-04-12 15:07:11 +08:00
parent d12ff23ff9
commit 8748299a96
4 changed files with 25 additions and 10 deletions

View File

@@ -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
}

View File

@@ -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"`
}

View File

@@ -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"`
}