grafana/pkg/api/frontend_logging.go
ying-jeanne a8eef45a44
Logger migration from log15 to gokit/log (#41636)
* migrate log15 to gokit/log

* fix console log

* update some unittest

* fix all unittest

* fix the build

* Update pkg/infra/log/log.go

Co-authored-by: Yuriy Tseretyan <tceretian@gmail.com>

* general type vector

* correct the level key

Co-authored-by: Yuriy Tseretyan <tceretian@gmail.com>
2022-01-06 22:28:05 +08:00

49 lines
1.3 KiB
Go

package api
import (
"net/http"
"github.com/getsentry/sentry-go"
"github.com/grafana/grafana/pkg/api/frontendlogging"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/web"
)
var frontendLogger = log.New("frontend")
type frontendLogMessageHandler func(c *models.ReqContext) response.Response
func NewFrontendLogMessageHandler(store *frontendlogging.SourceMapStore) frontendLogMessageHandler {
return func(c *models.ReqContext) response.Response {
event := frontendlogging.FrontendSentryEvent{}
if err := web.Bind(c.Req, &event); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
var msg = "unknown"
if len(event.Message) > 0 {
msg = event.Message
} else if event.Exception != nil && len(event.Exception.Values) > 0 {
msg = event.Exception.Values[0].FmtMessage()
}
var ctx = event.ToLogContext(store)
switch event.Level {
case sentry.LevelError:
frontendLogger.Error(msg, ctx...)
case sentry.LevelWarning:
frontendLogger.Warn(msg, ctx...)
case sentry.LevelDebug:
frontendLogger.Debug(msg, ctx...)
default:
frontendLogger.Info(msg, ctx...)
}
return response.Success("ok")
}
}