remove the global log error/warn etc functions (#41404)

* remove the global log error/warn etc functions and use request context logger whenever possible
This commit is contained in:
ying-jeanne
2021-11-08 17:56:56 +01:00
committed by GitHub
parent bda60f3458
commit 54de1078c8
28 changed files with 88 additions and 93 deletions

View File

@@ -17,6 +17,7 @@ import (
)
var pluginProxyTransport *http.Transport
var applog = log.New("app.routes")
func (hs *HTTPServer) initAppPluginRoutes(r *web.Mux) {
pluginProxyTransport = &http.Transport{
@@ -51,7 +52,8 @@ func (hs *HTTPServer) initAppPluginRoutes(r *web.Mux) {
for _, method := range strings.Split(route.Method, ",") {
r.Handle(strings.TrimSpace(method), url, handlers)
}
log.Debug("Plugins: Adding proxy route", "url", url)
applog.Debug("Plugins: Adding proxy route", "url", url)
}
}
}

View File

@@ -40,6 +40,8 @@ type Avatar struct {
timestamp time.Time
}
var alog = log.New("avatar")
func New(hash string) *Avatar {
return &Avatar{
hash: hash,
@@ -95,7 +97,7 @@ func (a *CacheServer) Handler(ctx *models.ReqContext) {
if avatar.Expired() {
// The cache item is either expired or newly created, update it from the server
if err := avatar.Update(); err != nil {
log.Debug("avatar update", "err", err)
ctx.Logger.Debug("avatar update", "err", err)
avatar = a.notFound
}
}
@@ -104,7 +106,7 @@ func (a *CacheServer) Handler(ctx *models.ReqContext) {
avatar = a.notFound
} else if !exists {
if err := a.cache.Add(hash, avatar, gocache.DefaultExpiration); err != nil {
log.Debug("add avatar to cache", "err", err)
ctx.Logger.Debug("add avatar to cache", "err", err)
}
}
@@ -117,7 +119,7 @@ func (a *CacheServer) Handler(ctx *models.ReqContext) {
ctx.Resp.Header().Set("Cache-Control", "private, max-age=3600")
if err := avatar.Encode(ctx.Resp); err != nil {
log.Warn("avatar encode error:", "err", err)
ctx.Logger.Warn("avatar encode error:", "err", err)
ctx.Resp.WriteHeader(500)
}
}
@@ -142,7 +144,7 @@ func newNotFound(cfg *setting.Cfg) *Avatar {
// variable.
// nolint:gosec
if data, err := ioutil.ReadFile(path); err != nil {
log.Error("Failed to read user_profile.png", "path", path)
alog.Error("Failed to read user_profile.png", "path", path)
} else {
avatar.data = bytes.NewBuffer(data)
}
@@ -215,7 +217,7 @@ var client = &http.Client{
func (a *thunderTask) fetch() error {
a.Avatar.timestamp = time.Now()
log.Debug("avatar.fetch(fetch new avatar)", "url", a.Url)
alog.Debug("avatar.fetch(fetch new avatar)", "url", a.Url)
req, err := http.NewRequest("GET", a.Url, nil)
if err != nil {
return err
@@ -232,7 +234,7 @@ func (a *thunderTask) fetch() error {
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Warn("Failed to close response body", "err", err)
alog.Warn("Failed to close response body", "err", err)
}
}()

View File

@@ -13,6 +13,7 @@ import (
)
var regNonAlphaNumeric = regexp.MustCompile("[^a-zA-Z0-9]+")
var mlog = log.New("models")
type AnyId struct {
Id int64 `json:"id"`
@@ -65,7 +66,7 @@ func GetGravatarUrl(text string) string {
hasher := md5.New()
if _, err := hasher.Write([]byte(strings.ToLower(text))); err != nil {
log.Warn("Failed to hash text", "err", err)
mlog.Warn("Failed to hash text", "err", err)
}
return fmt.Sprintf(setting.AppSubUrl+"/avatar/%x", hasher.Sum(nil))
}

View File

@@ -7,7 +7,6 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/services/accesscontrol"
@@ -64,7 +63,7 @@ func (hs *HTTPServer) getFSDataSources(c *models.ReqContext, enabledPlugins Enab
meta, exists := enabledPlugins.Get(plugins.DataSource, ds.Type)
if !exists {
log.Error("Could not find plugin definition for data source", "datasource_type", ds.Type)
c.Logger.Error("Could not find plugin definition for data source", "datasource_type", ds.Type)
continue
}
dsMap["preload"] = meta.Preload

View File

@@ -11,7 +11,6 @@ import (
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/infra/network"
"github.com/grafana/grafana/pkg/login"
@@ -131,7 +130,7 @@ func (hs *HTTPServer) LoginView(c *models.ReqContext) {
if err := hs.ValidateRedirectTo(redirectTo); err != nil {
// the user is already logged so instead of rendering the login page with error
// it should be redirected to the home page.
log.Debug("Ignored invalid redirect_to cookie value", "redirect_to", redirectTo)
c.Logger.Debug("Ignored invalid redirect_to cookie value", "redirect_to", redirectTo)
redirectTo = hs.Cfg.AppSubURL + "/"
}
cookies.DeleteCookie(c.Resp, "redirect_to", hs.CookieOptionsFromCfg)
@@ -152,12 +151,12 @@ func (hs *HTTPServer) tryOAuthAutoLogin(c *models.ReqContext) bool {
}
oauthInfos := hs.SocialService.GetOAuthInfoProviders()
if len(oauthInfos) != 1 {
log.Warn("Skipping OAuth auto login because multiple OAuth providers are configured")
c.Logger.Warn("Skipping OAuth auto login because multiple OAuth providers are configured")
return false
}
for key := range oauthInfos {
redirectUrl := hs.Cfg.AppSubURL + "/login/" + key
log.Info("OAuth auto login enabled. Redirecting to " + redirectUrl)
c.Logger.Info("OAuth auto login enabled. Redirecting to " + redirectUrl)
c.Redirect(redirectUrl, 307)
return true
}
@@ -249,7 +248,7 @@ func (hs *HTTPServer) LoginPost(c *models.ReqContext) response.Response {
if err := hs.ValidateRedirectTo(redirectTo); err == nil {
result["redirectUrl"] = redirectTo
} else {
log.Info("Ignored invalid redirect_to cookie value.", "url", redirectTo)
c.Logger.Info("Ignored invalid redirect_to cookie value.", "url", redirectTo)
}
cookies.DeleteCookie(c.Resp, "redirect_to", hs.CookieOptionsFromCfg)
}

View File

@@ -256,7 +256,7 @@ func (hs *HTTPServer) OAuthLogin(ctx *models.ReqContext) {
ctx.Redirect(redirectTo)
return
}
log.Debug("Ignored invalid redirect_to cookie value", "redirect_to", redirectTo)
ctx.Logger.Debug("Ignored invalid redirect_to cookie value", "redirect_to", redirectTo)
}
ctx.Redirect(setting.AppSubUrl + "/")