2017-06-05 18:21:06 +00:00
|
|
|
package main
|
|
|
|
|
2017-06-10 18:45:30 +00:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2017-06-05 18:21:06 +00:00
|
|
|
|
2017-06-10 18:45:30 +00:00
|
|
|
"github.com/aerogo/aero"
|
|
|
|
"github.com/aerogo/log"
|
|
|
|
)
|
2017-06-05 18:21:06 +00:00
|
|
|
|
2017-06-10 18:45:30 +00:00
|
|
|
func init() {
|
|
|
|
err := log.NewChannel("error")
|
|
|
|
err.AddOutput(log.File("logs/error.log"))
|
|
|
|
err.AddOutput(os.Stderr)
|
2017-06-05 18:21:06 +00:00
|
|
|
|
2017-06-10 18:45:30 +00:00
|
|
|
web := log.NewChannel("web")
|
|
|
|
web.AddOutput(log.File("logs/request.log"))
|
2017-06-05 18:21:06 +00:00
|
|
|
|
2017-06-10 18:45:30 +00:00
|
|
|
app.Use(func(ctx *aero.Context, next func()) {
|
|
|
|
start := time.Now()
|
|
|
|
next()
|
|
|
|
responseTime := time.Since(start)
|
|
|
|
responseTimeString := strconv.Itoa(int(responseTime.Nanoseconds()/1000000)) + " ms"
|
|
|
|
responseTimeString = strings.Repeat(" ", 8-len(responseTimeString)) + responseTimeString
|
|
|
|
|
|
|
|
// Log every request
|
|
|
|
web.Info(ctx.RealIP(), ctx.StatusCode, responseTimeString, ctx.URI())
|
|
|
|
|
|
|
|
// Notify us about long requests
|
|
|
|
if responseTime >= 100*time.Millisecond {
|
|
|
|
err.Error("Unusually long response time", ctx.RealIP(), ctx.StatusCode, responseTimeString, ctx.URI())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|