refactor: replace standard log with zap logger and optimize code patterns

- Migrate all log.Printf/log.Println calls to structured zap logging
- Use cmp.Or for cleaner default value handling
- Replace manual loops with slices.ContainsFunc/IndexFunc
- Add batch query methods (IsLikedBatch, IsFavoritedBatch) to solve N+1 problem
- Update JSON tags from omitempty to omitzero for numeric fields
- Use errgroup-style wg.Go for worker goroutines
- Remove duplicate casbin/v2 dependency (keeping v3)
- Add plans/ to gitignore
This commit is contained in:
lafay
2026-03-17 00:47:17 +08:00
parent b028f7e1d3
commit 5d6c982c9c
38 changed files with 2256 additions and 290 deletions

View File

@@ -1,6 +1,7 @@
package service
import (
"cmp"
"context"
"os"
"sync"
@@ -67,9 +68,11 @@ func NewAsyncLogManager(
}
}
for i := 0; i < cfg.WorkerCount; i++ {
m.wg.Add(1)
go m.worker(i, operationRepo, loginRepo, dataChangeRepo)
for i := range cfg.WorkerCount {
workerID := i
m.wg.Go(func() {
m.worker(workerID, operationRepo, loginRepo, dataChangeRepo)
})
}
go m.flushScheduler()
@@ -91,8 +94,6 @@ func (m *AsyncLogManager) worker(
loginRepo *repository.LoginLogRepository,
dataChangeRepo *repository.DataChangeLogRepository,
) {
defer m.wg.Done()
opBatch := make([]*model.OperationLog, 0, m.cfg.BatchSize)
loginBatch := make([]*model.LoginLog, 0, m.cfg.BatchSize)
changeBatch := make([]*model.DataChangeLog, 0, m.cfg.BatchSize)
@@ -228,9 +229,7 @@ func (m *AsyncLogManager) writeToFallback(logType string, logs interface{}) erro
// initFallbackWriter 初始化降级写入器
func (m *AsyncLogManager) initFallbackWriter() error {
if m.cfg.FallbackPath == "" {
m.cfg.FallbackPath = "./logs/fallback.log"
}
m.cfg.FallbackPath = cmp.Or(m.cfg.FallbackPath, "./logs/fallback.log")
dir := "./logs"
if _, err := os.Stat(dir); os.IsNotExist(err) {