update log functions in service

This commit is contained in:
Jack Liu 2016-03-25 09:08:44 +08:00
parent 82899e9731
commit 44b27e71c2
5 changed files with 24 additions and 23 deletions

View File

@ -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), &notification)
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)
}
}()
}

View File

@ -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 {

View File

@ -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.

View File

@ -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)
}
}

View File

@ -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)