mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-07 02:20:24 +08:00
修复日志内容可能过长而无法存入数据库的问题
This commit is contained in:
@@ -3,6 +3,7 @@ package models
|
|||||||
import (
|
import (
|
||||||
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/iwind/TeaGo/Tea"
|
"github.com/iwind/TeaGo/Tea"
|
||||||
"github.com/iwind/TeaGo/dbs"
|
"github.com/iwind/TeaGo/dbs"
|
||||||
@@ -38,7 +39,7 @@ func init() {
|
|||||||
func (this *LogDAO) CreateLog(tx *dbs.Tx, adminType string, adminId int64, level string, description string, action string, ip string) error {
|
func (this *LogDAO) CreateLog(tx *dbs.Tx, adminType string, adminId int64, level string, description string, action string, ip string) error {
|
||||||
var op = NewLogOperator()
|
var op = NewLogOperator()
|
||||||
op.Level = level
|
op.Level = level
|
||||||
op.Description = description
|
op.Description = utils.LimitString(description, 1000)
|
||||||
op.Action = action
|
op.Action = action
|
||||||
op.Ip = ip
|
op.Ip = ip
|
||||||
op.Type = adminType
|
op.Type = adminType
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
dbutils "github.com/TeaOSLab/EdgeAPI/internal/db/utils"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||||
|
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
@@ -43,6 +44,8 @@ func init() {
|
|||||||
|
|
||||||
// CreateLog 创建日志
|
// 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, logType string, paramsJSON []byte) error {
|
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 {
|
||||||
|
description = utils.LimitString(description, 1000)
|
||||||
|
|
||||||
// 修复以前同样的日志
|
// 修复以前同样的日志
|
||||||
if nodeId > 0 && level == "success" && len(logType) > 0 && len(paramsJSON) > 0 {
|
if nodeId > 0 && level == "success" && len(logType) > 0 && len(paramsJSON) > 0 {
|
||||||
err := this.Query(tx).
|
err := this.Query(tx).
|
||||||
|
|||||||
@@ -62,3 +62,23 @@ func Similar(s1 string, s2 string) float32 {
|
|||||||
|
|
||||||
return (float32(count)/float32(l1) + float32(count)/float32(l2)) / 2
|
return (float32(count)/float32(l1) + float32(count)/float32(l2)) / 2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LimitString 限制字符串长度
|
||||||
|
func LimitString(s string, maxLength int) string {
|
||||||
|
if len(s) <= maxLength {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
if maxLength <= 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var runes = []rune(s)
|
||||||
|
var rs = len(runes)
|
||||||
|
for i := 0; i < rs; i++ {
|
||||||
|
if len(string(runes[:i+1])) > maxLength {
|
||||||
|
return string(runes[:i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|||||||
@@ -28,3 +28,15 @@ func TestSimilar(t *testing.T) {
|
|||||||
t.Log(utils.Similar("efgj", "hijk"))
|
t.Log(utils.Similar("efgj", "hijk"))
|
||||||
t.Log(utils.Similar("efgj", "klmn"))
|
t.Log(utils.Similar("efgj", "klmn"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLimitString(t *testing.T) {
|
||||||
|
var a = assert.NewAssertion(t)
|
||||||
|
a.IsTrue(utils.LimitString("", 4) == "")
|
||||||
|
a.IsTrue(utils.LimitString("abcd", 0) == "")
|
||||||
|
a.IsTrue(utils.LimitString("abcd", 5) == "abcd")
|
||||||
|
a.IsTrue(utils.LimitString("abcd", 4) == "abcd")
|
||||||
|
a.IsTrue(utils.LimitString("abcd", 3) == "abc")
|
||||||
|
a.IsTrue(utils.LimitString("abcd", 1) == "a")
|
||||||
|
a.IsTrue(utils.LimitString("中文测试", 1) == "")
|
||||||
|
a.IsTrue(utils.LimitString("中文测试", 3) == "中")
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user