feat: Establish core backend services including SQLite database, structured logging, and initial owner management capabilities.

This commit is contained in:
2026-02-06 20:53:05 +08:00
parent 98ac10987c
commit 6c8a036018
4 changed files with 199 additions and 6 deletions

View File

@@ -238,7 +238,38 @@ func GetLogs(limit int) []LogEntry {
}
// GetLogsByModule 按模块筛选日志并分页(最新的在前)
// 优先从内存读取,内存无数据时回退到数据库
func GetLogsByModule(module string, page, pageSize int) ([]LogEntry, int) {
logsMu.RLock()
// 检查内存中是否有该模块的日志
hasMemoryLogs := false
for i := len(logs) - 1; i >= 0; i-- {
if logs[i].Module == module {
hasMemoryLogs = true
break
}
}
logsMu.RUnlock()
// 内存中无数据,回退到数据库查询
if !hasMemoryLogs && database.Instance != nil {
dbLogs, total, err := database.Instance.GetLogsByModule(module, page, pageSize)
if err == nil && total > 0 {
entries := make([]LogEntry, len(dbLogs))
for i, dl := range dbLogs {
entries[i] = LogEntry{
Timestamp: dl.Timestamp,
Level: dl.Level,
Message: dl.Message,
Email: dl.Email,
Module: dl.Module,
}
}
return entries, total
}
}
// 从内存读取
logsMu.RLock()
defer logsMu.RUnlock()
@@ -283,7 +314,40 @@ func ClearLogs() {
}
// GetLogsByModuleAndLevel 按模块和级别筛选日志并分页(最新的在前)
// 优先从内存读取,内存无数据时回退到数据库
func GetLogsByModuleAndLevel(module, level string, page, pageSize int) ([]LogEntry, int) {
logsMu.RLock()
// 检查内存中是否有该模块+级别的日志
hasMemoryLogs := false
for i := len(logs) - 1; i >= 0; i-- {
if logs[i].Module == module {
if level == "" || logs[i].Level == level {
hasMemoryLogs = true
break
}
}
}
logsMu.RUnlock()
// 内存中无数据,回退到数据库查询
if !hasMemoryLogs && database.Instance != nil {
dbLogs, total, err := database.Instance.GetLogsByModuleAndLevel(module, level, page, pageSize)
if err == nil && total > 0 {
entries := make([]LogEntry, len(dbLogs))
for i, dl := range dbLogs {
entries[i] = LogEntry{
Timestamp: dl.Timestamp,
Level: dl.Level,
Message: dl.Message,
Email: dl.Email,
Module: dl.Module,
}
}
return entries, total
}
}
// 从内存读取
logsMu.RLock()
defer logsMu.RUnlock()