mirror of
https://github.com/dnote/dnote
synced 2026-03-14 22:45:50 +01:00
Improve logging
This commit is contained in:
parent
e0c4cb1545
commit
d57b8eaadb
4 changed files with 30 additions and 10 deletions
|
|
@ -73,7 +73,7 @@ var (
|
|||
// ErrInvalidPasswordChangeInput is an error for changing password
|
||||
ErrInvalidPasswordChangeInput appError = "Both current and new passwords are required to change the password."
|
||||
|
||||
ErrInvalidPassword appError = "Invalid currnet password."
|
||||
ErrInvalidPassword appError = "Invalid current password."
|
||||
// ErrEmailTooLong is an error for email length exceeding the limit
|
||||
ErrEmailTooLong appError = "Email is too long."
|
||||
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ func (a *App) CreateSession(userID int) (database.Session, error) {
|
|||
// DeleteUserSessions deletes all existing sessions for the given user. It effectively
|
||||
// invalidates all existing sessions.
|
||||
func (a *App) DeleteUserSessions(db *gorm.DB, userID int) error {
|
||||
if err := db.Debug().Where("user_id = ?", userID).Delete(&database.Session{}).Error; err != nil {
|
||||
if err := db.Where("user_id = ?", userID).Delete(&database.Session{}).Error; err != nil {
|
||||
return errors.Wrap(err, "deleting sessions")
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,9 +20,11 @@ import (
|
|||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/dnote/dnote/pkg/server/log"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -30,6 +32,21 @@ var (
|
|||
MigrationTableName = "migrations"
|
||||
)
|
||||
|
||||
// getDBLogLevel converts application log level to GORM log level
|
||||
func getDBLogLevel(level string) logger.LogLevel {
|
||||
switch level {
|
||||
case log.LevelDebug:
|
||||
case log.LevelInfo:
|
||||
return logger.Info
|
||||
case log.LevelWarn:
|
||||
return logger.Warn
|
||||
case log.LevelError:
|
||||
return logger.Error
|
||||
default:
|
||||
return logger.Error
|
||||
}
|
||||
}
|
||||
|
||||
// InitSchema migrates database schema to reflect the latest model definition
|
||||
func InitSchema(db *gorm.DB) {
|
||||
if err := db.AutoMigrate(
|
||||
|
|
@ -51,7 +68,9 @@ func Open(dbPath string) *gorm.DB {
|
|||
panic(errors.Wrapf(err, "creating database directory at %s", dir))
|
||||
}
|
||||
|
||||
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(getDBLogLevel(log.GetLevel())),
|
||||
})
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "opening database conection"))
|
||||
}
|
||||
|
|
@ -96,16 +115,14 @@ func StartWALCheckpointing(db *gorm.DB, interval time.Duration) {
|
|||
for range ticker.C {
|
||||
// TRUNCATE mode removes the WAL file after checkpointing
|
||||
if err := db.Exec("PRAGMA wal_checkpoint(TRUNCATE)").Error; err != nil {
|
||||
// Log error but don't panic - this is a background maintenance task
|
||||
// TODO: Use proper logging once available
|
||||
_ = err
|
||||
log.ErrorWrap(err, "WAL checkpoint failed")
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// StartPeriodicVacuum runs full VACUUM on a schedule to reclaim space and defragment.
|
||||
// WARNING: VACUUM acquires an exclusive lock and blocks all database operations briefly.
|
||||
// VACUUM acquires an exclusive lock and blocks all database operations briefly.
|
||||
func StartPeriodicVacuum(db *gorm.DB, interval time.Duration) {
|
||||
go func() {
|
||||
ticker := time.NewTicker(interval)
|
||||
|
|
@ -113,9 +130,7 @@ func StartPeriodicVacuum(db *gorm.DB, interval time.Duration) {
|
|||
|
||||
for range ticker.C {
|
||||
if err := db.Exec("VACUUM").Error; err != nil {
|
||||
// Log error but don't panic - this is a background maintenance task
|
||||
// TODO: Use proper logging once available
|
||||
_ = err
|
||||
log.ErrorWrap(err, "VACUUM failed")
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
|
|
|||
|
|
@ -70,6 +70,11 @@ func SetLevel(level string) {
|
|||
currentLevel = level
|
||||
}
|
||||
|
||||
// GetLevel returns the current global log level
|
||||
func GetLevel() string {
|
||||
return currentLevel
|
||||
}
|
||||
|
||||
// levelPriority returns a numeric priority for comparison
|
||||
func levelPriority(level string) int {
|
||||
switch level {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue