From 9872041f12cd123ecbe3cf9893607d870cba4f8e Mon Sep 17 00:00:00 2001 From: JessonChan Date: Fri, 11 Mar 2016 14:14:58 +0800 Subject: [PATCH 1/2] timeDur is used only when need --- router.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/router.go b/router.go index d0bf534f..e767bec5 100644 --- a/router.go +++ b/router.go @@ -802,9 +802,9 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) p.execFilter(context, FinishRouter, urlPath) Admin: - timeDur := time.Since(startTime) //admin module record QPS if BConfig.Listen.EnableAdmin { + timeDur := time.Since(startTime) if FilterMonitorFunc(r.Method, r.URL.Path, timeDur) { if runRouter != nil { go toolbox.StatisticsMap.AddStatistics(r.Method, r.URL.Path, runRouter.Name(), timeDur) @@ -815,6 +815,7 @@ Admin: } if BConfig.RunMode == DEV || BConfig.Log.AccessLogs { + timeDur := time.Since(startTime) var devInfo string if findRouter { if routerInfo != nil { From c92c3fc8b512b1a9ae6107a6351ba076d5acdba5 Mon Sep 17 00:00:00 2001 From: JessonChan Date: Fri, 11 Mar 2016 14:45:39 +0800 Subject: [PATCH 2/2] make the BeegoLogger a adapter of http server ErrorLog --- app.go | 2 ++ logs/log.go | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app.go b/app.go index af54ea4b..a065670a 100644 --- a/app.go +++ b/app.go @@ -16,6 +16,7 @@ package beego import ( "fmt" + "log" "net" "net/http" "net/http/fcgi" @@ -95,6 +96,7 @@ func (app *App) Run() { app.Server.Handler = app.Handlers app.Server.ReadTimeout = time.Duration(BConfig.Listen.ServerTimeOut) * time.Second app.Server.WriteTimeout = time.Duration(BConfig.Listen.ServerTimeOut) * time.Second + app.Server.ErrorLog = log.New(BeeLogger, "", 0) // run graceful mode if BConfig.Listen.Graceful { diff --git a/logs/log.go b/logs/log.go index a5982e45..583b35e0 100644 --- a/logs/log.go +++ b/logs/log.go @@ -196,6 +196,22 @@ func (bl *BeeLogger) writeToLoggers(when time.Time, msg string, level int) { } } +func (bl *BeeLogger) Write(p []byte) (n int, err error) { + if len(p) == 0 { + return 0, nil + } + // writeMsg will always add a '\n' character + if p[len(p)-1] == '\n' { + p = p[0 : len(p)-1] + } + // set LevelCritical to ensure all log message will be write out + err = bl.writeMsg(LevelCritical, string(p)) + if err == nil { + return len(p), err + } + return 0, err +} + func (bl *BeeLogger) writeMsg(logLevel int, msg string) error { when := time.Now() if bl.enableFuncCallDepth { @@ -205,7 +221,7 @@ func (bl *BeeLogger) writeMsg(logLevel int, msg string) error { line = 0 } _, filename := path.Split(file) - msg = "[" + filename + ":" + strconv.FormatInt(int64(line), 10) + "]" + msg + msg = "[" + filename + ":" + strconv.FormatInt(int64(line), 10) + "] " + msg } if bl.asynchronous { lm := logMsgPool.Get().(*logMsg)