refactor(logging): 迁移日志记录到 Zerolog 并优化错误处理
- 将日志记录库从 Logrus 替换为 Zerolog,提升性能和灵活性 - 更新各个模块的日志记录方式,确保一致性 - 优化错误处理逻辑,确保在发生错误时能够正确记录并传递错误信息 - 移除不再使用的错误处理字段,简化代码结构 - 更新相关文档以反映新的日志记录和错误处理机制
This commit is contained in:
+2
-2
@@ -55,13 +55,13 @@ func testDmRawSQL(that *Context) Map {
|
||||
testArticle := that.Db.Get("article", "id", Map{"state": 0})
|
||||
if testArticle != nil {
|
||||
res, err := that.Db.Exec(`UPDATE "article" SET "modify_time" = NOW() WHERE "id" = ?`, testArticle.GetInt64("id"))
|
||||
if err.GetError() == nil && res != nil {
|
||||
if err == nil && res != nil {
|
||||
affected, _ := res.RowsAffected()
|
||||
test2["result"] = affected >= 0
|
||||
test2["affected"] = affected
|
||||
} else {
|
||||
test2["result"] = false
|
||||
test2["error"] = err.GetError()
|
||||
test2["error"] = err
|
||||
}
|
||||
} else {
|
||||
test2["result"] = true
|
||||
|
||||
@@ -55,13 +55,13 @@ func testMysqlRawSQL(that *Context) Map {
|
||||
testArticle := that.Db.Get("article", "id", Map{"state": 0})
|
||||
if testArticle != nil {
|
||||
res, err := that.Db.Exec("UPDATE `article` SET modify_time = NOW() WHERE id = ?", testArticle.GetInt64("id"))
|
||||
if err.GetError() == nil && res != nil {
|
||||
if err == nil && res != nil {
|
||||
affected, _ := res.RowsAffected()
|
||||
test2["result"] = affected >= 0
|
||||
test2["affected"] = affected
|
||||
} else {
|
||||
test2["result"] = false
|
||||
test2["error"] = err.GetError()
|
||||
test2["error"] = err
|
||||
}
|
||||
} else {
|
||||
test2["result"] = true
|
||||
@@ -73,21 +73,21 @@ func testMysqlRawSQL(that *Context) Map {
|
||||
articles3 := that.Db.Query("SELECT id, title FROM `article` WHERE id IN (?) LIMIT 10", []int{1, 2, 3, 4, 5})
|
||||
test3["result"] = len(articles3) >= 0
|
||||
test3["count"] = len(articles3)
|
||||
test3["lastQuery"] = that.Db.LastQuery
|
||||
test3["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test3)
|
||||
|
||||
test4 := Map{"name": "IN (?) 空数组替换为1=0"}
|
||||
articles4 := that.Db.Query("SELECT id, title FROM `article` WHERE id IN (?) LIMIT 10", []int{})
|
||||
test4["result"] = len(articles4) == 0
|
||||
test4["count"] = len(articles4)
|
||||
test4["lastQuery"] = that.Db.LastQuery
|
||||
test4["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test4)
|
||||
|
||||
test5 := Map{"name": "NOT IN (?) 空数组替换为1=1"}
|
||||
articles5 := that.Db.Query("SELECT id, title FROM `article` WHERE id NOT IN (?) LIMIT 10", []int{})
|
||||
test5["result"] = len(articles5) > 0
|
||||
test5["count"] = len(articles5)
|
||||
test5["lastQuery"] = that.Db.LastQuery
|
||||
test5["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test5)
|
||||
|
||||
result["tests"] = tests
|
||||
|
||||
+19
-19
@@ -157,7 +157,7 @@ func testBasicCRUD(that *Context) Map {
|
||||
})
|
||||
insertTest["result"] = adminId > 0
|
||||
insertTest["adminId"] = adminId
|
||||
insertTest["lastQuery"] = that.Db.LastQuery
|
||||
insertTest["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, insertTest)
|
||||
|
||||
getTest := Map{"name": "Get 获取单条记录测试"}
|
||||
@@ -181,7 +181,7 @@ func testBasicCRUD(that *Context) Map {
|
||||
})
|
||||
selectTest2["result"] = true
|
||||
selectTest2["count"] = len(admins2)
|
||||
selectTest2["lastQuery"] = that.Db.LastQuery
|
||||
selectTest2["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, selectTest2)
|
||||
|
||||
updateTest := Map{"name": "Update 更新测试"}
|
||||
@@ -224,7 +224,7 @@ func testConditionSyntax(that *Context) Map {
|
||||
articles2 := that.Db.Select("article", "id,title,state", Map{"state[!]": -1, "LIMIT": 3})
|
||||
test2["result"] = true
|
||||
test2["count"] = len(articles2)
|
||||
test2["lastQuery"] = that.Db.LastQuery
|
||||
test2["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test2)
|
||||
|
||||
test3 := Map{"name": "大于小于条件 ([>], [<])"}
|
||||
@@ -251,7 +251,7 @@ func testConditionSyntax(that *Context) Map {
|
||||
articles5 := that.Db.Select("article", "id,title", Map{"title[~]": "新闻", "LIMIT": 3})
|
||||
test5["result"] = true
|
||||
test5["count"] = len(articles5)
|
||||
test5["lastQuery"] = that.Db.LastQuery
|
||||
test5["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test5)
|
||||
|
||||
test6 := Map{"name": "右模糊查询 ([~!])"}
|
||||
@@ -264,7 +264,7 @@ func testConditionSyntax(that *Context) Map {
|
||||
articles7 := that.Db.Select("article", "id,title,click_num", Map{"click_num[<>]": Slice{0, 1000}, "LIMIT": 3})
|
||||
test7["result"] = true
|
||||
test7["count"] = len(articles7)
|
||||
test7["lastQuery"] = that.Db.LastQuery
|
||||
test7["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test7)
|
||||
|
||||
test8 := Map{"name": "NOT BETWEEN 查询 ([><])"}
|
||||
@@ -277,7 +277,7 @@ func testConditionSyntax(that *Context) Map {
|
||||
articles9 := that.Db.Select("article", "id,title", Map{"id": Slice{1, 2, 3, 4, 5}, "LIMIT": 5})
|
||||
test9["result"] = true
|
||||
test9["count"] = len(articles9)
|
||||
test9["lastQuery"] = that.Db.LastQuery
|
||||
test9["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test9)
|
||||
|
||||
test10 := Map{"name": "NOT IN 查询 ([!])"}
|
||||
@@ -313,7 +313,7 @@ func testConditionSyntax(that *Context) Map {
|
||||
})
|
||||
test13["result"] = true
|
||||
test13["count"] = len(articles13)
|
||||
test13["lastQuery"] = that.Db.LastQuery
|
||||
test13["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test13)
|
||||
|
||||
test14 := Map{"name": "显式 AND 条件"}
|
||||
@@ -353,7 +353,7 @@ func testConditionSyntax(that *Context) Map {
|
||||
})
|
||||
test16["result"] = true
|
||||
test16["count"] = len(articles16)
|
||||
test16["lastQuery"] = that.Db.LastQuery
|
||||
test16["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test16)
|
||||
|
||||
result["tests"] = tests
|
||||
@@ -494,7 +494,7 @@ func testJoinQuery(that *Context) Map {
|
||||
})
|
||||
test2["result"] = len(articles2) >= 0
|
||||
test2["count"] = len(articles2)
|
||||
test2["lastQuery"] = that.Db.LastQuery
|
||||
test2["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test2)
|
||||
|
||||
test3 := Map{"name": "多表 JOIN"}
|
||||
@@ -544,28 +544,28 @@ func testAggregate(that *Context) Map {
|
||||
sum3 := that.Db.Sum("article", "click_num", Map{"state": 0})
|
||||
test3["result"] = sum3 >= 0
|
||||
test3["sum"] = sum3
|
||||
test3["lastQuery"] = that.Db.LastQuery
|
||||
test3["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test3)
|
||||
|
||||
test4 := Map{"name": "Avg 平均值 (单字段名)"}
|
||||
avg4 := that.Db.Avg("article", "click_num", Map{"state": 0})
|
||||
test4["result"] = avg4 >= 0
|
||||
test4["avg"] = avg4
|
||||
test4["lastQuery"] = that.Db.LastQuery
|
||||
test4["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test4)
|
||||
|
||||
test5 := Map{"name": "Max 最大值 (单字段名)"}
|
||||
max5 := that.Db.Max("article", "click_num", Map{"state": 0})
|
||||
test5["result"] = max5 >= 0
|
||||
test5["max"] = max5
|
||||
test5["lastQuery"] = that.Db.LastQuery
|
||||
test5["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test5)
|
||||
|
||||
test6 := Map{"name": "Min 最小值 (单字段名)"}
|
||||
min6 := that.Db.Min("article", "sort", Map{"state": 0})
|
||||
test6["result"] = true
|
||||
test6["min"] = min6
|
||||
test6["lastQuery"] = that.Db.LastQuery
|
||||
test6["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test6)
|
||||
|
||||
test7 := Map{"name": "GROUP BY 分组统计"}
|
||||
@@ -586,7 +586,7 @@ func testAggregate(that *Context) Map {
|
||||
test8["result"] = sum8 >= 0
|
||||
test8["sum"] = sum8
|
||||
test8["match_single_field"] = sum8 == sum3
|
||||
test8["lastQuery"] = that.Db.LastQuery
|
||||
test8["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test8)
|
||||
|
||||
test9 := Map{"name": "聚合函数一致性验证"}
|
||||
@@ -606,14 +606,14 @@ func testAggregate(that *Context) Map {
|
||||
sum10 := that.Db.Sum("article", "article.click_num", joinSlice, Map{"article.state": 0})
|
||||
test10["result"] = sum10 >= 0
|
||||
test10["sum"] = sum10
|
||||
test10["lastQuery"] = that.Db.LastQuery
|
||||
test10["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test10)
|
||||
|
||||
test11 := Map{"name": "Count 带 JOIN"}
|
||||
count11 := that.Db.Count("article", joinSlice, Map{"article.state": 0})
|
||||
test11["result"] = count11 >= 0
|
||||
test11["count"] = count11
|
||||
test11["lastQuery"] = that.Db.LastQuery
|
||||
test11["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test11)
|
||||
|
||||
result["tests"] = tests
|
||||
@@ -661,7 +661,7 @@ func testPagination(that *Context) Map {
|
||||
Select("id,title")
|
||||
test4["result"] = len(articles4) <= 3
|
||||
test4["count"] = len(articles4)
|
||||
test4["lastQuery"] = that.Db.LastQuery
|
||||
test4["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test4)
|
||||
|
||||
result["tests"] = tests
|
||||
@@ -683,7 +683,7 @@ func testInserts(that *Context) Map {
|
||||
})
|
||||
test1["result"] = affected1 >= 0
|
||||
test1["affected"] = affected1
|
||||
test1["lastQuery"] = that.Db.LastQuery
|
||||
test1["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test1)
|
||||
|
||||
test2 := Map{"name": "Inserts 带 [#] 标记"}
|
||||
@@ -729,7 +729,7 @@ func testUpsert(that *Context) Map {
|
||||
)
|
||||
test1["result"] = affected1 >= 0
|
||||
test1["affected"] = affected1
|
||||
test1["lastQuery"] = that.Db.LastQuery
|
||||
test1["lastQuery"] = that.Db.GetLastQuery()
|
||||
tests = append(tests, test1)
|
||||
|
||||
test2 := Map{"name": "Upsert 更新已存在记录"}
|
||||
|
||||
@@ -43,7 +43,6 @@ func TestBatchCacheOperations(app *hotime.Application) {
|
||||
// testCacheMemoryBatch 测试内存缓存批量操作
|
||||
func testCacheMemoryBatch(app *hotime.Application) {
|
||||
memCache := &cache.CacheMemory{TimeOut: 3600, DbSet: true, SessionSet: true}
|
||||
memCache.SetError(&Error{})
|
||||
|
||||
// 测试 CachesSet
|
||||
testData := Map{
|
||||
@@ -84,7 +83,6 @@ func testCacheDbBatch(app *hotime.Application) {
|
||||
Mode: cache.CacheModeNew,
|
||||
Db: &app.Db,
|
||||
}
|
||||
dbCache.SetError(&Error{})
|
||||
|
||||
// 清理测试数据
|
||||
dbCache.CachesDelete([]string{"db_batch_key1", "db_batch_key2", "db_batch_key3"})
|
||||
@@ -241,7 +239,6 @@ func testCacheBackfill(app *hotime.Application) {
|
||||
Mode: cache.CacheModeNew,
|
||||
Db: &app.Db,
|
||||
}
|
||||
dbCache.SetError(&Error{})
|
||||
|
||||
testKey := "backfill_test_key_" + ObjToStr(time.Now().UnixNano())
|
||||
testValue := Map{"backfill": "test_data"}
|
||||
|
||||
Reference in New Issue
Block a user