Merge pull request #68 from alanwooo/master

Updated the new logger module for the go files in the controllers directory
This commit is contained in:
Wenkai Yin 2016-03-30 23:23:33 -05:00
commit 77b52a1dfd
5 changed files with 31 additions and 33 deletions

View File

@ -16,10 +16,10 @@
package controllers package controllers
import ( import (
"log"
"os" "os"
"strings" "strings"
"github.com/vmware/harbor/utils/log"
"github.com/astaxie/beego" "github.com/astaxie/beego"
"github.com/beego/i18n" "github.com/beego/i18n"
) )
@ -124,10 +124,10 @@ func init() {
//conf/app.conf -> os.Getenv("config_path") //conf/app.conf -> os.Getenv("config_path")
configPath := os.Getenv("CONFIG_PATH") configPath := os.Getenv("CONFIG_PATH")
if len(configPath) != 0 { if len(configPath) != 0 {
log.Printf("Config path: %s", configPath) log.Infof("Config path: %s", configPath)
beego.AppConfigPath = configPath beego.AppConfigPath = configPath
if err := beego.ParseConfig(); err != nil { if err := beego.ParseConfig(); err != nil {
beego.Warning("Failed to parse config file: ", configPath, "error: ", err) log.Warningf("Failed to parse config file: %s, error: %v", configPath, err)
} }
} }
@ -150,7 +150,7 @@ func init() {
for _, lang := range langs { for _, lang := range langs {
if err := i18n.SetMessage(lang, "static/i18n/"+"locale_"+lang+".ini"); err != nil { if err := i18n.SetMessage(lang, "static/i18n/"+"locale_"+lang+".ini"); err != nil {
beego.Error("Fail to set message file:" + err.Error()) log.Errorf("Fail to set message file: %s", err.Error())
} }
} }
} }

View File

