日志参数转字符串
This commit is contained in:
@@ -1,85 +0,0 @@
|
|||||||
package log
|
|
||||||
|
|
||||||
type T struct {
|
|
||||||
Mappings Mappings `json:"mappings"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Mappings struct {
|
|
||||||
Properties Properties `json:"properties"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Properties struct {
|
|
||||||
Caller Caller `json:"caller"`
|
|
||||||
CreatedAt CreatedAt `json:"created_at"`
|
|
||||||
Level Level `json:"level"`
|
|
||||||
Log Log `json:"log"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Caller struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
Fields struct {
|
|
||||||
Keyword struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
IgnoreAbove int `json:"ignore_above"`
|
|
||||||
} `json:"keyword"`
|
|
||||||
} `json:"fields"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type CreatedAt struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
Fields struct {
|
|
||||||
Keyword struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
IgnoreAbove int `json:"ignore_above"`
|
|
||||||
} `json:"keyword"`
|
|
||||||
} `json:"fields"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Level struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
Fields struct {
|
|
||||||
Keyword struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
IgnoreAbove int `json:"ignore_above"`
|
|
||||||
} `json:"keyword"`
|
|
||||||
} `json:"fields"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Log struct {
|
|
||||||
Properties struct {
|
|
||||||
At struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
Fields struct {
|
|
||||||
Keyword struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
IgnoreAbove int `json:"ignore_above"`
|
|
||||||
} `json:"keyword"`
|
|
||||||
} `json:"fields"`
|
|
||||||
} `json:"at"`
|
|
||||||
LogType struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
Fields struct {
|
|
||||||
Keyword struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
IgnoreAbove int `json:"ignore_above"`
|
|
||||||
} `json:"keyword"`
|
|
||||||
} `json:"fields"`
|
|
||||||
} `json:"log_type"`
|
|
||||||
Param struct {
|
|
||||||
} `json:"param"`
|
|
||||||
Result struct {
|
|
||||||
} `json:"result"`
|
|
||||||
Timestamp struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
} `json:"timestamp"`
|
|
||||||
Title struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
Fields struct {
|
|
||||||
Keyword struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
IgnoreAbove int `json:"ignore_above"`
|
|
||||||
} `json:"keyword"`
|
|
||||||
} `json:"fields"`
|
|
||||||
} `json:"title"`
|
|
||||||
} `json:"properties"`
|
|
||||||
}
|
|
75
log/elasticsearch_map.json
Normal file
75
log/elasticsearch_map.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
15
log/log.go
15
log/log.go
@@ -6,7 +6,6 @@ import (
|
|||||||
"github.com/dromara/carbon/v2"
|
"github.com/dromara/carbon/v2"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"reflect"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type LogType = string
|
type LogType = string
|
||||||
@@ -98,7 +97,6 @@ func HandleLog(log *LogInfo) {
|
|||||||
log.At = carbon.Now().ToDateTimeString()
|
log.At = carbon.Now().ToDateTimeString()
|
||||||
log.Timestamp = carbon.Now().Timestamp()
|
log.Timestamp = carbon.Now().Timestamp()
|
||||||
// 处理 Param
|
// 处理 Param
|
||||||
// 处理 Param
|
|
||||||
log.Param = processValue(log.Param)
|
log.Param = processValue(log.Param)
|
||||||
// 处理 Result
|
// 处理 Result
|
||||||
if _, ok := log.Result.(error); ok {
|
if _, ok := log.Result.(error); ok {
|
||||||
@@ -161,15 +159,8 @@ func (l *LogInfo) ZapCommonHandelResult(logger *zap.Logger, result interface{},
|
|||||||
|
|
||||||
func processValue(value interface{}) interface{} {
|
func processValue(value interface{}) interface{} {
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return struct{}{}
|
return ""
|
||||||
}
|
|
||||||
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)}
|
|
||||||
}
|
}
|
||||||
|
// 对于其他类型,转换为字符串
|
||||||
|
return fmt.Sprintf("%+v", value)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user