Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
33e119864e | ||
![]() |
f6a41a5e7c | ||
![]() |
994ed9e639 | ||
![]() |
e2dfb3e5ad |
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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
33
log/log.go
33
log/log.go
@@ -3,10 +3,8 @@ package log
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"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
|
||||||
@@ -49,9 +47,6 @@ type LogInfo struct {
|
|||||||
Title string `json:"title"` // 必填,标题,做什么事情
|
Title string `json:"title"` // 必填,标题,做什么事情
|
||||||
Param interface{} `json:"param,omitempty"` // 参数,请求参数,传指针
|
Param interface{} `json:"param,omitempty"` // 参数,请求参数,传指针
|
||||||
Result interface{} `json:"result,omitempty"` // 结果,返回值,传指针,如果是报错,传err
|
Result interface{} `json:"result,omitempty"` // 结果,返回值,传指针,如果是报错,传err
|
||||||
ErrStr string `json:"err_str,omitempty"` // 错误信息,不用传入,自动记录
|
|
||||||
At string `json:"at"` // 记录时间,不用传入,自动记录
|
|
||||||
Timestamp int64 `json:"timestamp"` // 时间戳,不用传入,自动记录
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type param struct {
|
type param struct {
|
||||||
@@ -95,22 +90,10 @@ func ZapFatal(logger *zap.Logger, log *LogInfo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func HandleLog(log *LogInfo) {
|
func HandleLog(log *LogInfo) {
|
||||||
log.At = carbon.Now().ToDateTimeString()
|
|
||||||
log.Timestamp = carbon.Now().Timestamp()
|
|
||||||
// 处理 Param
|
// 处理 Param
|
||||||
// 处理 Param
|
log.Param = processValue(log.Param)
|
||||||
if log.Param != nil {
|
|
||||||
log.Param = processValue(log.Param)
|
|
||||||
}
|
|
||||||
// 处理 Result
|
// 处理 Result
|
||||||
if log.Result != nil {
|
log.Result = processValue(log.Result)
|
||||||
if _, ok := log.Result.(error); ok {
|
|
||||||
log.ErrStr = fmt.Sprintf("%+v", log.Result)
|
|
||||||
log.Result = nil
|
|
||||||
} else {
|
|
||||||
log.Result = processValue(log.Result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GFInfo(ctx context.Context, log *LogInfo) {
|
func GFInfo(ctx context.Context, log *LogInfo) {
|
||||||
@@ -164,13 +147,9 @@ func (l *LogInfo) ZapCommonHandelResult(logger *zap.Logger, result interface{},
|
|||||||
}
|
}
|
||||||
|
|
||||||
func processValue(value interface{}) interface{} {
|
func processValue(value interface{}) interface{} {
|
||||||
kind := reflect.TypeOf(value).Kind()
|
if value == nil {
|
||||||
switch kind {
|
return ""
|
||||||
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