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:
@@ -0,0 +1,60 @@
|
||||
package postLog
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
logsDB *sql.DB
|
||||
tableName string
|
||||
)
|
||||
|
||||
func InitLogsDatabase(dbPath string) error {
|
||||
var err error
|
||||
logsDB, err = sql.Open("sqlite", dbPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open logs database: %w", err)
|
||||
}
|
||||
|
||||
if err = logsDB.Ping(); err != nil {
|
||||
return fmt.Errorf("failed to ping logs database: %w", err)
|
||||
}
|
||||
|
||||
timestamp := time.Now().Format("20060102_150405")
|
||||
tableName = fmt.Sprintf("logs_%s", timestamp)
|
||||
|
||||
createTableSQL := fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
level INTEGER NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
timestamp TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);`, tableName)
|
||||
|
||||
_, err = logsDB.Exec(createTableSQL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create logs table %s: %w", tableName, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertLogToDB(db *sql.DB, level int, content string, timestamp string) {
|
||||
if db != nil {
|
||||
_, err := db.Exec(fmt.Sprintf(`INSERT INTO %s (level, content, timestamp) VALUES (%d, '%s', '%s')`, tableName,
|
||||
level, content, timestamp))
|
||||
// fmt.Fprintf(os.Stderr, `INSERT INTO %s (level, content, timestamp) VALUES (%d, '%s', '%s')`, tableName, level, content, timestamp)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Failed to write log to database: %v\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func SetLogsDatabase(db *sql.DB) {
|
||||
loggerMutex.Lock()
|
||||
defer loggerMutex.Unlock()
|
||||
logsDB = db
|
||||
}
|
||||
+8
-13
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user