mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-04 07:34:01 +08:00
优化节点日志
This commit is contained in:
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -8,5 +8,5 @@ import (
|
||||
)
|
||||
|
||||
type DAOInterface interface {
|
||||
CreateLog(tx *dbs.Tx, nodeRole nodeconfigs.NodeRole, nodeId int64, serverId int64, originId int64, level string, tag string, description string, createdAt int64) error
|
||||
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
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ Loop:
|
||||
for {
|
||||
select {
|
||||
case log := <-logChan:
|
||||
err := sharedDAO.CreateLog(nil, nodeconfigs.NodeRoleAPI, log.NodeId, log.ServerId, log.OriginId, log.Level, log.Tag, log.Description, log.CreatedAt)
|
||||
err := sharedDAO.CreateLog(nil, nodeconfigs.NodeRoleAPI, log.NodeId, log.ServerId, log.OriginId, log.Level, log.Tag, log.Description, log.CreatedAt, "", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func (this *NodeLogService) CreateNodeLogs(ctx context.Context, req *pb.CreateNo
|
||||
tx := this.NullTx()
|
||||
|
||||
for _, nodeLog := range req.NodeLogs {
|
||||
err := models.SharedNodeLogDAO.CreateLog(tx, nodeLog.Role, nodeLog.NodeId, nodeLog.ServerId, nodeLog.OriginId, nodeLog.Level, nodeLog.Tag, nodeLog.Description, nodeLog.CreatedAt)
|
||||
err := models.SharedNodeLogDAO.CreateLog(tx, nodeLog.Role, nodeLog.NodeId, nodeLog.ServerId, nodeLog.OriginId, nodeLog.Level, nodeLog.Tag, nodeLog.Description, nodeLog.CreatedAt, nodeLog.Type, nodeLog.ParamsJSON)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -91,17 +91,19 @@ func (this *NodeLogService) ListNodeLogs(ctx context.Context, req *pb.ListNodeLo
|
||||
return &pb.ListNodeLogsResponse{NodeLogs: result}, nil
|
||||
}
|
||||
|
||||
// FixNodeLog 设置日志为已修复
|
||||
func (this *NodeLogService) FixNodeLog(ctx context.Context, req *pb.FixNodeLogRequest) (*pb.RPCSuccess, error) {
|
||||
// FixNodeLogs 设置日志为已修复
|
||||
func (this *NodeLogService) FixNodeLogs(ctx context.Context, req *pb.FixNodeLogsRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tx = this.NullTx()
|
||||
err = models.SharedNodeLogDAO.UpdateNodeLogFixed(tx, req.NodeLogId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
for _, logId := range req.NodeLogIds {
|
||||
err = models.SharedNodeLogDAO.UpdateNodeLogFixed(tx, logId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return this.Success()
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user