From 8748299a9675dfb473e11ab507d12a9bf187e86e Mon Sep 17 00:00:00 2001 From: Ge Song Date: Wed, 12 Apr 2023 15:07:11 +0800 Subject: [PATCH] =?UTF-8?q?log/=E4=BF=AE=E6=94=B9=20Field;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- log/log.go | 11 +++++++++-- log/logsdk/entry.go | 10 +++++----- log/logsdk/field.go | 12 ++++++++++-- log/logsdk/logjson/processor.go | 2 +- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/log/log.go b/log/log.go index 4504345..8063a12 100644 --- a/log/log.go +++ b/log/log.go @@ -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...) } diff --git a/log/logsdk/entry.go b/log/logsdk/entry.go index c4c3caf..f331579 100644 --- a/log/logsdk/entry.go +++ b/log/logsdk/entry.go @@ -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 } diff --git a/log/logsdk/field.go b/log/logsdk/field.go index fe379ff..6aba454 100644 --- a/log/logsdk/field.go +++ b/log/logsdk/field.go @@ -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"` } diff --git a/log/logsdk/logjson/processor.go b/log/logsdk/logjson/processor.go index eede248..0837a4c 100644 --- a/log/logsdk/logjson/processor.go +++ b/log/logsdk/logjson/processor.go @@ -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"` }