feat(logging): a lot of progress on moving to new logging lib, #4590

This commit is contained in:
Torkel Ödegaard
2016-06-06 23:06:44 +02:00
parent 25899b72d2
commit 22778e6efd
18 changed files with 314 additions and 707 deletions
+5 -11
View File
@@ -16,11 +16,9 @@
package middleware
import (
"fmt"
"net/http"
"time"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/setting"
"gopkg.in/macaron.v1"
@@ -40,25 +38,21 @@ func Logger() macaron.Handler {
c.Next()
timeTakenMs := time.Since(start) / time.Millisecond
content := fmt.Sprintf("Completed %s %s \"%s %s %s\" %v %s %d bytes in %dms", c.RemoteAddr(), uname, req.Method, req.URL.Path, req.Proto, rw.Status(), http.StatusText(rw.Status()), rw.Size(), timeTakenMs)
if timer, ok := c.Data["perfmon.timer"]; ok {
timerTyped := timer.(metrics.Timer)
timerTyped.Update(timeTakenMs)
}
switch rw.Status() {
case 200, 304:
content = fmt.Sprintf("%s", content)
status := rw.Status()
if status == 200 || status == 304 {
if !setting.RouterLogging {
return
}
case 404:
content = fmt.Sprintf("%s", content)
case 500:
content = fmt.Sprintf("%s", content)
}
log.Info(content)
if ctx, ok := c.Data["ctx"]; ok {
ctx.(*Context).Logger.Info("Request Completed", "method", req.Method, "path", req.URL.Path, "status", status, "remote_addr", c.RemoteAddr(), "uname", uname, "time_ms", timeTakenMs, "size", rw.Size())
}
}
}
+8
View File
@@ -1,6 +1,7 @@
package middleware
import (
"fmt"
"strconv"
"strings"
@@ -23,6 +24,7 @@ type Context struct {
IsSignedIn bool
AllowAnonymous bool
Logger log.Logger
}
func GetContextHandler() macaron.Handler {
@@ -33,6 +35,7 @@ func GetContextHandler() macaron.Handler {
Session: GetSession(),
IsSignedIn: false,
AllowAnonymous: false,
Logger: log.New("context"),
}
// the order in which these are tested are important
@@ -48,6 +51,11 @@ func GetContextHandler() macaron.Handler {
initContextWithAnonymousUser(ctx) {
}
ctx.Logger = log.New("context", "user", ctx.UserId, "orgId", ctx.OrgId)
// set ctx in data array on the original context
c.Data["ctx"] = ctx
fmt.Printf("c: %v\n", c)
c.Map(ctx)
}
}