#日志系统说明
1 、日志主要说明:谁在什么时间,在什么地方,做了什么事情,产生了什么影响,影响的变化因子。
2 、日志系统主要建设在 oplogger 基础上。将现有的 mysql 存储方式改成 elasticsearch 的存储方式。并且更新了现有字段,使其更优化
3 、以下为主要设计原理图:
说明:
6 、golang elasticsearch 查询封装:
import (
"github.com/xxjwxc/esLog/view/es"
)
func main(){
client,_ := New(WithIndexName("test_log"), WithAddrs("http://192.168.198.17:9200/"))
//精确搜索
term := make(map[string]interface{})
term["topic"] = "topic"
term["etype"] = oplogger.EOpType_EOpGunbuster
term["user_name"] = "username"
term["ekey"] = "iddd-1"
term["elevel"] = oplogger.ELogLevel_EOperate
//模糊匹配
match := make(map[string]interface{})
match["desc"] = "desc"
match["attach"] = "attach"
// 时间范围
timeCase := make(map[string]CaseSection)
timeCase["creat_time"] = CaseSection{
Min: time.Now().AddDate(0, 0, -1),
Max: time.Now(),
}
//构造搜索器
var que EsQuery
que.OnPages(0, 10).OnTerm(term).OnMatch(match).OnRangeTime(timeCase)
// 打印查询字符串
data1, _ := json.Marshal(que.OnSource())
fmt.Println(string(data1))
// 查询结果
client, _ := New(WithIndexName(Index), WithAddrs(url))
var eslog []ESLog
client.WithOption(WithIndexName(Index), WithTypeName(Index)).Search(que.OnSource(), func(e []byte) error {
var tmp ESLog
json.Unmarshal(e, &tmp)
eslog = append(eslog, tmp)
return nil
})
fmt.Println(eslog)// 答应结果
}
6 、逻辑及代码调用说明:
日志写入调用:
//批量添加
client.BulkAdd(list)
日志写入调用:
//批量添加
client.Add(info)
日志搜索:
//精确搜索
term := make(map[string]interface{})
...
//模糊匹配
match := make(map[string]interface{})
...
//时间段搜索
timeCase := make(map[string]es.CaseSection)
...
// tools 搜索
eslist := tools.Search(term, match, timeCase, req.Page, req.Limit)
说明:以接口解耦,具体业务根据不同需求实现接口实现。主要用来对数据分流。