@ -21,8 +21,7 @@ import (
"os" "os"
"github.com/vmware/harbor/dao" "github.com/vmware/harbor/dao"
"github.com/vmware/harbor/utils/log"
"github.com/astaxie/beego"
) )
// ItemDetailController handles requet to /registry/detail, which shows the detail of a project. // ItemDetailController handles requet to /registry/detail, which shows the detail of a project.
@ -37,7 +36,7 @@ func (idc *ItemDetailController) Get() {
projectID, _ := idc.GetInt64("project_id") projectID, _ := idc.GetInt64("project_id")
if projectID <= 0 { if projectID <= 0 {
beego.Error("Invalid project id:", projectID) log.Errorf("Invalid project id: %d", projectID)
idc.Redirect("/signIn", http.StatusFound) idc.Redirect("/signIn", http.StatusFound)
return return
} }
@ -45,7 +44,7 @@ func (idc *ItemDetailController) Get() {
project, err := dao.GetProjectByID(projectID) project, err := dao.GetProjectByID(projectID)
if err != nil { if err != nil {
beego.Error("Error occurred in GetProjectById:", err) log.Errorf("Error occurred in GetProjectById: %v", err)
idc.CustomAbort(http.StatusInternalServerError, "Internal error.") idc.CustomAbort(http.StatusInternalServerError, "Internal error.")
} }
@ -70,13 +69,13 @@ func (idc *ItemDetailController) Get() {
roleList, err := dao.GetUserProjectRoles(userID, projectID) roleList, err := dao.GetUserProjectRoles(userID, projectID)
if err != nil { if err != nil {
beego.Error("Error occurred in GetUserProjectRoles:", err) log.Errorf("Error occurred in GetUserProjectRoles: %v", err)
idc.CustomAbort(http.StatusInternalServerError, "Internal error.") idc.CustomAbort(http.StatusInternalServerError, "Internal error.")
} }
isAdmin, err := dao.IsAdminRole(userID) isAdmin, err := dao.IsAdminRole(userID)
if err != nil { if err != nil {
beego.Error("Error occurred in IsAdminRole:", err) log.Errorf("Error occurred in IsAdminRole: %v", err)
idc.CustomAbort(http.StatusInternalServerError, "Internal error.") idc.CustomAbort(http.StatusInternalServerError, "Internal error.")
} }

View File

@ -20,8 +20,7 @@ import (
"github.com/vmware/harbor/auth" "github.com/vmware/harbor/auth"
"github.com/vmware/harbor/models" "github.com/vmware/harbor/models"
"github.com/vmware/harbor/utils/log"
"github.com/astaxie/beego"
) )
// IndexController handles request to / // IndexController handles request to /
@ -52,7 +51,7 @@ func (c *CommonController) Login() {
user, err := auth.Login(models.AuthModel{principal, password}) user, err := auth.Login(models.AuthModel{principal, password})
if err != nil { if err != nil {
beego.Error("Error occurred in UserLogin:", err) log.Errorf("Error occurred in UserLogin: %v", err)
c.CustomAbort(http.StatusUnauthorized, "") c.CustomAbort(http.StatusUnauthorized, "")
} }

View File

@ -25,6 +25,7 @@ import (
"github.com/vmware/harbor/dao" "github.com/vmware/harbor/dao"
"github.com/vmware/harbor/models" "github.com/vmware/harbor/models"
"github.com/vmware/harbor/utils" "github.com/vmware/harbor/utils"
"github.com/vmware/harbor/utils/log"
"github.com/astaxie/beego" "github.com/astaxie/beego"
) )
@ -51,25 +52,25 @@ func (cc *CommonController) UpdatePassword() {
sessionUserID := cc.GetSession("userId") sessionUserID := cc.GetSession("userId")
if sessionUserID == nil { if sessionUserID == nil {
beego.Warning("User does not login.") log.Warning("User does not login.")
cc.CustomAbort(http.StatusUnauthorized, "please_login_first") cc.CustomAbort(http.StatusUnauthorized, "please_login_first")
} }
oldPassword := cc.GetString("old_password") oldPassword := cc.GetString("old_password")
if oldPassword == "" { if oldPassword == "" {
beego.Error("Old password is blank") log.Error("Old password is blank")
cc.CustomAbort(http.StatusBadRequest, "Old password is blank") cc.CustomAbort(http.StatusBadRequest, "Old password is blank")
} }
queryUser := models.User{UserID: sessionUserID.(int), Password: oldPassword} queryUser := models.User{UserID: sessionUserID.(int), Password: oldPassword}
user, err := dao.CheckUserPassword(queryUser) user, err := dao.CheckUserPassword(queryUser)
if err != nil { if err != nil {
beego.Error("Error occurred in CheckUserPassword:", err) log.Errorf("Error occurred in CheckUserPassword: %v", err)
cc.CustomAbort(http.StatusInternalServerError, "Internal error.") cc.CustomAbort(http.StatusInternalServerError, "Internal error.")
} }
if user == nil { if user == nil {
beego.Warning("Password input is not correct") log.Warning("Password input is not correct")
cc.CustomAbort(http.StatusForbidden, "old_password_is_not_correct") cc.CustomAbort(http.StatusForbidden, "old_password_is_not_correct")
} }
@ -78,7 +79,7 @@ func (cc *CommonController) UpdatePassword() {
updateUser := models.User{UserID: sessionUserID.(int), Password: password, Salt: user.Salt} updateUser := models.User{UserID: sessionUserID.(int), Password: password, Salt: user.Salt}
err = dao.ChangeUserPassword(updateUser, oldPassword) err = dao.ChangeUserPassword(updateUser, oldPassword)
if err != nil { if err != nil {
beego.Error("Error occurred in ChangeUserPassword:", err) log.Errorf("Error occurred in ChangeUserPassword: %v", err)
cc.CustomAbort(http.StatusInternalServerError, "Internal error.") cc.CustomAbort(http.StatusInternalServerError, "Internal error.")
} }
} else { } else {
@ -116,7 +117,7 @@ func (cc *CommonController) SendEmail() {
queryUser := models.User{Email: email} queryUser := models.User{Email: email}
exist, err := dao.UserExists(queryUser, "email") exist, err := dao.UserExists(queryUser, "email")
if err != nil { if err != nil {
beego.Error("Error occurred in UserExists:", err) log.Errorf("Error occurred in UserExists: %v", err)
cc.CustomAbort(http.StatusInternalServerError, "Internal error.") cc.CustomAbort(http.StatusInternalServerError, "Internal error.")
} }
if !exist { if !exist {
@ -125,7 +126,7 @@ func (cc *CommonController) SendEmail() {
messageTemplate, err := template.ParseFiles("views/reset-password-mail.tpl") messageTemplate, err := template.ParseFiles("views/reset-password-mail.tpl")
if err != nil { if err != nil {
beego.Error("Parse email template file failed:", err) log.Errorf("Parse email template file failed: %v", err)
cc.CustomAbort(http.StatusInternalServerError, err.Error()) cc.CustomAbort(http.StatusInternalServerError, err.Error())
} }
@ -137,7 +138,7 @@ func (cc *CommonController) SendEmail() {
} }
uuid, err := dao.GenerateRandomString() uuid, err := dao.GenerateRandomString()
if err != nil { if err != nil {
beego.Error("Error occurred in GenerateRandomString:", err) log.Errorf("Error occurred in GenerateRandomString: %v", err)
cc.CustomAbort(http.StatusInternalServerError, "Internal error.") cc.CustomAbort(http.StatusInternalServerError, "Internal error.")
} }
err = messageTemplate.Execute(message, messageDetail{ err = messageTemplate.Execute(message, messageDetail{
@ -147,13 +148,13 @@ func (cc *CommonController) SendEmail() {
}) })
if err != nil { if err != nil {
beego.Error("message template error:", err) log.Errorf("Message template error: %v", err)
cc.CustomAbort(http.StatusInternalServerError, "internal_error") cc.CustomAbort(http.StatusInternalServerError, "internal_error")
} }
config, err := beego.AppConfig.GetSection("mail") config, err := beego.AppConfig.GetSection("mail")
if err != nil { if err != nil {
beego.Error("Can not load app.conf:", err) log.Errorf("Can not load app.conf: %v", err)
cc.CustomAbort(http.StatusInternalServerError, "internal_error") cc.CustomAbort(http.StatusInternalServerError, "internal_error")
} }
@ -166,7 +167,7 @@ func (cc *CommonController) SendEmail() {
err = mail.SendMail() err = mail.SendMail()
if err != nil { if err != nil {
beego.Error("send email failed:", err) log.Errorf("Send email failed: %v", err)
cc.CustomAbort(http.StatusInternalServerError, "send_email_failed") cc.CustomAbort(http.StatusInternalServerError, "send_email_failed")
} }
@ -187,7 +188,7 @@ func (rpc *ResetPasswordController) Get() {
resetUUID := rpc.GetString("reset_uuid") resetUUID := rpc.GetString("reset_uuid")
if resetUUID == "" { if resetUUID == "" {
beego.Error("Reset uuid is blank.") log.Error("Reset uuid is blank.")
rpc.Redirect("/", http.StatusFound) rpc.Redirect("/", http.StatusFound)
return return
} }
@ -195,7 +196,7 @@ func (rpc *ResetPasswordController) Get() {
queryUser := models.User{ResetUUID: resetUUID} queryUser := models.User{ResetUUID: resetUUID}
user, err := dao.GetUser(queryUser) user, err := dao.GetUser(queryUser)
if err != nil { if err != nil {
beego.Error("Error occurred in GetUser:", err) log.Errorf("Error occurred in GetUser: %v", err)
rpc.CustomAbort(http.StatusInternalServerError, "Internal error.") rpc.CustomAbort(http.StatusInternalServerError, "Internal error.")
} }
@ -218,11 +219,11 @@ func (cc *CommonController) ResetPassword() {
queryUser := models.User{ResetUUID: resetUUID} queryUser := models.User{ResetUUID: resetUUID}
user, err := dao.GetUser(queryUser) user, err := dao.GetUser(queryUser)
if err != nil { if err != nil {
beego.Error("Error occurred in GetUser:", err) log.Errorf("Error occurred in GetUser: %v", err)
cc.CustomAbort(http.StatusInternalServerError, "Internal error.") cc.CustomAbort(http.StatusInternalServerError, "Internal error.")
} }
if user == nil { if user == nil {
beego.Error("User does not exist") log.Error("User does not exist")
cc.CustomAbort(http.StatusBadRequest, "User does not exist") cc.CustomAbort(http.StatusBadRequest, "User does not exist")
} }
@ -232,7 +233,7 @@ func (cc *CommonController) ResetPassword() {
user.Password = password user.Password = password
err = dao.ResetUserPassword(*user) err = dao.ResetUserPassword(*user)
if err != nil { if err != nil {
beego.Error("Error occurred in ResetUserPassword:", err) log.Errorf("Error occurred in ResetUserPassword: %v", err)
cc.CustomAbort(http.StatusInternalServerError, "Internal error.") cc.CustomAbort(http.StatusInternalServerError, "Internal error.")
} }
} else { } else {

View File

@ -22,8 +22,7 @@ import (
"github.com/vmware/harbor/dao" "github.com/vmware/harbor/dao"
"github.com/vmware/harbor/models" "github.com/vmware/harbor/models"
"github.com/vmware/harbor/utils/log"
"github.com/astaxie/beego"
) )
// RegisterController handles request to /register // RegisterController handles request to /register
@ -53,7 +52,7 @@ func (rc *CommonController) SignUp() {
_, err := dao.Register(user) _, err := dao.Register(user)
if err != nil { if err != nil {
beego.Error("Error occurred in Register:", err) log.Errorf("Error occurred in Register: %v", err)
rc.CustomAbort(http.StatusInternalServerError, "Internal error.") rc.CustomAbort(http.StatusInternalServerError, "Internal error.")
} }
} }
@ -73,7 +72,7 @@ func (rc *CommonController) UserExists() {
exist, err := dao.UserExists(user, target) exist, err := dao.UserExists(user, target)
if err != nil { if err != nil {
beego.Error("Error occurred in UserExists:", err) log.Errorf("Error occurred in UserExists: %v", err)
rc.CustomAbort(http.StatusInternalServerError, "Internal error.") rc.CustomAbort(http.StatusInternalServerError, "Internal error.")
} }
rc.Data["json"] = exist rc.Data["json"] = exist