优化节点日志

This commit is contained in:
刘祥超
2021-11-30 16:43:16 +08:00
parent cfcad531f4
commit ed234b58bc
7 changed files with 43 additions and 13 deletions

View File

@@ -353,7 +353,7 @@ func (this *DBNodeInitializer) loop() error {
remotelogs.Error("DB_NODE", "create first table in database node failed: "+err.Error())
// 创建节点日志
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix(), "", nil)
if createLogErr != nil {
remotelogs.Error("NODE_LOG", createLogErr.Error())
}
@@ -397,7 +397,7 @@ func (this *DBNodeInitializer) loop() error {
remotelogs.Error("DB_NODE", "create first table in database node failed: "+err.Error())
// 创建节点日志
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix())
createLogErr := SharedNodeLogDAO.CreateLog(nil, nodeconfigs.NodeRoleDatabase, nodeId, 0, 0, "error", "ACCESS_LOG", "can not create access log table: "+err.Error(), time.Now().Unix(), "", nil)
if createLogErr != nil {
remotelogs.Error("NODE_LOG", createLogErr.Error())
}

View File

@@ -41,8 +41,25 @@ func init() {
}
// CreateLog 创建日志
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, originId int64, level string, tag string, description string, createdAt int64) error {
hash := stringutil.Md5(nodeRole + "@" + types.String(nodeId) + "@" + types.String(serverId) + "@" + types.String(originId) + "@" + level + "@" + tag + "@" + description)
func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, originId int64, level string, tag string, description string, createdAt int64, logType string, paramsJSON []byte) error {
// 修复以前同样的日志
if nodeId > 0 && level == "success" && len(logType) > 0 && len(paramsJSON) > 0 {
err := this.Query(tx).
Attr("nodeId", nodeId).
Attr("serverId", serverId).
Attr("type", logType).
Attr("level", "error").
Attr("isFixed", 0).
JSONContains("params", string(paramsJSON)).
Set("isFixed", 1).
Set("isRead", 1).
UpdateQuickly()
if err != nil {
return err
}
}
hash := stringutil.Md5(nodeRole + "@" + types.String(nodeId) + "@" + types.String(serverId) + "@" + types.String(originId) + "@" + level + "@" + tag + "@" + description + "@" + string(paramsJSON))
// 检查是否在重复最后一条,避免重复创建
lastLog, err := this.Query(tx).
@@ -76,6 +93,12 @@ func (this *NodeLogDAO) CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nod
op.Hash = hash
op.Count = 1
op.IsRead = level != "error"
op.Type = logType
if len(paramsJSON) > 0 {
op.Params = paramsJSON
}
err = this.Save(tx, op)
return err
}
@@ -253,6 +276,7 @@ func (this *NodeLogDAO) UpdateNodeLogFixed(tx *dbs.Tx, logId int64) error {
Attr("hash", hash).
Attr("isFixed", false).
Set("isFixed", true).
Set("isRead", true).
UpdateQuickly()
if err != nil {
return err

View File

@@ -4,6 +4,7 @@ package models
type NodeLog struct {
Id uint64 `field:"id"` // ID
Role string `field:"role"` // 节点角色
Type string `field:"type"` // 类型
CreatedAt uint64 `field:"createdAt"` // 创建时间
Tag string `field:"tag"` // 标签
Description string `field:"description"` // 描述
@@ -16,11 +17,13 @@ type NodeLog struct {
Count uint32 `field:"count"` // 重复次数
IsFixed uint8 `field:"isFixed"` // 是否已处理
IsRead uint8 `field:"isRead"` // 是否已读
Params string `field:"params"` // 参数
}
type NodeLogOperator struct {
Id interface{} // ID
Role interface{} // 节点角色
Type interface{} // 类型
CreatedAt interface{} // 创建时间
Tag interface{} // 标签
Description interface{} // 描述
@@ -33,6 +36,7 @@ type NodeLogOperator struct {
Count interface{} // 重复次数
IsFixed interface{} // 是否已处理
IsRead interface{} // 是否已读
Params interface{} // 参数
}
func NewNodeLogOperator() *NodeLogOperator {