diff --git a/service/notification.go b/service/notification.go index 7da62cb2d..7f35f8b67 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" + "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.Errorf("error while decoding json: %v", 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.Errorf("Failed to match the media type against pattern, error: %v", 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.Errorf("Error happens when refreshing cache: %v", err2) } }() } diff --git a/service/token.go b/service/token.go index fe013be33..b274be3f8 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" + "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.Infof("request url: %v", 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..4c00dd579 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" + "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\n", 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..81ec5a52b 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" + "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.Errorf("Failed to initialize cache, error:%v", err) } } diff --git a/service/utils/registryutils.go b/service/utils/registryutils.go index b1aa272d1..97c4b53f8 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" + + "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.Debugf("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.Debugf("url: %s", 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)