2017-06-16 23:12:28 +00:00
|
|
|
package middleware
|
2017-06-05 18:21:06 +00:00
|
|
|
|
2017-06-10 18:45:30 +00:00
|
|
|
import (
|
2017-06-15 13:50:39 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2017-06-10 18:45:30 +00:00
|
|
|
"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-22 20:26:52 +00:00
|
|
|
"github.com/animenotifier/notify.moe/utils"
|
2017-06-10 18:45:30 +00:00
|
|
|
)
|
2017-06-05 18:21:06 +00:00
|
|
|
|
2018-07-07 04:17:30 +00:00
|
|
|
var requestLog = log.New()
|
|
|
|
var errorLog = log.New()
|
|
|
|
var ipLog = log.New()
|
2017-06-23 13:51:26 +00:00
|
|
|
|
|
|
|
// Initialize log files
|
|
|
|
func init() {
|
2018-07-07 04:17:30 +00:00
|
|
|
requestLog.AddOutput(log.File("logs/request.log"))
|
|
|
|
errorLog.AddOutput(log.File("logs/error.log"))
|
|
|
|
errorLog.AddOutput(os.Stderr)
|
|
|
|
ipLog.AddOutput(log.File("logs/ip.log"))
|
2017-06-23 13:51:26 +00:00
|
|
|
}
|
2017-06-05 18:21:06 +00:00
|
|
|
|
2017-06-23 13:51:26 +00:00
|
|
|
// Log middleware logs every request into logs/request.log and errors into logs/error.log.
|
|
|
|
func Log() aero.Middleware {
|
2017-06-16 23:12:28 +00:00
|
|
|
return func(ctx *aero.Context, next func()) {
|
2017-06-10 18:45:30 +00:00
|
|
|
start := time.Now()
|
|
|
|
next()
|
|
|
|
responseTime := time.Since(start)
|
2017-06-23 13:51:26 +00:00
|
|
|
|
|
|
|
go logRequest(ctx, responseTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logs a single request
|
|
|
|
func logRequest(ctx *aero.Context, responseTime time.Duration) {
|
|
|
|
responseTimeString := strconv.Itoa(int(responseTime.Nanoseconds()/1000000)) + " ms"
|
2017-11-29 18:23:14 +00:00
|
|
|
repeatSpaceCount := 8 - len(responseTimeString)
|
|
|
|
|
|
|
|
if repeatSpaceCount < 0 {
|
|
|
|
repeatSpaceCount = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
responseTimeString = strings.Repeat(" ", repeatSpaceCount) + responseTimeString
|
2017-06-23 13:51:26 +00:00
|
|
|
|
|
|
|
user := utils.GetUser(ctx)
|
|
|
|
ip := ctx.RealIP()
|
2018-07-07 04:17:30 +00:00
|
|
|
hostNames, cached := GetHostsForIP(ip)
|
2017-06-23 13:51:26 +00:00
|
|
|
|
2018-07-07 04:17:30 +00:00
|
|
|
if !cached && len(hostNames) > 0 {
|
|
|
|
ipLog.Info(ip, strings.Join(hostNames, ", "))
|
2017-06-23 13:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Log every request
|
2018-07-07 03:42:00 +00:00
|
|
|
id := "[id]"
|
|
|
|
nick := "[guest]"
|
|
|
|
|
2017-06-23 13:51:26 +00:00
|
|
|
if user != nil {
|
2018-07-07 03:42:00 +00:00
|
|
|
id = user.ID
|
|
|
|
nick = user.Nick
|
2017-06-23 13:51:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-07 04:17:30 +00:00
|
|
|
requestLog.Info(nick, id, ip, responseTimeString, ctx.StatusCode, ctx.URI())
|
2018-07-07 03:42:00 +00:00
|
|
|
|
2017-06-23 13:51:26 +00:00
|
|
|
// Log all requests that failed
|
|
|
|
switch ctx.StatusCode {
|
|
|
|
case http.StatusOK, http.StatusFound, http.StatusMovedPermanently, http.StatusPermanentRedirect, http.StatusTemporaryRedirect:
|
|
|
|
// Ok.
|
|
|
|
|
|
|
|
default:
|
2018-07-07 04:17:30 +00:00
|
|
|
errorLog.Error(nick, id, ip, responseTimeString, ctx.StatusCode, ctx.URI(), ctx.ErrorMessage)
|
2017-06-23 13:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify us about long requests.
|
|
|
|
// However ignore requests under /auth/ because those depend on 3rd party servers.
|
2018-11-06 20:40:03 +00:00
|
|
|
if responseTime >= 300*time.Millisecond && !strings.HasPrefix(ctx.URI(), "/auth/") && !strings.HasPrefix(ctx.URI(), "/sitemap/") && !strings.HasPrefix(ctx.URI(), "/api/sse/") {
|
2018-07-07 04:17:30 +00:00
|
|
|
errorLog.Error("Long response time", nick, id, ip, responseTimeString, ctx.StatusCode, ctx.URI())
|
2017-06-16 23:12:28 +00:00
|
|
|
}
|
2017-06-10 18:45:30 +00:00
|
|
|
}
|