Merge pull request #46 from redkafei/master

Update new logger for the files under package "service"
This commit is contained in:
Daniel Jiang 2016-03-28 14:10:19 +08:00
commit 4a2a8b6ad3
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/dao"
"github.com/vmware/harbor/models" "github.com/vmware/harbor/models"
svc_utils "github.com/vmware/harbor/service/utils" svc_utils "github.com/vmware/harbor/service/utils"
"github.com/vmware/harbor/utils/log"
"github.com/astaxie/beego" "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. // Post handles POST request, and records audit log or refreshes cache based on event.
func (n *NotificationHandler) Post() { func (n *NotificationHandler) Post() {
var notification models.Notification var notification models.Notification
// log.Printf("Notification Handler triggered!\n") // log.Info("Notification Handler triggered!\n")
// log.Printf("request body in string: %s", string(n.Ctx.Input.CopyBody())) // log.Infof("request body in string: %s", string(n.Ctx.Input.CopyBody()))
err := json.Unmarshal(n.Ctx.Input.CopyBody(1<<32), &notification) err := json.Unmarshal(n.Ctx.Input.CopyBody(1<<32), &notification)
if err != nil { if err != nil {
beego.Error("error while decoding json: ", err) log.Errorf("error while decoding json: %v", err)
return return
} }
var username, action, repo, project string var username, action, repo, project string
@ -50,7 +51,7 @@ func (n *NotificationHandler) Post() {
for _, e := range notification.Events { for _, e := range notification.Events {
matched, err = regexp.MatchString(manifestPattern, e.Target.MediaType) matched, err = regexp.MatchString(manifestPattern, e.Target.MediaType)
if err != nil { 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 matched = false
} }
if matched && strings.HasPrefix(e.Request.UserAgent, "docker") { if matched && strings.HasPrefix(e.Request.UserAgent, "docker") {
@ -68,7 +69,7 @@ func (n *NotificationHandler) Post() {
go func() { go func() {
err2 := svc_utils.RefreshCatalogCache() err2 := svc_utils.RefreshCatalogCache()
if err2 != nil { if err2 != nil {
beego.Error("Error happens when refreshing cache:", err2) log.Errorf("Error happens when refreshing cache: %v", err2)
} }
}() }()
} }

View File

@ -16,12 +16,12 @@
package service package service
import ( import (
"log"
"net/http" "net/http"
"github.com/vmware/harbor/auth" "github.com/vmware/harbor/auth"
"github.com/vmware/harbor/models" "github.com/vmware/harbor/models"
svc_utils "github.com/vmware/harbor/service/utils" svc_utils "github.com/vmware/harbor/service/utils"
"github.com/vmware/harbor/utils/log"
"github.com/astaxie/beego" "github.com/astaxie/beego"
"github.com/docker/distribution/registry/auth/token" "github.com/docker/distribution/registry/auth/token"
@ -38,14 +38,14 @@ type TokenHandler struct {
func (a *TokenHandler) Get() { func (a *TokenHandler) Get() {
request := a.Ctx.Request request := a.Ctx.Request
log.Println("request url: " + request.URL.String()) log.Infof("request url: %v", request.URL.String())
username, password, _ := request.BasicAuth() username, password, _ := request.BasicAuth()
authenticated := authenticate(username, password) authenticated := authenticate(username, password)
service := a.GetString("service") service := a.GetString("service")
scope := a.GetString("scope") scope := a.GetString("scope")
if len(scope) == 0 && !authenticated { if len(scope) == 0 && !authenticated {
log.Printf("login request with invalid credentials") log.Info("login request with invalid credentials")
a.CustomAbort(http.StatusUnauthorized, "") a.CustomAbort(http.StatusUnauthorized, "")
} }
access := svc_utils.GetResourceActions(scope) access := svc_utils.GetResourceActions(scope)
@ -60,7 +60,7 @@ func (a *TokenHandler) serveToken(username, service string, access []*token.Reso
//create token //create token
rawToken, err := svc_utils.MakeToken(username, service, access) rawToken, err := svc_utils.MakeToken(username, service, access)
if err != nil { 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) writer.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -73,7 +73,7 @@ func (a *TokenHandler) serveToken(username, service string, access []*token.Reso
func authenticate(principal, password string) bool { func authenticate(principal, password string) bool {
user, err := auth.Login(models.AuthModel{principal, password}) user, err := auth.Login(models.AuthModel{principal, password})
if err != nil { if err != nil {
log.Printf("Error occurred in UserLogin: %v", err) log.Errorf("Error occurred in UserLogin: %v", err)
return false return false
} }
if user == nil { if user == nil {

View File

@ -21,11 +21,11 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"strings" "strings"
"time" "time"
"github.com/vmware/harbor/dao" "github.com/vmware/harbor/dao"
"github.com/vmware/harbor/utils/log"
"github.com/docker/distribution/registry/auth/token" "github.com/docker/distribution/registry/auth/token"
"github.com/docker/libtrust" "github.com/docker/libtrust"
@ -71,19 +71,19 @@ func FilterAccess(username string, authenticated bool, a *token.ResourceActions)
if username == "admin" { if username == "admin" {
exist, err := dao.ProjectExists(projectName) exist, err := dao.ProjectExists(projectName)
if err != nil { if err != nil {
log.Printf("Error occurred in CheckExistProject: %v", err) log.Errorf("Error occurred in CheckExistProject: %v", err)
return return
} }
if exist { if exist {
permission = "RW" permission = "RW"
} else { } else {
permission = "" 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 { } else {
permission, err = dao.GetPermission(username, projectName) permission, err = dao.GetPermission(username, projectName)
if err != nil { if err != nil {
log.Printf("Error occurred in GetPermission: %v", err) log.Errorf("Error occurred in GetPermission: %v", err)
return 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. // 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" "time"
"github.com/vmware/harbor/models" "github.com/vmware/harbor/models"
"github.com/vmware/harbor/utils/log"
"github.com/astaxie/beego"
"github.com/astaxie/beego/cache" "github.com/astaxie/beego/cache"
) )
@ -35,7 +34,7 @@ func init() {
var err error var err error
Cache, err = cache.NewCache("memory", `{"interval":720}`) Cache, err = cache.NewCache("memory", `{"interval":720}`)
if err != nil { if err != nil {
beego.Error("Failed to initialize cache, error:", err) log.Errorf("Failed to initialize cache, error:%v", err)
} }
} }

View File

@ -19,10 +19,11 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"os" "os"
"strings" "strings"
"github.com/vmware/harbor/utils/log"
) )
// BuildRegistryURL ... // BuildRegistryURL ...
@ -34,7 +35,7 @@ func BuildRegistryURL(segments ...string) string {
url := registryURL + "/v2" url := registryURL + "/v2"
for _, s := range segments { for _, s := range segments {
if s == "v2" { if s == "v2" {
log.Printf("unnecessary v2 in %v", segments) log.Debugf("unnecessary v2 in %v", segments)
continue continue
} }
url += "/" + s url += "/" + s
@ -59,7 +60,7 @@ func RegistryAPIGet(url, username string) ([]byte, error) {
} else if response.StatusCode == http.StatusUnauthorized { } else if response.StatusCode == http.StatusUnauthorized {
authenticate := response.Header.Get("WWW-Authenticate") authenticate := response.Header.Get("WWW-Authenticate")
str := strings.Split(authenticate, " ")[1] str := strings.Split(authenticate, " ")[1]
log.Println("url: " + url) log.Debugf("url: %s", url)
var service string var service string
var scope string var scope string
strs := strings.Split(str, ",") strs := strings.Split(str, ",")
@ -83,7 +84,7 @@ func RegistryAPIGet(url, username string) ([]byte, error) {
request.Header.Add("Authorization", "Bearer "+token) request.Header.Add("Authorization", "Bearer "+token)
client := &http.Client{} client := &http.Client{}
client.CheckRedirect = func(req *http.Request, via []*http.Request) error { 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 { if len(via) >= 10 {
return fmt.Errorf("too many redirects") return fmt.Errorf("too many redirects")
} }
@ -100,7 +101,7 @@ func RegistryAPIGet(url, username string) ([]byte, error) {
} }
if response.StatusCode != http.StatusOK { if response.StatusCode != http.StatusOK {
errMsg := fmt.Sprintf("Unexpected return code from registry: %d", response.StatusCode) errMsg := fmt.Sprintf("Unexpected return code from registry: %d", response.StatusCode)
log.Printf(errMsg) log.Error(errMsg)
return nil, fmt.Errorf(errMsg) return nil, fmt.Errorf(errMsg)
} }
result, err = ioutil.ReadAll(response.Body) result, err = ioutil.ReadAll(response.Body)