feat(logging): add database logging support and refactor logging system

- Implement new database logging functionality with separate logs database
- Refactor PostLog function to store logs in database while maintaining console output
- Add new InitLogsDatabase function and related database operations
- Update main.go to support separate database paths for data and logs
- Add logging for user creation events
This commit is contained in:
2026-03-17 18:47:51 +08:00
parent 49048597f2
commit 20ec25328d
5 changed files with 91 additions and 20 deletions
+8 -13
View File
@@ -48,28 +48,23 @@ func PostLog(message string, level int) {
// Lock to make reading debug flag and all output atomic across threads
loggerMutex.Lock()
defer loggerMutex.Unlock()
// Skip DEBUG when debug is off
if idx == DEBUG && !isDebug {
loggerMutex.Unlock()
return
}
// Colored output
levelDisplay := colorOut_256(levelNames[idx], levelColors[idx])
switch idx {
case DEBUG:
fmt.Printf("[%s - %s] %s\n", timeNow, levelDisplay, message)
case INFO:
fmt.Printf("[%s - %s] %s\n", timeNow, levelDisplay, message)
case WARNING:
fmt.Printf("[%s - %s] %s\n", timeNow, levelDisplay, message)
case ERROR:
fmt.Printf("[%s - %s] %s\n", timeNow, levelDisplay, message)
case FATAL:
fmt.Printf("[%s - %s] %s\n", timeNow, levelDisplay, message)
}
// Copy logsDB to avoid holding the lock during DB operation
db := logsDB
loggerMutex.Unlock()
fmt.Printf("[%s - %s] %s\n", timeNow, levelDisplay, message)
insertLogToDB(db, level, message, timeNow)
}
// Helper functions for different log levels