Compare commits

...

3 Commits

Author SHA1 Message Date
yuguojian
f6a41a5e7c 日志参数转字符串 2025-07-14 14:51:05 +08:00
yuguojian
994ed9e639 更新日志 2025-07-11 00:20:34 +08:00
yuguojian
e2dfb3e5ad 日志参数和结果转对象 2025-07-10 10:18:37 +08:00
2 changed files with 85 additions and 20 deletions

View File

@@ -0,0 +1,75 @@
{
"settings": {
"index": {
"mapping": {
"source": {
"mode": "synthetic"
}
}
}
},
"mappings": {
"properties": {
"caller": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"created_at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis||epoch_second"
},
"level": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"log": {
"properties": {
"at": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis||epoch_second"
},
"log_type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"param": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"result": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
}
}

View File

@@ -6,7 +6,6 @@ import (
"github.com/dromara/carbon/v2"
"github.com/gogf/gf/v2/frame/g"
"go.uber.org/zap"
"reflect"
)
type LogType = string
@@ -98,18 +97,13 @@ func HandleLog(log *LogInfo) {
log.At = carbon.Now().ToDateTimeString()
log.Timestamp = carbon.Now().Timestamp()
// 处理 Param
// 处理 Param
if log.Param != nil {
log.Param = processValue(log.Param)
}
log.Param = processValue(log.Param)
// 处理 Result
if log.Result != nil {
if _, ok := log.Result.(error); ok {
log.ErrStr = fmt.Sprintf("%+v", log.Result)
log.Result = nil
} else {
log.Result = processValue(log.Result)
}
if _, ok := log.Result.(error); ok {
log.ErrStr = fmt.Sprintf("%+v", log.Result)
log.Result = struct{}{}
} else {
log.Result = processValue(log.Result)
}
}
@@ -164,13 +158,9 @@ func (l *LogInfo) ZapCommonHandelResult(logger *zap.Logger, result interface{},
}
func processValue(value interface{}) interface{} {
kind := reflect.TypeOf(value).Kind()
switch kind {
case reflect.Map, reflect.Slice, reflect.Array, reflect.Struct:
// 如果是复杂类型,保持原样
return value
default:
// 对于其他类型,转换为字符串
return param{Type: kind.String(), Value: fmt.Sprintf("%v", value)}
if value == nil {
return ""
}
// 对于其他类型,转换为字符串
return fmt.Sprintf("%+v", value)
}