From 44b27e71c2c2d3309bea614f6cfe88c196645c87 Mon Sep 17 00:00:00 2001 From: Jack Liu Date: Fri, 25 Mar 2016 09:08:44 +0800 Subject: [PATCH 1/3] update log functions in service --- service/notification.go | 11 ++++++----- service/token.go | 10 +++++----- service/utils/authutils.go | 10 +++++----- service/utils/cache.go | 5 ++--- service/utils/registryutils.go | 11 ++++++----- 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/service/notification.go b/service/notification.go index 7da62cb2d..75d8ce382 100644 --- a/service/notification.go +++ b/service/notification.go @@ -23,6 +23,7 @@ import ( "github.com/vmware/harbor/dao" "github.com/vmware/harbor/models" svc_utils "github.com/vmware/harbor/service/utils" + log "github.com/vmware/harbor/utils/log" "github.com/astaxie/beego" ) @@ -37,12 +38,12 @@ const manifestPattern = `^application/vnd.docker.distribution.manifest.v\d\+json // Post handles POST request, and records audit log or refreshes cache based on event. func (n *NotificationHandler) Post() { var notification models.Notification - // log.Printf("Notification Handler triggered!\n") - // log.Printf("request body in string: %s", string(n.Ctx.Input.CopyBody())) + // log.Info("Notification Handler triggered!\n") + // log.Infof("request body in string: %s", string(n.Ctx.Input.CopyBody())) err := json.Unmarshal(n.Ctx.Input.CopyBody(1<<32), ¬ification) if err != nil { - beego.Error("error while decoding json: ", err) + log.Error("error while decoding json: ", err) return } var username, action, repo, project string @@ -50,7 +51,7 @@ func (n *NotificationHandler) Post() { for _, e := range notification.Events { matched, err = regexp.MatchString(manifestPattern, e.Target.MediaType) if err != nil { - beego.Error("Failed to match the media type against pattern, error: ", err) + log.Error("Failed to match the media type against pattern, error: ", err) matched = false } if matched && strings.HasPrefix(e.Request.UserAgent, "docker") { @@ -68,7 +69,7 @@ func (n *NotificationHandler) Post() { go func() { err2 := svc_utils.RefreshCatalogCache() if err2 != nil { - beego.Error("Error happens when refreshing cache:", err2) + log.Error("Error happens when refreshing cache:", err2) } }() } diff --git a/service/token.go b/service/token.go index fe013be33..483583b4e 100644 --- a/service/token.go +++ b/service/token.go @@ -16,12 +16,12 @@ package service import ( - "log" "net/http" "github.com/vmware/harbor/auth" "github.com/vmware/harbor/models" svc_utils "github.com/vmware/harbor/service/utils" + log "github.com/vmware/harbor/utils/log" "github.com/astaxie/beego" "github.com/docker/distribution/registry/auth/token" @@ -38,14 +38,14 @@ type TokenHandler struct { func (a *TokenHandler) Get() { request := a.Ctx.Request - log.Println("request url: " + request.URL.String()) + log.Info("request url: " + request.URL.String()) username, password, _ := request.BasicAuth() authenticated := authenticate(username, password) service := a.GetString("service") scope := a.GetString("scope") if len(scope) == 0 && !authenticated { - log.Printf("login request with invalid credentials") + log.Info("login request with invalid credentials") a.CustomAbort(http.StatusUnauthorized, "") } access := svc_utils.GetResourceActions(scope) @@ -60,7 +60,7 @@ func (a *TokenHandler) serveToken(username, service string, access []*token.Reso //create token rawToken, err := svc_utils.MakeToken(username, service, access) if err != nil { - log.Printf("Failed to make token, error: %v", err) + log.Errorf("Failed to make token, error: %v", err) writer.WriteHeader(http.StatusInternalServerError) return } @@ -73,7 +73,7 @@ func (a *TokenHandler) serveToken(username, service string, access []*token.Reso func authenticate(principal, password string) bool { user, err := auth.Login(models.AuthModel{principal, password}) if err != nil { - log.Printf("Error occurred in UserLogin: %v", err) + log.Errorf("Error occurred in UserLogin: %v", err) return false } if user == nil { diff --git a/service/utils/authutils.go b/service/utils/authutils.go index 77ba8a738..df996524e 100644 --- a/service/utils/authutils.go +++ b/service/utils/authutils.go @@ -21,11 +21,11 @@ import ( "encoding/base64" "encoding/json" "fmt" - "log" "strings" "time" "github.com/vmware/harbor/dao" + log "github.com/vmware/harbor/utils/log" "github.com/docker/distribution/registry/auth/token" "github.com/docker/libtrust" @@ -71,19 +71,19 @@ func FilterAccess(username string, authenticated bool, a *token.ResourceActions) if username == "admin" { exist, err := dao.ProjectExists(projectName) if err != nil { - log.Printf("Error occurred in CheckExistProject: %v", err) + log.Errorf("Error occurred in CheckExistProject: %v", err) return } if exist { permission = "RW" } else { permission = "" - log.Printf("project %s does not exist, set empty permission for admin", projectName) + log.Infof("project %s does not exist, set empty permission for admin", projectName) } } else { permission, err = dao.GetPermission(username, projectName) if err != nil { - log.Printf("Error occurred in GetPermission: %v", err) + log.Errorf("Error occurred in GetPermission: %v", err) return } } @@ -96,7 +96,7 @@ func FilterAccess(username string, authenticated bool, a *token.ResourceActions) } } } - log.Printf("current access, type: %s, name:%s, actions:%v \n", a.Type, a.Name, a.Actions) + log.Infof("current access, type: %s, name:%s, actions:%v \n", a.Type, a.Name, a.Actions) } // GenTokenForUI is for the UI process to call, so it won't establish a https connection from UI to proxy. diff --git a/service/utils/cache.go b/service/utils/cache.go index 7a0ca93a4..0180cbfdd 100644 --- a/service/utils/cache.go +++ b/service/utils/cache.go @@ -20,8 +20,7 @@ import ( "time" "github.com/vmware/harbor/models" - - "github.com/astaxie/beego" + log "github.com/vmware/harbor/utils/log" "github.com/astaxie/beego/cache" ) @@ -35,7 +34,7 @@ func init() { var err error Cache, err = cache.NewCache("memory", `{"interval":720}`) if err != nil { - beego.Error("Failed to initialize cache, error:", err) + log.Error("Failed to initialize cache, error:", err) } } diff --git a/service/utils/registryutils.go b/service/utils/registryutils.go index b1aa272d1..ed3507f38 100644 --- a/service/utils/registryutils.go +++ b/service/utils/registryutils.go @@ -19,10 +19,11 @@ import ( "errors" "fmt" "io/ioutil" - "log" "net/http" "os" "strings" + + log "github.com/vmware/harbor/utils/log" ) // BuildRegistryURL ... @@ -34,7 +35,7 @@ func BuildRegistryURL(segments ...string) string { url := registryURL + "/v2" for _, s := range segments { if s == "v2" { - log.Printf("unnecessary v2 in %v", segments) + log.Infof("unnecessary v2 in %v", segments) continue } url += "/" + s @@ -59,7 +60,7 @@ func RegistryAPIGet(url, username string) ([]byte, error) { } else if response.StatusCode == http.StatusUnauthorized { authenticate := response.Header.Get("WWW-Authenticate") str := strings.Split(authenticate, " ")[1] - log.Println("url: " + url) + log.Info("url: " + url) var service string var scope string strs := strings.Split(str, ",") @@ -83,7 +84,7 @@ func RegistryAPIGet(url, username string) ([]byte, error) { request.Header.Add("Authorization", "Bearer "+token) client := &http.Client{} client.CheckRedirect = func(req *http.Request, via []*http.Request) error { - // log.Printf("via length: %d\n", len(via)) + // log.Infof("via length: %d\n", len(via)) if len(via) >= 10 { return fmt.Errorf("too many redirects") } @@ -100,7 +101,7 @@ func RegistryAPIGet(url, username string) ([]byte, error) { } if response.StatusCode != http.StatusOK { errMsg := fmt.Sprintf("Unexpected return code from registry: %d", response.StatusCode) - log.Printf(errMsg) + log.Error(errMsg) return nil, fmt.Errorf(errMsg) } result, err = ioutil.ReadAll(response.Body) From aa9fa581ecb30710811367fd3e98de0c6b3ee032 Mon Sep 17 00:00:00 2001 From: Jack Liu Date: Fri, 25 Mar 2016 09:31:50 +0800 Subject: [PATCH 2/3] fix bugs --- service/notification.go | 2 +- service/token.go | 4 ++-- service/utils/authutils.go | 2 +- service/utils/cache.go | 2 +- service/utils/registryutils.go | 6 +++--- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/service/notification.go b/service/notification.go index 75d8ce382..9e9cdd97f 100644 --- a/service/notification.go +++ b/service/notification.go @@ -23,7 +23,7 @@ import ( "github.com/vmware/harbor/dao" "github.com/vmware/harbor/models" svc_utils "github.com/vmware/harbor/service/utils" - log "github.com/vmware/harbor/utils/log" + "github.com/vmware/harbor/utils/log" "github.com/astaxie/beego" ) diff --git a/service/token.go b/service/token.go index 483583b4e..d30f44e9a 100644 --- a/service/token.go +++ b/service/token.go @@ -21,7 +21,7 @@ import ( "github.com/vmware/harbor/auth" "github.com/vmware/harbor/models" svc_utils "github.com/vmware/harbor/service/utils" - log "github.com/vmware/harbor/utils/log" + "github.com/vmware/harbor/utils/log" "github.com/astaxie/beego" "github.com/docker/distribution/registry/auth/token" @@ -38,7 +38,7 @@ type TokenHandler struct { func (a *TokenHandler) Get() { request := a.Ctx.Request - log.Info("request url: " + request.URL.String()) + log.Infof("request url: " + request.URL.String()) username, password, _ := request.BasicAuth() authenticated := authenticate(username, password) service := a.GetString("service") diff --git a/service/utils/authutils.go b/service/utils/authutils.go index df996524e..d8abc917b 100644 --- a/service/utils/authutils.go +++ b/service/utils/authutils.go @@ -25,7 +25,7 @@ import ( "time" "github.com/vmware/harbor/dao" - log "github.com/vmware/harbor/utils/log" + "github.com/vmware/harbor/utils/log" "github.com/docker/distribution/registry/auth/token" "github.com/docker/libtrust" diff --git a/service/utils/cache.go b/service/utils/cache.go index 0180cbfdd..b44cf7031 100644 --- a/service/utils/cache.go +++ b/service/utils/cache.go @@ -20,7 +20,7 @@ import ( "time" "github.com/vmware/harbor/models" - log "github.com/vmware/harbor/utils/log" + "github.com/vmware/harbor/utils/log" "github.com/astaxie/beego/cache" ) diff --git a/service/utils/registryutils.go b/service/utils/registryutils.go index ed3507f38..153398a3b 100644 --- a/service/utils/registryutils.go +++ b/service/utils/registryutils.go @@ -23,7 +23,7 @@ import ( "os" "strings" - log "github.com/vmware/harbor/utils/log" + "github.com/vmware/harbor/utils/log" ) // BuildRegistryURL ... @@ -35,7 +35,7 @@ func BuildRegistryURL(segments ...string) string { url := registryURL + "/v2" for _, s := range segments { if s == "v2" { - log.Infof("unnecessary v2 in %v", segments) + log.Debugf("unnecessary v2 in %v", segments) continue } url += "/" + s @@ -60,7 +60,7 @@ func RegistryAPIGet(url, username string) ([]byte, error) { } else if response.StatusCode == http.StatusUnauthorized { authenticate := response.Header.Get("WWW-Authenticate") str := strings.Split(authenticate, " ")[1] - log.Info("url: " + url) + log.Debugf("url: " + url) var service string var scope string strs := strings.Split(str, ",") From cea9cca52ae0a03cff1f4eaa2e0143b12815343d Mon Sep 17 00:00:00 2001 From: Jack Liu Date: Sun, 27 Mar 2016 00:18:11 +0800 Subject: [PATCH 3/3] Update to use placeholder --- service/notification.go | 6 +++--- service/token.go | 2 +- service/utils/authutils.go | 2 +- service/utils/cache.go | 2 +- service/utils/registryutils.go | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/service/notification.go b/service/notification.go index 9e9cdd97f..7f35f8b67 100644 --- a/service/notification.go +++ b/service/notification.go @@ -43,7 +43,7 @@ func (n *NotificationHandler) Post() { err := json.Unmarshal(n.Ctx.Input.CopyBody(1<<32), ¬ification) if err != nil { - log.Error("error while decoding json: ", err) + log.Errorf("error while decoding json: %v", err) return } var username, action, repo, project string @@ -51,7 +51,7 @@ func (n *NotificationHandler) Post() { for _, e := range notification.Events { matched, err = regexp.MatchString(manifestPattern, e.Target.MediaType) if err != nil { - log.Error("Failed to match the media type against pattern, error: ", err) + log.Errorf("Failed to match the media type against pattern, error: %v", err) matched = false } if matched && strings.HasPrefix(e.Request.UserAgent, "docker") { @@ -69,7 +69,7 @@ func (n *NotificationHandler) Post() { go func() { err2 := svc_utils.RefreshCatalogCache() if err2 != nil { - log.Error("Error happens when refreshing cache:", err2) + log.Errorf("Error happens when refreshing cache: %v", err2) } }() } diff --git a/service/token.go b/service/token.go index d30f44e9a..b274be3f8 100644 --- a/service/token.go +++ b/service/token.go @@ -38,7 +38,7 @@ type TokenHandler struct { func (a *TokenHandler) Get() { request := a.Ctx.Request - log.Infof("request url: " + request.URL.String()) + log.Infof("request url: %v", request.URL.String()) username, password, _ := request.BasicAuth() authenticated := authenticate(username, password) service := a.GetString("service") diff --git a/service/utils/authutils.go b/service/utils/authutils.go index d8abc917b..4c00dd579 100644 --- a/service/utils/authutils.go +++ b/service/utils/authutils.go @@ -78,7 +78,7 @@ func FilterAccess(username string, authenticated bool, a *token.ResourceActions) permission = "RW" } else { permission = "" - log.Infof("project %s does not exist, set empty permission for admin", projectName) + log.Infof("project %s does not exist, set empty permission for admin\n", projectName) } } else { permission, err = dao.GetPermission(username, projectName) diff --git a/service/utils/cache.go b/service/utils/cache.go index b44cf7031..81ec5a52b 100644 --- a/service/utils/cache.go +++ b/service/utils/cache.go @@ -34,7 +34,7 @@ func init() { var err error Cache, err = cache.NewCache("memory", `{"interval":720}`) if err != nil { - log.Error("Failed to initialize cache, error:", err) + log.Errorf("Failed to initialize cache, error:%v", err) } } diff --git a/service/utils/registryutils.go b/service/utils/registryutils.go index 153398a3b..97c4b53f8 100644 --- a/service/utils/registryutils.go +++ b/service/utils/registryutils.go @@ -60,7 +60,7 @@ func RegistryAPIGet(url, username string) ([]byte, error) { } else if response.StatusCode == http.StatusUnauthorized { authenticate := response.Header.Get("WWW-Authenticate") str := strings.Split(authenticate, " ")[1] - log.Debugf("url: " + url) + log.Debugf("url: %s", url) var service string var scope string strs := strings.Split(str, ",")