refactor: centralize logging and update resource initialization
This commit refactors the logging mechanism across the application by replacing direct logger calls with a centralized logging approach using the `common` package. Key changes include: - Replaced instances of `logger.SysLog` and `logger.FatalLog` with `common.SysLog` and `common.FatalLog` for consistent logging practices. - Updated resource initialization error handling to utilize the new logging structure, enhancing maintainability and readability. - Minor adjustments to improve code clarity and organization throughout various modules. This change aims to streamline logging and improve the overall architecture of the codebase.
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"log"
|
||||
"one-api/common"
|
||||
"one-api/constant"
|
||||
"one-api/logger"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -85,7 +84,7 @@ func createRootAccountIfNeed() error {
|
||||
var user User
|
||||
//if user.Status != common.UserStatusEnabled {
|
||||
if err := DB.First(&user).Error; err != nil {
|
||||
logger.SysLog("no user exists, create a root user for you: username is root, password is 123456")
|
||||
common.SysLog("no user exists, create a root user for you: username is root, password is 123456")
|
||||
hashedPassword, err := common.Password2Hash("123456")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -109,7 +108,7 @@ func CheckSetup() {
|
||||
if setup == nil {
|
||||
// No setup record exists, check if we have a root user
|
||||
if RootUserExists() {
|
||||
logger.SysLog("system is not initialized, but root user exists")
|
||||
common.SysLog("system is not initialized, but root user exists")
|
||||
// Create setup record
|
||||
newSetup := Setup{
|
||||
Version: common.Version,
|
||||
@@ -117,16 +116,16 @@ func CheckSetup() {
|
||||
}
|
||||
err := DB.Create(&newSetup).Error
|
||||
if err != nil {
|
||||
logger.SysLog("failed to create setup record: " + err.Error())
|
||||
common.SysLog("failed to create setup record: " + err.Error())
|
||||
}
|
||||
constant.Setup = true
|
||||
} else {
|
||||
logger.SysLog("system is not initialized and no root user exists")
|
||||
common.SysLog("system is not initialized and no root user exists")
|
||||
constant.Setup = false
|
||||
}
|
||||
} else {
|
||||
// Setup record exists, system is initialized
|
||||
logger.SysLog("system is already initialized at: " + time.Unix(setup.InitializedAt, 0).String())
|
||||
common.SysLog("system is already initialized at: " + time.Unix(setup.InitializedAt, 0).String())
|
||||
constant.Setup = true
|
||||
}
|
||||
}
|
||||
@@ -139,7 +138,7 @@ func chooseDB(envName string, isLog bool) (*gorm.DB, error) {
|
||||
if dsn != "" {
|
||||
if strings.HasPrefix(dsn, "postgres://") || strings.HasPrefix(dsn, "postgresql://") {
|
||||
// Use PostgreSQL
|
||||
logger.SysLog("using PostgreSQL as database")
|
||||
common.SysLog("using PostgreSQL as database")
|
||||
if !isLog {
|
||||
common.UsingPostgreSQL = true
|
||||
} else {
|
||||
@@ -153,7 +152,7 @@ func chooseDB(envName string, isLog bool) (*gorm.DB, error) {
|
||||
})
|
||||
}
|
||||
if strings.HasPrefix(dsn, "local") {
|
||||
logger.SysLog("SQL_DSN not set, using SQLite as database")
|
||||
common.SysLog("SQL_DSN not set, using SQLite as database")
|
||||
if !isLog {
|
||||
common.UsingSQLite = true
|
||||
} else {
|
||||
@@ -164,7 +163,7 @@ func chooseDB(envName string, isLog bool) (*gorm.DB, error) {
|
||||
})
|
||||
}
|
||||
// Use MySQL
|
||||
logger.SysLog("using MySQL as database")
|
||||
common.SysLog("using MySQL as database")
|
||||
// check parseTime
|
||||
if !strings.Contains(dsn, "parseTime") {
|
||||
if strings.Contains(dsn, "?") {
|
||||
@@ -183,7 +182,7 @@ func chooseDB(envName string, isLog bool) (*gorm.DB, error) {
|
||||
})
|
||||
}
|
||||
// Use SQLite
|
||||
logger.SysLog("SQL_DSN not set, using SQLite as database")
|
||||
common.SysLog("SQL_DSN not set, using SQLite as database")
|
||||
common.UsingSQLite = true
|
||||
return gorm.Open(sqlite.Open(common.SQLitePath), &gorm.Config{
|
||||
PrepareStmt: true, // precompile SQL
|
||||
@@ -217,11 +216,11 @@ func InitDB() (err error) {
|
||||
if common.UsingMySQL {
|
||||
//_, _ = sqlDB.Exec("ALTER TABLE channels MODIFY model_mapping TEXT;") // TODO: delete this line when most users have upgraded
|
||||
}
|
||||
logger.SysLog("database migration started")
|
||||
common.SysLog("database migration started")
|
||||
err = migrateDB()
|
||||
return err
|
||||
} else {
|
||||
logger.FatalLog(err)
|
||||
common.FatalLog(err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -254,11 +253,11 @@ func InitLogDB() (err error) {
|
||||
if !common.IsMasterNode {
|
||||
return nil
|
||||
}
|
||||
logger.SysLog("database migration started")
|
||||
common.SysLog("database migration started")
|
||||
err = migrateLOGDB()
|
||||
return err
|
||||
} else {
|
||||
logger.FatalLog(err)
|
||||
common.FatalLog(err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -355,7 +354,7 @@ func migrateDBFast() error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
logger.SysLog("database migrated")
|
||||
common.SysLog("database migrated")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -504,6 +503,6 @@ func PingDB() error {
|
||||
}
|
||||
|
||||
lastPingTime = time.Now()
|
||||
logger.SysLog("Database pinged successfully")
|
||||
common.SysLog("Database pinged successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user