From 56ad57238724b833e51e416cf3702e75523beec6 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Tue, 10 Sep 2024 00:32:36 +0800 Subject: [PATCH] code refactor --- cmd/cron_jobs.go | 10 +-- cmd/database.go | 6 +- cmd/user_data.go | 86 +++++++++---------- pkg/cli/user_data.go | 200 +++++++++++++++++++++---------------------- pkg/log/logger.go | 45 +++++++++- 5 files changed, 195 insertions(+), 152 deletions(-) diff --git a/cmd/cron_jobs.go b/cmd/cron_jobs.go index ea5df617..bd885cf9 100644 --- a/cmd/cron_jobs.go +++ b/cmd/cron_jobs.go @@ -46,14 +46,14 @@ func listAllCronJobs(c *core.CliContext) error { err = cron.InitializeCronJobSchedulerContainer(c, config, false) if err != nil { - log.BootErrorf(c, "[cron_jobs.listAllCronJobs] initializes cron job scheduler failed, because %s", err.Error()) + log.CliErrorf(c, "[cron_jobs.listAllCronJobs] initializes cron job scheduler failed, because %s", err.Error()) return err } cronJobs := cron.Container.GetAllJobs() if len(cronJobs) < 1 { - log.BootErrorf(c, "[cron_jobs.listAllCronJobs] there are no enabled cron jobs") + log.CliErrorf(c, "[cron_jobs.listAllCronJobs] there are no enabled cron jobs") return err } @@ -82,7 +82,7 @@ func runCronJob(c *core.CliContext) error { err = cron.InitializeCronJobSchedulerContainer(c, config, false) if err != nil { - log.BootErrorf(c, "[cron_jobs.runCronJob] initializes cron job scheduler failed, because %s", err.Error()) + log.CliErrorf(c, "[cron_jobs.runCronJob] initializes cron job scheduler failed, because %s", err.Error()) return err } @@ -90,11 +90,11 @@ func runCronJob(c *core.CliContext) error { err = cron.Container.SyncRunJobNow(jobName) if err != nil { - log.BootErrorf(c, "[cron_jobs.runCronJob] failed to run cron job \"%s\", because %s", jobName, err.Error()) + log.CliErrorf(c, "[cron_jobs.runCronJob] failed to run cron job \"%s\", because %s", jobName, err.Error()) return err } - log.BootInfof(c, "[cron_jobs.runCronJob] run cron job \"%s\" successfully", jobName) + log.CliInfof(c, "[cron_jobs.runCronJob] run cron job \"%s\" successfully", jobName) return nil } diff --git a/cmd/database.go b/cmd/database.go index a7805c06..6727f09a 100644 --- a/cmd/database.go +++ b/cmd/database.go @@ -29,16 +29,16 @@ func updateDatabaseStructure(c *core.CliContext) error { return err } - log.BootInfof(c, "[database.updateDatabaseStructure] starting maintaining") + log.CliInfof(c, "[database.updateDatabaseStructure] starting maintaining") err = updateAllDatabaseTablesStructure(c) if err != nil { - log.BootErrorf(c, "[database.updateDatabaseStructure] update database table structure failed, because %s", err.Error()) + log.CliErrorf(c, "[database.updateDatabaseStructure] update database table structure failed, because %s", err.Error()) return err } - log.BootInfof(c, "[database.updateDatabaseStructure] all tables maintained successfully") + log.CliInfof(c, "[database.updateDatabaseStructure] all tables maintained successfully") return nil } diff --git a/cmd/user_data.go b/cmd/user_data.go index f33bf4bb..14192721 100644 --- a/cmd/user_data.go +++ b/cmd/user_data.go @@ -313,7 +313,7 @@ func addNewUser(c *core.CliContext) error { user, err := clis.UserData.AddNewUser(c, username, email, nickname, password, defaultCurrency) if err != nil { - log.BootErrorf(c, "[user_data.addNewUser] error occurs when adding new user") + log.CliErrorf(c, "[user_data.addNewUser] error occurs when adding new user") return err } @@ -333,7 +333,7 @@ func getUserInfo(c *core.CliContext) error { user, err := clis.UserData.GetUserByUsername(c, username) if err != nil { - log.BootErrorf(c, "[user_data.getUserInfo] error occurs when getting user data") + log.CliErrorf(c, "[user_data.getUserInfo] error occurs when getting user data") return err } @@ -354,11 +354,11 @@ func modifyUserPassword(c *core.CliContext) error { err = clis.UserData.ModifyUserPassword(c, username, password) if err != nil { - log.BootErrorf(c, "[user_data.modifyUserPassword] error occurs when modifying user password") + log.CliErrorf(c, "[user_data.modifyUserPassword] error occurs when modifying user password") return err } - log.BootInfof(c, "[user_data.modifyUserPassword] password of user \"%s\" has been changed", username) + log.CliInfof(c, "[user_data.modifyUserPassword] password of user \"%s\" has been changed", username) return nil } @@ -374,11 +374,11 @@ func sendPasswordResetMail(c *core.CliContext) error { err = clis.UserData.SendPasswordResetMail(c, username) if err != nil { - log.BootErrorf(c, "[user_data.sendPasswordResetMail] error occurs when sending password reset email") + log.CliErrorf(c, "[user_data.sendPasswordResetMail] error occurs when sending password reset email") return err } - log.BootInfof(c, "[user_data.sendPasswordResetMail] a password reset email for user \"%s\" has been sent", username) + log.CliInfof(c, "[user_data.sendPasswordResetMail] a password reset email for user \"%s\" has been sent", username) return nil } @@ -394,11 +394,11 @@ func enableUser(c *core.CliContext) error { err = clis.UserData.EnableUser(c, username) if err != nil { - log.BootErrorf(c, "[user_data.enableUser] error occurs when setting user enabled") + log.CliErrorf(c, "[user_data.enableUser] error occurs when setting user enabled") return err } - log.BootInfof(c, "[user_data.enableUser] user \"%s\" has been set enabled", username) + log.CliInfof(c, "[user_data.enableUser] user \"%s\" has been set enabled", username) return nil } @@ -414,11 +414,11 @@ func disableUser(c *core.CliContext) error { err = clis.UserData.DisableUser(c, username) if err != nil { - log.BootErrorf(c, "[user_data.disableUser] error occurs when setting user disabled") + log.CliErrorf(c, "[user_data.disableUser] error occurs when setting user disabled") return err } - log.BootInfof(c, "[user_data.disableUser] user \"%s\" has been set disabled", username) + log.CliInfof(c, "[user_data.disableUser] user \"%s\" has been set disabled", username) return nil } @@ -434,11 +434,11 @@ func resendUserVerifyEmail(c *core.CliContext) error { err = clis.UserData.ResendVerifyEmail(c, username) if err != nil { - log.BootErrorf(c, "[user_data.resendUserVerifyEmail] error occurs when resending user verify email") + log.CliErrorf(c, "[user_data.resendUserVerifyEmail] error occurs when resending user verify email") return err } - log.BootInfof(c, "[user_data.resendUserVerifyEmail] verify email for user \"%s\" has been resent", username) + log.CliInfof(c, "[user_data.resendUserVerifyEmail] verify email for user \"%s\" has been resent", username) return nil } @@ -454,11 +454,11 @@ func setUserEmailVerified(c *core.CliContext) error { err = clis.UserData.SetUserEmailVerified(c, username) if err != nil { - log.BootErrorf(c, "[user_data.setUserEmailVerified] error occurs when setting user email address verified") + log.CliErrorf(c, "[user_data.setUserEmailVerified] error occurs when setting user email address verified") return err } - log.BootInfof(c, "[user_data.setUserEmailVerified] user \"%s\" email address has been set verified", username) + log.CliInfof(c, "[user_data.setUserEmailVerified] user \"%s\" email address has been set verified", username) return nil } @@ -474,11 +474,11 @@ func setUserEmailUnverified(c *core.CliContext) error { err = clis.UserData.SetUserEmailUnverified(c, username) if err != nil { - log.BootErrorf(c, "[user_data.setUserEmailUnverified] error occurs when setting user email address unverified") + log.CliErrorf(c, "[user_data.setUserEmailUnverified] error occurs when setting user email address unverified") return err } - log.BootInfof(c, "[user_data.setUserEmailUnverified] user \"%s\" email address has been set unverified", username) + log.CliInfof(c, "[user_data.setUserEmailUnverified] user \"%s\" email address has been set unverified", username) return nil } @@ -494,11 +494,11 @@ func deleteUser(c *core.CliContext) error { err = clis.UserData.DeleteUser(c, username) if err != nil { - log.BootErrorf(c, "[user_data.deleteUser] error occurs when deleting user") + log.CliErrorf(c, "[user_data.deleteUser] error occurs when deleting user") return err } - log.BootInfof(c, "[user_data.deleteUser] user \"%s\" has been deleted", username) + log.CliInfof(c, "[user_data.deleteUser] user \"%s\" has been deleted", username) return nil } @@ -514,11 +514,11 @@ func disableUser2FA(c *core.CliContext) error { err = clis.UserData.DisableUserTwoFactorAuthorization(c, username) if err != nil { - log.BootErrorf(c, "[user_data.disableUser2FA] error occurs when disabling user two-factor authorization") + log.CliErrorf(c, "[user_data.disableUser2FA] error occurs when disabling user two-factor authorization") return err } - log.BootInfof(c, "[user_data.disableUser2FA] two-factor authorization of user \"%s\" has been disabled", username) + log.CliInfof(c, "[user_data.disableUser2FA] two-factor authorization of user \"%s\" has been disabled", username) return nil } @@ -534,7 +534,7 @@ func listUserTokens(c *core.CliContext) error { tokens, err := clis.UserData.ListUserTokens(c, username) if err != nil { - log.BootErrorf(c, "[user_data.listUserTokens] error occurs when getting user tokens") + log.CliErrorf(c, "[user_data.listUserTokens] error occurs when getting user tokens") return err } @@ -560,11 +560,11 @@ func clearUserTokens(c *core.CliContext) error { err = clis.UserData.ClearUserTokens(c, username) if err != nil { - log.BootErrorf(c, "[user_data.clearUserTokens] error occurs when clearing user tokens") + log.CliErrorf(c, "[user_data.clearUserTokens] error occurs when clearing user tokens") return err } - log.BootInfof(c, "[user_data.clearUserTokens] all tokens of user \"%s\" has been cleared", username) + log.CliInfof(c, "[user_data.clearUserTokens] all tokens of user \"%s\" has been cleared", username) return nil } @@ -578,16 +578,16 @@ func checkUserTransactionAndAccount(c *core.CliContext) error { username := c.String("username") - log.BootInfof(c, "[user_data.checkUserTransactionAndAccount] starting checking user \"%s\" data", username) + log.CliInfof(c, "[user_data.checkUserTransactionAndAccount] starting checking user \"%s\" data", username) _, err = clis.UserData.CheckTransactionAndAccount(c, username) if err != nil { - log.BootErrorf(c, "[user_data.checkUserTransactionAndAccount] error occurs when checking user data") + log.CliErrorf(c, "[user_data.checkUserTransactionAndAccount] error occurs when checking user data") return err } - log.BootInfof(c, "[user_data.checkUserTransactionAndAccount] user transactions and accounts data has been checked successfully, there is no problem with user data") + log.CliInfof(c, "[user_data.checkUserTransactionAndAccount] user transactions and accounts data has been checked successfully, there is no problem with user data") return nil } @@ -601,16 +601,16 @@ func fixTransactionTagIndexNotHaveTransactionTime(c *core.CliContext) error { username := c.String("username") - log.BootInfof(c, "[user_data.fixTransactionTagIndexNotHaveTransactionTime] starting fixing user \"%s\" transaction tag index data", username) + log.CliInfof(c, "[user_data.fixTransactionTagIndexNotHaveTransactionTime] starting fixing user \"%s\" transaction tag index data", username) _, err = clis.UserData.FixTransactionTagIndexWithTransactionTime(c, username) if err != nil { - log.BootErrorf(c, "[user_data.fixTransactionTagIndexNotHaveTransactionTime] error occurs when fixing user data") + log.CliErrorf(c, "[user_data.fixTransactionTagIndexNotHaveTransactionTime] error occurs when fixing user data") return err } - log.BootInfof(c, "[user_data.fixTransactionTagIndexNotHaveTransactionTime] user transaction tag index data has been fixed successfully") + log.CliInfof(c, "[user_data.fixTransactionTagIndexNotHaveTransactionTime] user transaction tag index data has been fixed successfully") return nil } @@ -627,39 +627,39 @@ func exportUserTransaction(c *core.CliContext) error { fileType := c.String("type") if fileType != "" && fileType != "csv" && fileType != "tsv" { - log.BootErrorf(c, "[user_data.exportUserTransaction] export file type is not supported") + log.CliErrorf(c, "[user_data.exportUserTransaction] export file type is not supported") return errs.ErrNotSupported } if filePath == "" { - log.BootErrorf(c, "[user_data.exportUserTransaction] export file path is unspecified") + log.CliErrorf(c, "[user_data.exportUserTransaction] export file path is unspecified") return os.ErrNotExist } fileExists, err := utils.IsExists(filePath) if fileExists { - log.BootErrorf(c, "[user_data.exportUserTransaction] specified file path already exists") + log.CliErrorf(c, "[user_data.exportUserTransaction] specified file path already exists") return os.ErrExist } - log.BootInfof(c, "[user_data.exportUserTransaction] starting exporting user \"%s\" data", username) + log.CliInfof(c, "[user_data.exportUserTransaction] starting exporting user \"%s\" data", username) content, err := clis.UserData.ExportTransaction(c, username, fileType) if err != nil { - log.BootErrorf(c, "[user_data.exportUserTransaction] error occurs when exporting user data") + log.CliErrorf(c, "[user_data.exportUserTransaction] error occurs when exporting user data") return err } err = utils.WriteFile(filePath, content) if err != nil { - log.BootErrorf(c, "[user_data.exportUserTransaction] failed to write to %s", filePath) + log.CliErrorf(c, "[user_data.exportUserTransaction] failed to write to %s", filePath) return err } - log.BootInfof(c, "[user_data.exportUserTransaction] user transactions have been exported to %s", filePath) + log.CliInfof(c, "[user_data.exportUserTransaction] user transactions have been exported to %s", filePath) return nil } @@ -676,39 +676,39 @@ func importUserTransaction(c *core.CliContext) error { filetype := c.String("type") if filePath == "" { - log.BootErrorf(c, "[user_data.importUserTransaction] import file path is not specified") + log.CliErrorf(c, "[user_data.importUserTransaction] import file path is not specified") return os.ErrNotExist } fileExists, err := utils.IsExists(filePath) if !fileExists { - log.BootErrorf(c, "[user_data.importUserTransaction] import file does not exist") + log.CliErrorf(c, "[user_data.importUserTransaction] import file does not exist") return os.ErrExist } if filetype != "ezbookkeeping_csv" && filetype != "ezbookkeeping_tsv" { - log.BootErrorf(c, "[user_data.importUserTransaction] unknown file type \"%s\"", filetype) + log.CliErrorf(c, "[user_data.importUserTransaction] unknown file type \"%s\"", filetype) return errs.ErrImportFileTypeNotSupported } data, err := os.ReadFile(filePath) if err != nil { - log.BootErrorf(c, "[user_data.importUserTransaction] failed to load import file") + log.CliErrorf(c, "[user_data.importUserTransaction] failed to load import file") return err } - log.BootInfof(c, "[user_data.importUserTransaction] start importing transactions to user \"%s\"", username) + log.CliInfof(c, "[user_data.importUserTransaction] start importing transactions to user \"%s\"", username) err = clis.UserData.ImportTransaction(c, username, filetype, data) if err != nil { - log.BootErrorf(c, "[user_data.importUserTransaction] error occurs when importing user data") + log.CliErrorf(c, "[user_data.importUserTransaction] error occurs when importing user data") return err } - log.BootInfof(c, "[user_data.importUserTransaction] transactions have been imported to user \"%s\"", username) + log.CliInfof(c, "[user_data.importUserTransaction] transactions have been imported to user \"%s\"", username) return nil } diff --git a/pkg/cli/user_data.go b/pkg/cli/user_data.go index 4fa52a35..cf04a0c4 100644 --- a/pkg/cli/user_data.go +++ b/pkg/cli/user_data.go @@ -55,32 +55,32 @@ var ( // AddNewUser adds a new user according to specified info func (l *UserDataCli) AddNewUser(c *core.CliContext, username string, email string, nickname string, password string, defaultCurrency string) (*models.User, error) { if username == "" { - log.BootErrorf(c, "[user_data.AddNewUser] user name is empty") + log.CliErrorf(c, "[user_data.AddNewUser] user name is empty") return nil, errs.ErrUsernameIsEmpty } if email == "" { - log.BootErrorf(c, "[user_data.AddNewUser] user email is empty") + log.CliErrorf(c, "[user_data.AddNewUser] user email is empty") return nil, errs.ErrEmailIsEmpty } if nickname == "" { - log.BootErrorf(c, "[user_data.AddNewUser] user nickname is empty") + log.CliErrorf(c, "[user_data.AddNewUser] user nickname is empty") return nil, errs.ErrNicknameIsEmpty } if password == "" { - log.BootErrorf(c, "[user_data.AddNewUser] user password is empty") + log.CliErrorf(c, "[user_data.AddNewUser] user password is empty") return nil, errs.ErrPasswordIsEmpty } if defaultCurrency == "" { - log.BootErrorf(c, "[user_data.AddNewUser] user default currency is empty") + log.CliErrorf(c, "[user_data.AddNewUser] user default currency is empty") return nil, errs.ErrUserDefaultCurrencyIsEmpty } if _, ok := validators.AllCurrencyNames[defaultCurrency]; !ok { - log.BootErrorf(c, "[user_data.AddNewUser] user default currency is invalid") + log.CliErrorf(c, "[user_data.AddNewUser] user default currency is invalid") return nil, errs.ErrUserDefaultCurrencyIsInvalid } @@ -97,11 +97,11 @@ func (l *UserDataCli) AddNewUser(c *core.CliContext, username string, email stri err := l.users.CreateUser(c, user) if err != nil { - log.BootErrorf(c, "[user_data.AddNewUser] failed to create user \"%s\", because %s", user.Username, err.Error()) + log.CliErrorf(c, "[user_data.AddNewUser] failed to create user \"%s\", because %s", user.Username, err.Error()) return nil, err } - log.BootInfof(c, "[user_data.AddNewUser] user \"%s\" has add successfully, uid is %d", user.Username, user.Uid) + log.CliInfof(c, "[user_data.AddNewUser] user \"%s\" has add successfully, uid is %d", user.Username, user.Uid) return user, nil } @@ -109,14 +109,14 @@ func (l *UserDataCli) AddNewUser(c *core.CliContext, username string, email stri // GetUserByUsername returns user by user name func (l *UserDataCli) GetUserByUsername(c *core.CliContext, username string) (*models.User, error) { if username == "" { - log.BootErrorf(c, "[user_data.GetUserByUsername] user name is empty") + log.CliErrorf(c, "[user_data.GetUserByUsername] user name is empty") return nil, errs.ErrUsernameIsEmpty } user, err := l.users.GetUserByUsername(c, username) if err != nil { - log.BootErrorf(c, "[user_data.GetUserByUsername] failed to get user by user name \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.GetUserByUsername] failed to get user by user name \"%s\", because %s", username, err.Error()) return nil, err } @@ -126,19 +126,19 @@ func (l *UserDataCli) GetUserByUsername(c *core.CliContext, username string) (*m // ModifyUserPassword modifies user password func (l *UserDataCli) ModifyUserPassword(c *core.CliContext, username string, password string) error { if username == "" { - log.BootErrorf(c, "[user_data.ModifyUserPassword] user name is empty") + log.CliErrorf(c, "[user_data.ModifyUserPassword] user name is empty") return errs.ErrUsernameIsEmpty } if password == "" { - log.BootErrorf(c, "[user_data.ModifyUserPassword] user password is empty") + log.CliErrorf(c, "[user_data.ModifyUserPassword] user password is empty") return errs.ErrPasswordIsEmpty } user, err := l.users.GetUserByUsername(c, username) if err != nil { - log.BootErrorf(c, "[user_data.ModifyUserPassword] failed to get user by user name \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.ModifyUserPassword] failed to get user by user name \"%s\", because %s", username, err.Error()) return err } @@ -155,7 +155,7 @@ func (l *UserDataCli) ModifyUserPassword(c *core.CliContext, username string, pa _, _, err = l.users.UpdateUser(c, userNew, false) if err != nil { - log.BootErrorf(c, "[user_data.ModifyUserPassword] failed to update user \"%s\" password, because %s", user.Username, err.Error()) + log.CliErrorf(c, "[user_data.ModifyUserPassword] failed to update user \"%s\" password, because %s", user.Username, err.Error()) return err } @@ -163,9 +163,9 @@ func (l *UserDataCli) ModifyUserPassword(c *core.CliContext, username string, pa err = l.tokens.DeleteTokensBeforeTime(c, user.Uid, now) if err == nil { - log.BootInfof(c, "[user_data.ModifyUserPassword] revoke old tokens before unix time \"%d\" for user \"%s\"", now, user.Username) + log.CliInfof(c, "[user_data.ModifyUserPassword] revoke old tokens before unix time \"%d\" for user \"%s\"", now, user.Username) } else { - log.BootWarnf(c, "[user_data.ModifyUserPassword] failed to revoke old tokens for user \"%s\", because %s", user.Username, err.Error()) + log.CliWarnf(c, "[user_data.ModifyUserPassword] failed to revoke old tokens for user \"%s\", because %s", user.Username, err.Error()) } return nil @@ -174,33 +174,33 @@ func (l *UserDataCli) ModifyUserPassword(c *core.CliContext, username string, pa // SendPasswordResetMail sends an email with password reset link func (l *UserDataCli) SendPasswordResetMail(c *core.CliContext, username string) error { if username == "" { - log.BootErrorf(c, "[user_data.SendPasswordResetMail] user name is empty") + log.CliErrorf(c, "[user_data.SendPasswordResetMail] user name is empty") return errs.ErrUsernameIsEmpty } user, err := l.users.GetUserByUsername(c, username) if err != nil { - log.BootErrorf(c, "[user_data.SendPasswordResetMail] failed to get user by user name \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.SendPasswordResetMail] failed to get user by user name \"%s\", because %s", username, err.Error()) return err } if l.CurrentConfig().ForgetPasswordRequireVerifyEmail && !user.EmailVerified { - log.BootWarnf(c, "[user_data.SendPasswordResetMail] user \"uid:%d\" has not verified email", user.Uid) + log.CliWarnf(c, "[user_data.SendPasswordResetMail] user \"uid:%d\" has not verified email", user.Uid) return errs.ErrEmailIsNotVerified } token, _, err := l.tokens.CreatePasswordResetTokenWithoutUserAgent(c, user) if err != nil { - log.BootErrorf(c, "[user_data.SendPasswordResetMail] failed to create token for user \"uid:%d\", because %s", user.Uid, err.Error()) + log.CliErrorf(c, "[user_data.SendPasswordResetMail] failed to create token for user \"uid:%d\", because %s", user.Uid, err.Error()) return err } err = l.forgetPasswords.SendPasswordResetEmail(c, user, token, "") if err != nil { - log.BootWarnf(c, "[user_data.SendPasswordResetMail] cannot send email to \"%s\", because %s", user.Email, err.Error()) + log.CliWarnf(c, "[user_data.SendPasswordResetMail] cannot send email to \"%s\", because %s", user.Email, err.Error()) return err } @@ -210,14 +210,14 @@ func (l *UserDataCli) SendPasswordResetMail(c *core.CliContext, username string) // EnableUser sets user enabled according to the specified user name func (l *UserDataCli) EnableUser(c *core.CliContext, username string) error { if username == "" { - log.BootErrorf(c, "[user_data.EnableUser] user name is empty") + log.CliErrorf(c, "[user_data.EnableUser] user name is empty") return errs.ErrUsernameIsEmpty } err := l.users.EnableUser(c, username) if err != nil { - log.BootErrorf(c, "[user_data.EnableUser] failed to set user enabled by user name \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.EnableUser] failed to set user enabled by user name \"%s\", because %s", username, err.Error()) return err } @@ -227,14 +227,14 @@ func (l *UserDataCli) EnableUser(c *core.CliContext, username string) error { // DisableUser sets user disabled according to the specified user name func (l *UserDataCli) DisableUser(c *core.CliContext, username string) error { if username == "" { - log.BootErrorf(c, "[user_data.DisableUser] user name is empty") + log.CliErrorf(c, "[user_data.DisableUser] user name is empty") return errs.ErrUsernameIsEmpty } err := l.users.DisableUser(c, username) if err != nil { - log.BootErrorf(c, "[user_data.DisableUser] failed to set user disabled by user name \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.DisableUser] failed to set user disabled by user name \"%s\", because %s", username, err.Error()) return err } @@ -248,33 +248,33 @@ func (l *UserDataCli) ResendVerifyEmail(c *core.CliContext, username string) err } if username == "" { - log.BootErrorf(c, "[user_data.ResendVerifyEmail] user name is empty") + log.CliErrorf(c, "[user_data.ResendVerifyEmail] user name is empty") return errs.ErrUsernameIsEmpty } user, err := l.users.GetUserByUsername(c, username) if err != nil { - log.BootErrorf(c, "[user_data.ResendVerifyEmail] failed to get user by user name \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.ResendVerifyEmail] failed to get user by user name \"%s\", because %s", username, err.Error()) return err } if user.EmailVerified { - log.BootWarnf(c, "[user_data.ResendVerifyEmail] user \"uid:%d\" email has been verified", user.Uid) + log.CliWarnf(c, "[user_data.ResendVerifyEmail] user \"uid:%d\" email has been verified", user.Uid) return errs.ErrEmailIsVerified } token, _, err := l.tokens.CreateEmailVerifyTokenWithoutUserAgent(c, user) if err != nil { - log.BootErrorf(c, "[user_data.ResendVerifyEmail] failed to create token for user \"uid:%d\", because %s", user.Uid, err.Error()) + log.CliErrorf(c, "[user_data.ResendVerifyEmail] failed to create token for user \"uid:%d\", because %s", user.Uid, err.Error()) return errs.ErrTokenGenerating } err = l.users.SendVerifyEmail(user, token, "") if err != nil { - log.BootErrorf(c, "[user_data.ResendVerifyEmail] cannot send email to \"%s\", because %s", user.Email, err.Error()) + log.CliErrorf(c, "[user_data.ResendVerifyEmail] cannot send email to \"%s\", because %s", user.Email, err.Error()) return err } @@ -284,14 +284,14 @@ func (l *UserDataCli) ResendVerifyEmail(c *core.CliContext, username string) err // SetUserEmailVerified sets user email address verified func (l *UserDataCli) SetUserEmailVerified(c *core.CliContext, username string) error { if username == "" { - log.BootErrorf(c, "[user_data.SetUserEmailVerified] user name is empty") + log.CliErrorf(c, "[user_data.SetUserEmailVerified] user name is empty") return errs.ErrUsernameIsEmpty } err := l.users.SetUserEmailVerified(c, username) if err != nil { - log.BootErrorf(c, "[user_data.SetUserEmailVerified] failed to set user email address verified by user name \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.SetUserEmailVerified] failed to set user email address verified by user name \"%s\", because %s", username, err.Error()) return err } @@ -301,14 +301,14 @@ func (l *UserDataCli) SetUserEmailVerified(c *core.CliContext, username string) // SetUserEmailUnverified sets user email address unverified func (l *UserDataCli) SetUserEmailUnverified(c *core.CliContext, username string) error { if username == "" { - log.BootErrorf(c, "[user_data.SetUserEmailUnverified] user name is empty") + log.CliErrorf(c, "[user_data.SetUserEmailUnverified] user name is empty") return errs.ErrUsernameIsEmpty } err := l.users.SetUserEmailUnverified(c, username) if err != nil { - log.BootErrorf(c, "[user_data.SetUserEmailUnverified] failed to set user email address unverified by user name \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.SetUserEmailUnverified] failed to set user email address unverified by user name \"%s\", because %s", username, err.Error()) return err } @@ -318,14 +318,14 @@ func (l *UserDataCli) SetUserEmailUnverified(c *core.CliContext, username string // DeleteUser deletes user according to the specified user name func (l *UserDataCli) DeleteUser(c *core.CliContext, username string) error { if username == "" { - log.BootErrorf(c, "[user_data.DeleteUser] user name is empty") + log.CliErrorf(c, "[user_data.DeleteUser] user name is empty") return errs.ErrUsernameIsEmpty } err := l.users.DeleteUser(c, username) if err != nil { - log.BootErrorf(c, "[user_data.DeleteUser] failed to delete user by user name \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.DeleteUser] failed to delete user by user name \"%s\", because %s", username, err.Error()) return err } @@ -335,21 +335,21 @@ func (l *UserDataCli) DeleteUser(c *core.CliContext, username string) error { // ListUserTokens returns all tokens of the specified user func (l *UserDataCli) ListUserTokens(c *core.CliContext, username string) ([]*models.TokenRecord, error) { if username == "" { - log.BootErrorf(c, "[user_data.ListUserTokens] user name is empty") + log.CliErrorf(c, "[user_data.ListUserTokens] user name is empty") return nil, errs.ErrUsernameIsEmpty } uid, err := l.getUserIdByUsername(c, username) if err != nil { - log.BootErrorf(c, "[user_data.ListUserTokens] error occurs when getting user id by user name") + log.CliErrorf(c, "[user_data.ListUserTokens] error occurs when getting user id by user name") return nil, err } tokens, err := l.tokens.GetAllUnexpiredNormalTokensByUid(c, uid) if err != nil { - log.BootErrorf(c, "[user_data.ListUserTokens] failed to get tokens of user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.ListUserTokens] failed to get tokens of user \"%s\", because %s", username, err.Error()) return nil, err } @@ -359,14 +359,14 @@ func (l *UserDataCli) ListUserTokens(c *core.CliContext, username string) ([]*mo // ClearUserTokens clears all tokens of the specified user func (l *UserDataCli) ClearUserTokens(c *core.CliContext, username string) error { if username == "" { - log.BootErrorf(c, "[user_data.ClearUserTokens] user name is empty") + log.CliErrorf(c, "[user_data.ClearUserTokens] user name is empty") return errs.ErrUsernameIsEmpty } uid, err := l.getUserIdByUsername(c, username) if err != nil { - log.BootErrorf(c, "[user_data.ClearUserTokens] error occurs when getting user id by user name") + log.CliErrorf(c, "[user_data.ClearUserTokens] error occurs when getting user id by user name") return err } @@ -374,7 +374,7 @@ func (l *UserDataCli) ClearUserTokens(c *core.CliContext, username string) error err = l.tokens.DeleteTokensBeforeTime(c, uid, now) if err != nil { - log.BootErrorf(c, "[user_data.ClearUserTokens] failed to delete tokens of user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.ClearUserTokens] failed to delete tokens of user \"%s\", because %s", username, err.Error()) return err } @@ -384,21 +384,21 @@ func (l *UserDataCli) ClearUserTokens(c *core.CliContext, username string) error // DisableUserTwoFactorAuthorization disables 2fa for the specified user func (l *UserDataCli) DisableUserTwoFactorAuthorization(c *core.CliContext, username string) error { if username == "" { - log.BootErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] user name is empty") + log.CliErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] user name is empty") return errs.ErrUsernameIsEmpty } uid, err := l.getUserIdByUsername(c, username) if err != nil { - log.BootErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] error occurs when getting user id by user name") + log.CliErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] error occurs when getting user id by user name") return err } enableTwoFactor, err := l.twoFactorAuthorizations.ExistsTwoFactorSetting(c, uid) if err != nil { - log.BootErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] failed to check two-factor setting, because %s", err.Error()) + log.CliErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] failed to check two-factor setting, because %s", err.Error()) return err } @@ -409,14 +409,14 @@ func (l *UserDataCli) DisableUserTwoFactorAuthorization(c *core.CliContext, user err = l.twoFactorAuthorizations.DeleteTwoFactorRecoveryCodes(c, uid) if err != nil { - log.BootErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] failed to delete two-factor recovery codes for user \"%s\"", username) + log.CliErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] failed to delete two-factor recovery codes for user \"%s\"", username) return err } err = l.twoFactorAuthorizations.DeleteTwoFactorSetting(c, uid) if err != nil { - log.BootErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] failed to delete two-factor setting for user \"%s\"", username) + log.CliErrorf(c, "[user_data.DisableUserTwoFactorAuthorization] failed to delete two-factor setting for user \"%s\"", username) return err } @@ -426,21 +426,21 @@ func (l *UserDataCli) DisableUserTwoFactorAuthorization(c *core.CliContext, user // CheckTransactionAndAccount checks whether all user transactions and all user accounts are correct func (l *UserDataCli) CheckTransactionAndAccount(c *core.CliContext, username string) (bool, error) { if username == "" { - log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] user name is empty") + log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] user name is empty") return false, errs.ErrUsernameIsEmpty } uid, err := l.getUserIdByUsername(c, username) if err != nil { - log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] error occurs when getting user id by user name") + log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] error occurs when getting user id by user name") return false, err } accountMap, categoryMap, tagMap, tagIndexes, tagIndexesMap, err := l.getUserEssentialData(c, uid, username) if err != nil { - log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] failed to get essential data for user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] failed to get essential data for user \"%s\", because %s", username, err.Error()) return false, err } @@ -455,7 +455,7 @@ func (l *UserDataCli) CheckTransactionAndAccount(c *core.CliContext, username st allTransactions, err := l.transactions.GetAllTransactions(c, uid, pageCountForGettingTransactions, false) if err != nil { - log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] failed to all transactions for user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] failed to all transactions for user \"%s\", because %s", username, err.Error()) return false, err } @@ -506,7 +506,7 @@ func (l *UserDataCli) CheckTransactionAndAccount(c *core.CliContext, username st } else if transaction.Type == models.TRANSACTION_DB_TYPE_TRANSFER_IN { balance = balance + transaction.Amount } else { - log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] transaction type of transaction \"id:%d\" is invalid", transaction.TransactionId) + log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] transaction type of transaction \"id:%d\" is invalid", transaction.TransactionId) return false, errs.ErrOperationFailed } @@ -521,12 +521,12 @@ func (l *UserDataCli) CheckTransactionAndAccount(c *core.CliContext, username st } if !exists && account.Balance != 0 { - log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] account \"id:%d\" balance is not correct, expected balance is %d, but there is no transaction actually", account.AccountId, account.Balance) + log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] account \"id:%d\" balance is not correct, expected balance is %d, but there is no transaction actually", account.AccountId, account.Balance) return false, errs.ErrOperationFailed } if account.Balance != actualBalance { - log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] account \"id:%d\" balance is not correct, expected balance is %d, but actual balance is %d", account.AccountId, account.Balance, actualBalance) + log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] account \"id:%d\" balance is not correct, expected balance is %d, but actual balance is %d", account.AccountId, account.Balance, actualBalance) return false, errs.ErrOperationFailed } } @@ -535,7 +535,7 @@ func (l *UserDataCli) CheckTransactionAndAccount(c *core.CliContext, username st _, exists := accountMap[accountId] if !exists { - log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] account \"id:%d\" does not exist, but there are some transactions of this account actually, and actual balance is %d", accountId, actualBalance) + log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] account \"id:%d\" does not exist, but there are some transactions of this account actually, and actual balance is %d", accountId, actualBalance) return false, errs.ErrOperationFailed } } @@ -544,7 +544,7 @@ func (l *UserDataCli) CheckTransactionAndAccount(c *core.CliContext, username st tagIndex := tagIndexes[i] if tagIndex.TransactionTime < 1 { - log.BootErrorf(c, "[user_data.CheckTransactionAndAccount] transaction tag index \"id:%d\" does not have transaction time", tagIndex.TagIndexId) + log.CliErrorf(c, "[user_data.CheckTransactionAndAccount] transaction tag index \"id:%d\" does not have transaction time", tagIndex.TagIndexId) return false, errs.ErrOperationFailed } } @@ -555,21 +555,21 @@ func (l *UserDataCli) CheckTransactionAndAccount(c *core.CliContext, username st // FixTransactionTagIndexWithTransactionTime fixes user transaction tag index data with transaction time func (l *UserDataCli) FixTransactionTagIndexWithTransactionTime(c *core.CliContext, username string) (bool, error) { if username == "" { - log.BootErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] user name is empty") + log.CliErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] user name is empty") return false, errs.ErrUsernameIsEmpty } uid, err := l.getUserIdByUsername(c, username) if err != nil { - log.BootErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] error occurs when getting user id by user name") + log.CliErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] error occurs when getting user id by user name") return false, err } tagIndexes, err := l.tags.GetAllTagIdsOfAllTransactions(c, uid) if err != nil { - log.BootErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] failed to get tag index for user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] failed to get tag index for user \"%s\", because %s", username, err.Error()) return false, err } @@ -584,14 +584,14 @@ func (l *UserDataCli) FixTransactionTagIndexWithTransactionTime(c *core.CliConte } if len(invalidTagIndexes) < 1 { - log.BootErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] all user transaction tag index data has been checked, there is no problem with user data") + log.CliErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] all user transaction tag index data has been checked, there is no problem with user data") return false, errs.ErrOperationFailed } allTransactions, err := l.transactions.GetAllTransactions(c, uid, pageCountForGettingTransactions, false) if err != nil { - log.BootErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] failed to all transactions for user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] failed to all transactions for user \"%s\", because %s", username, err.Error()) return false, err } @@ -611,7 +611,7 @@ func (l *UserDataCli) FixTransactionTagIndexWithTransactionTime(c *core.CliConte err = l.tags.ModifyTagIndexTransactionTime(c, uid, invalidTagIndexes) if err != nil { - log.BootErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] failed to update transaction tag index for user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.FixTransactionTagIndexWithTransactionTime] failed to update transaction tag index for user \"%s\", because %s", username, err.Error()) return false, err } @@ -621,28 +621,28 @@ func (l *UserDataCli) FixTransactionTagIndexWithTransactionTime(c *core.CliConte // ExportTransaction returns csv file content according user all transactions func (l *UserDataCli) ExportTransaction(c *core.CliContext, username string, fileType string) ([]byte, error) { if username == "" { - log.BootErrorf(c, "[user_data.ExportTransaction] user name is empty") + log.CliErrorf(c, "[user_data.ExportTransaction] user name is empty") return nil, errs.ErrUsernameIsEmpty } uid, err := l.getUserIdByUsername(c, username) if err != nil { - log.BootErrorf(c, "[user_data.ExportTransaction] error occurs when getting user id by user name") + log.CliErrorf(c, "[user_data.ExportTransaction] error occurs when getting user id by user name") return nil, err } accountMap, categoryMap, tagMap, _, tagIndexesMap, err := l.getUserEssentialData(c, uid, username) if err != nil { - log.BootErrorf(c, "[user_data.ExportTransaction] failed to get essential data for user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.ExportTransaction] failed to get essential data for user \"%s\", because %s", username, err.Error()) return nil, err } allTransactions, err := l.transactions.GetAllTransactions(c, uid, pageCountForDataExport, true) if err != nil { - log.BootErrorf(c, "[user_data.ExportTransaction] failed to all transactions for user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.ExportTransaction] failed to all transactions for user \"%s\", because %s", username, err.Error()) return nil, err } @@ -657,7 +657,7 @@ func (l *UserDataCli) ExportTransaction(c *core.CliContext, username string, fil result, err := dataExporter.ToExportedContent(c, uid, allTransactions, accountMap, categoryMap, tagMap, tagIndexesMap) if err != nil { - log.BootErrorf(c, "[user_data.ExportTransaction] failed to get csv format exported data for \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.ExportTransaction] failed to get csv format exported data for \"%s\", because %s", username, err.Error()) return nil, err } @@ -666,7 +666,7 @@ func (l *UserDataCli) ExportTransaction(c *core.CliContext, username string, fil func (l *UserDataCli) ImportTransaction(c *core.CliContext, username string, fileType string, data []byte) error { if username == "" { - log.BootErrorf(c, "[user_data.ImportTransaction] user name is empty") + log.CliErrorf(c, "[user_data.ImportTransaction] user name is empty") return errs.ErrUsernameIsEmpty } @@ -683,44 +683,44 @@ func (l *UserDataCli) ImportTransaction(c *core.CliContext, username string, fil user, err := l.GetUserByUsername(c, username) if err != nil { - log.BootErrorf(c, "[user_data.ImportTransaction] failed to get user by user name \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.ImportTransaction] failed to get user by user name \"%s\", because %s", username, err.Error()) return err } accountMap, categoryMap, tagMap, err := l.getUserEssentialDataForImport(c, user.Uid, username) if err != nil { - log.BootErrorf(c, "[user_data.ImportTransaction] failed to get essential data for user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.ImportTransaction] failed to get essential data for user \"%s\", because %s", username, err.Error()) return err } parsedTransactions, newAccounts, newCategories, newTags, err := dataImporter.ParseImportedData(c, user, data, utils.GetTimezoneOffsetMinutes(time.Local), accountMap, categoryMap, tagMap) if err != nil { - log.BootErrorf(c, "[user_data.ImportTransaction] failed to parse imported data for \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.ImportTransaction] failed to parse imported data for \"%s\", because %s", username, err.Error()) return err } if len(parsedTransactions) < 1 { - log.BootErrorf(c, "[user_data.ImportTransaction] there are no transactions in import file") + log.CliErrorf(c, "[user_data.ImportTransaction] there are no transactions in import file") return errs.ErrOperationFailed } if len(newAccounts) > 0 { accountNames := l.accounts.GetAccountNames(newAccounts) - log.BootErrorf(c, "[user_data.ImportTransaction] there are %d accounts (%s) need to be created, please create them manually", len(newAccounts), strings.Join(accountNames, ",")) + log.CliErrorf(c, "[user_data.ImportTransaction] there are %d accounts (%s) need to be created, please create them manually", len(newAccounts), strings.Join(accountNames, ",")) return errs.ErrOperationFailed } if len(newCategories) > 0 { categoryNames := l.categories.GetCategoryNames(newCategories) - log.BootErrorf(c, "[user_data.ImportTransaction] there are %d transaction categories (%s) need to be created, please create them manually", len(newCategories), strings.Join(categoryNames, ",")) + log.CliErrorf(c, "[user_data.ImportTransaction] there are %d transaction categories (%s) need to be created, please create them manually", len(newCategories), strings.Join(categoryNames, ",")) return errs.ErrOperationFailed } if len(newTags) > 0 { tagNames := l.tags.GetTagNames(newTags) - log.BootErrorf(c, "[user_data.ImportTransaction] there are %d transaction tags (%s) need to be created, please create them manually", len(newTags), strings.Join(tagNames, ",")) + log.CliErrorf(c, "[user_data.ImportTransaction] there are %d transaction tags (%s) need to be created, please create them manually", len(newTags), strings.Join(tagNames, ",")) return errs.ErrOperationFailed } @@ -728,14 +728,14 @@ func (l *UserDataCli) ImportTransaction(c *core.CliContext, username string, fil newTransactionTagIdsMap, err := parsedTransactions.ToTransactionTagIdsMap() if err != nil { - log.BootErrorf(c, "[user_data.ImportTransaction] failed to get transaction tag ids map, because %s", err.Error()) + log.CliErrorf(c, "[user_data.ImportTransaction] failed to get transaction tag ids map, because %s", err.Error()) return errs.ErrOperationFailed } err = l.transactions.BatchCreateTransactions(c, user.Uid, newTransactions, newTransactionTagIdsMap) if err != nil { - log.BootErrorf(c, "[user_data.ImportTransaction] failed to create transaction, because %s", err.Error()) + log.CliErrorf(c, "[user_data.ImportTransaction] failed to create transaction, because %s", err.Error()) return err } @@ -746,7 +746,7 @@ func (l *UserDataCli) getUserIdByUsername(c *core.CliContext, username string) ( user, err := l.GetUserByUsername(c, username) if err != nil { - log.BootErrorf(c, "[user_data.getUserIdByUsername] failed to get user by user name \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.getUserIdByUsername] failed to get user by user name \"%s\", because %s", username, err.Error()) return 0, err } @@ -755,14 +755,14 @@ func (l *UserDataCli) getUserIdByUsername(c *core.CliContext, username string) ( func (l *UserDataCli) getUserEssentialData(c *core.CliContext, uid int64, username string) (accountMap map[int64]*models.Account, categoryMap map[int64]*models.TransactionCategory, tagMap map[int64]*models.TransactionTag, tagIndexes []*models.TransactionTagIndex, tagIndexesMap map[int64][]int64, err error) { if uid <= 0 { - log.BootErrorf(c, "[user_data.getUserEssentialData] user uid \"%d\" is invalid", uid) + log.CliErrorf(c, "[user_data.getUserEssentialData] user uid \"%d\" is invalid", uid) return nil, nil, nil, nil, nil, errs.ErrUserIdInvalid } accounts, err := l.accounts.GetAllAccountsByUid(c, uid) if err != nil { - log.BootErrorf(c, "[user_data.getUserEssentialData] failed to get accounts for user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.getUserEssentialData] failed to get accounts for user \"%s\", because %s", username, err.Error()) return nil, nil, nil, nil, nil, err } @@ -771,7 +771,7 @@ func (l *UserDataCli) getUserEssentialData(c *core.CliContext, uid int64, userna categories, err := l.categories.GetAllCategoriesByUid(c, uid, 0, -1) if err != nil { - log.BootErrorf(c, "[user_data.getUserEssentialData] failed to get categories for user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.getUserEssentialData] failed to get categories for user \"%s\", because %s", username, err.Error()) return nil, nil, nil, nil, nil, err } @@ -780,7 +780,7 @@ func (l *UserDataCli) getUserEssentialData(c *core.CliContext, uid int64, userna tags, err := l.tags.GetAllTagsByUid(c, uid) if err != nil { - log.BootErrorf(c, "[user_data.getUserEssentialData] failed to get tags for user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.getUserEssentialData] failed to get tags for user \"%s\", because %s", username, err.Error()) return nil, nil, nil, nil, nil, err } @@ -789,7 +789,7 @@ func (l *UserDataCli) getUserEssentialData(c *core.CliContext, uid int64, userna tagIndexes, err = l.tags.GetAllTagIdsOfAllTransactions(c, uid) if err != nil { - log.BootErrorf(c, "[user_data.getUserEssentialData] failed to get tag index for user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.getUserEssentialData] failed to get tag index for user \"%s\", because %s", username, err.Error()) return nil, nil, nil, nil, nil, err } @@ -800,14 +800,14 @@ func (l *UserDataCli) getUserEssentialData(c *core.CliContext, uid int64, userna func (l *UserDataCli) getUserEssentialDataForImport(c *core.CliContext, uid int64, username string) (accountMap map[string]*models.Account, categoryMap map[string]*models.TransactionCategory, tagMap map[string]*models.TransactionTag, err error) { if uid <= 0 { - log.BootErrorf(c, "[user_data.getUserEssentialDataForImport] user uid \"%d\" is invalid", uid) + log.CliErrorf(c, "[user_data.getUserEssentialDataForImport] user uid \"%d\" is invalid", uid) return nil, nil, nil, errs.ErrUserIdInvalid } accounts, err := l.accounts.GetAllAccountsByUid(c, uid) if err != nil { - log.BootErrorf(c, "[user_data.getUserEssentialDataForImport] failed to get accounts for user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.getUserEssentialDataForImport] failed to get accounts for user \"%s\", because %s", username, err.Error()) return nil, nil, nil, err } @@ -816,7 +816,7 @@ func (l *UserDataCli) getUserEssentialDataForImport(c *core.CliContext, uid int6 categories, err := l.categories.GetAllCategoriesByUid(c, uid, 0, -1) if err != nil { - log.BootErrorf(c, "[user_data.getUserEssentialDataForImport] failed to get categories for user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.getUserEssentialDataForImport] failed to get categories for user \"%s\", because %s", username, err.Error()) return nil, nil, nil, err } @@ -825,7 +825,7 @@ func (l *UserDataCli) getUserEssentialDataForImport(c *core.CliContext, uid int6 tags, err := l.tags.GetAllTagsByUid(c, uid) if err != nil { - log.BootErrorf(c, "[user_data.getUserEssentialDataForImport] failed to get tags for user \"%s\", because %s", username, err.Error()) + log.CliErrorf(c, "[user_data.getUserEssentialDataForImport] failed to get tags for user \"%s\", because %s", username, err.Error()) return nil, nil, nil, err } @@ -838,12 +838,12 @@ func (l *UserDataCli) checkTransactionAccount(c *core.CliContext, transaction *m account, exists := accountMap[transaction.AccountId] if !exists { - log.BootErrorf(c, "[user_data.checkTransactionAccount] the account \"id:%d\" of transaction \"id:%d\" does not exist", transaction.AccountId, transaction.TransactionId) + log.CliErrorf(c, "[user_data.checkTransactionAccount] the account \"id:%d\" of transaction \"id:%d\" does not exist", transaction.AccountId, transaction.TransactionId) return errs.ErrAccountNotFound } if account.ParentAccountId == models.LevelOneAccountParentId && accountHasChild[account.AccountId] { - log.BootErrorf(c, "[user_data.checkTransactionAccount] the account \"id:%d\" of transaction \"id:%d\" is not a sub-account", transaction.AccountId, transaction.TransactionId) + log.CliErrorf(c, "[user_data.checkTransactionAccount] the account \"id:%d\" of transaction \"id:%d\" is not a sub-account", transaction.AccountId, transaction.TransactionId) return errs.ErrOperationFailed } @@ -851,12 +851,12 @@ func (l *UserDataCli) checkTransactionAccount(c *core.CliContext, transaction *m relatedAccount, exists := accountMap[transaction.RelatedAccountId] if !exists { - log.BootErrorf(c, "[user_data.checkTransactionAccount] the related account \"id:%d\" of transaction \"id:%d\" does not exist", transaction.RelatedAccountId, transaction.TransactionId) + log.CliErrorf(c, "[user_data.checkTransactionAccount] the related account \"id:%d\" of transaction \"id:%d\" does not exist", transaction.RelatedAccountId, transaction.TransactionId) return errs.ErrAccountNotFound } if relatedAccount.ParentAccountId == models.LevelOneAccountParentId && accountHasChild[relatedAccount.AccountId] { - log.BootErrorf(c, "[user_data.checkTransactionAccount] the related account \"id:%d\" of transaction \"id:%d\" is not a sub-account", transaction.RelatedAccountId, transaction.TransactionId) + log.CliErrorf(c, "[user_data.checkTransactionAccount] the related account \"id:%d\" of transaction \"id:%d\" is not a sub-account", transaction.RelatedAccountId, transaction.TransactionId) return errs.ErrOperationFailed } } @@ -867,7 +867,7 @@ func (l *UserDataCli) checkTransactionAccount(c *core.CliContext, transaction *m func (l *UserDataCli) checkTransactionCategory(c *core.CliContext, transaction *models.Transaction, categoryMap map[int64]*models.TransactionCategory) error { if transaction.Type == models.TRANSACTION_DB_TYPE_MODIFY_BALANCE { if transaction.CategoryId > 0 { - log.BootErrorf(c, "[user_data.checkTransactionCategory] transaction \"id:%d\" is balance modification transaction, but has category \"id:%d\"", transaction.TransactionId, transaction.CategoryId) + log.CliErrorf(c, "[user_data.checkTransactionCategory] transaction \"id:%d\" is balance modification transaction, but has category \"id:%d\"", transaction.TransactionId, transaction.CategoryId) return errs.ErrBalanceModificationTransactionCannotSetCategory } else { return nil @@ -877,12 +877,12 @@ func (l *UserDataCli) checkTransactionCategory(c *core.CliContext, transaction * category, exists := categoryMap[transaction.CategoryId] if !exists { - log.BootErrorf(c, "[user_data.checkTransactionCategory] the transaction category \"id:%d\" of transaction \"id:%d\" does not exist", transaction.CategoryId, transaction.TransactionId) + log.CliErrorf(c, "[user_data.checkTransactionCategory] the transaction category \"id:%d\" of transaction \"id:%d\" does not exist", transaction.CategoryId, transaction.TransactionId) return errs.ErrTransactionCategoryNotFound } if category.ParentCategoryId == models.LevelOneTransactionParentId { - log.BootErrorf(c, "[user_data.checkTransactionCategory] the transaction category \"id:%d\" of transaction \"id:%d\" is not a sub category", transaction.CategoryId, transaction.TransactionId) + log.CliErrorf(c, "[user_data.checkTransactionCategory] the transaction category \"id:%d\" of transaction \"id:%d\" is not a sub category", transaction.CategoryId, transaction.TransactionId) return errs.ErrOperationFailed } @@ -901,7 +901,7 @@ func (l *UserDataCli) checkTransactionTag(c *core.CliContext, transactionId int6 tag, exists := tagMap[tagIndex] if !exists { - log.BootErrorf(c, "[user_data.checkTransactionTag] the transaction tag \"id:%d\" of transaction \"id:%d\" does not exist", tag.TagId, transactionId) + log.CliErrorf(c, "[user_data.checkTransactionTag] the transaction tag \"id:%d\" of transaction \"id:%d\" does not exist", tag.TagId, transactionId) return errs.ErrTransactionTagNotFound } } @@ -917,22 +917,22 @@ func (l *UserDataCli) checkTransactionRelatedTransaction(c *core.CliContext, tra relatedTransaction, exists := transactionMap[transaction.RelatedId] if !exists { - log.BootErrorf(c, "[user_data.checkTransactionRelatedTransaction] the related transaction \"id:%d\" of transaction \"id:%d\" does not exist", transaction.RelatedId, transaction.TransactionId) + log.CliErrorf(c, "[user_data.checkTransactionRelatedTransaction] the related transaction \"id:%d\" of transaction \"id:%d\" does not exist", transaction.RelatedId, transaction.TransactionId) return errs.ErrTransactionNotFound } if transaction.RelatedId != relatedTransaction.TransactionId || transaction.TransactionId != relatedTransaction.RelatedId { - log.BootErrorf(c, "[user_data.checkTransactionRelatedTransaction] related ids of transaction \"id:%d\" and transaction \"id:%d\" are not equal", transaction.RelatedId, transaction.TransactionId) + log.CliErrorf(c, "[user_data.checkTransactionRelatedTransaction] related ids of transaction \"id:%d\" and transaction \"id:%d\" are not equal", transaction.RelatedId, transaction.TransactionId) return errs.ErrOperationFailed } if transaction.RelatedAccountId != relatedTransaction.AccountId || transaction.AccountId != relatedTransaction.RelatedAccountId { - log.BootErrorf(c, "[user_data.checkTransactionRelatedTransaction] related account ids of transaction \"id:%d\" and transaction \"id:%d\" are not equal", transaction.RelatedId, transaction.TransactionId) + log.CliErrorf(c, "[user_data.checkTransactionRelatedTransaction] related account ids of transaction \"id:%d\" and transaction \"id:%d\" are not equal", transaction.RelatedId, transaction.TransactionId) return errs.ErrOperationFailed } if transaction.RelatedAccountAmount != relatedTransaction.Amount || transaction.Amount != relatedTransaction.RelatedAccountAmount { - log.BootErrorf(c, "[user_data.checkTransactionRelatedTransaction] related amounts of transaction \"id:%d\" and transaction \"id:%d\" are not equal", transaction.RelatedId, transaction.TransactionId) + log.CliErrorf(c, "[user_data.checkTransactionRelatedTransaction] related amounts of transaction \"id:%d\" and transaction \"id:%d\" are not equal", transaction.RelatedId, transaction.TransactionId) return errs.ErrOperationFailed } @@ -940,7 +940,7 @@ func (l *UserDataCli) checkTransactionRelatedTransaction(c *core.CliContext, tra relatedAccount := accountMap[transaction.RelatedAccountId] if account.Currency == relatedAccount.Currency && transaction.Amount != transaction.RelatedAccountAmount { - log.BootWarnf(c, "[user_data.checkTransactionRelatedTransaction] transfer-in amount and transfer-out amount of transaction \"id:%d\" are not equal", transaction.TransactionId) + log.CliWarnf(c, "[user_data.checkTransactionRelatedTransaction] transfer-in amount and transfer-out amount of transaction \"id:%d\" are not equal", transaction.TransactionId) } return nil diff --git a/pkg/log/logger.go b/pkg/log/logger.go index 32a4118a..61d38ee5 100644 --- a/pkg/log/logger.go +++ b/pkg/log/logger.go @@ -16,6 +16,7 @@ const logFieldRequestId = "REQUEST_ID" const logFieldExtra = "EXTRA" var bootLogger = logrus.New() +var cliLogger = logrus.New() var defaultLogger = logrus.New() var requestLogger = logrus.New() var sqlQueryLogger = logrus.New() @@ -25,6 +26,10 @@ func init() { bootLogger.SetOutput(os.Stdout) bootLogger.SetLevel(logrus.InfoLevel) + cliLogger.SetFormatter(&LogFormatter{}) + cliLogger.SetOutput(os.Stdout) + cliLogger.SetLevel(logrus.InfoLevel) + defaultLogger.SetFormatter(&LogFormatter{}) defaultLogger.SetOutput(os.Stdout) defaultLogger.SetLevel(logrus.InfoLevel) @@ -108,15 +113,19 @@ func SetLoggerConfiguration(config *settings.Config, isDisableBootLog bool) erro sqlQueryLogger.SetOutput(queryMultipleWriter) if config.LogLevel == settings.LOGLEVEL_DEBUG { + cliLogger.SetLevel(logrus.DebugLevel) bootLogger.SetLevel(logrus.DebugLevel) defaultLogger.SetLevel(logrus.DebugLevel) } else if config.LogLevel == settings.LOGLEVEL_INFO { + cliLogger.SetLevel(logrus.InfoLevel) bootLogger.SetLevel(logrus.InfoLevel) defaultLogger.SetLevel(logrus.InfoLevel) } else if config.LogLevel == settings.LOGLEVEL_WARN { + cliLogger.SetLevel(logrus.WarnLevel) bootLogger.SetLevel(logrus.WarnLevel) defaultLogger.SetLevel(logrus.WarnLevel) } else if config.LogLevel == settings.LOGLEVEL_ERROR { + cliLogger.SetLevel(logrus.ErrorLevel) bootLogger.SetLevel(logrus.ErrorLevel) defaultLogger.SetLevel(logrus.ErrorLevel) } @@ -207,7 +216,41 @@ func BootErrorf(c core.Context, format string, args ...any) { } else { bootLogger.WithField(logFieldRequestId, c.GetContextId()).Error(getFinalLog(format, args...)) } - }} + } +} + +// CliInfof logs boot info log +func CliInfof(c core.Context, format string, args ...any) { + if cliLogger != nil { + if c == nil { + cliLogger.Info(getFinalLog(format, args...)) + } else { + cliLogger.WithField(logFieldRequestId, c.GetContextId()).Info(getFinalLog(format, args...)) + } + } +} + +// CliWarnf logs boot warn log +func CliWarnf(c core.Context, format string, args ...any) { + if cliLogger != nil { + if c == nil { + cliLogger.Warn(getFinalLog(format, args...)) + } else { + cliLogger.WithField(logFieldRequestId, c.GetContextId()).Warn(getFinalLog(format, args...)) + } + } +} + +// CliErrorf logs boot error log +func CliErrorf(c core.Context, format string, args ...any) { + if cliLogger != nil { + if c == nil { + cliLogger.Error(getFinalLog(format, args...)) + } else { + cliLogger.WithField(logFieldRequestId, c.GetContextId()).Error(getFinalLog(format, args...)) + } + } +} // Requestf logs http request log with custom format func Requestf(c core.Context, format string, args ...any) {