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"
|
2019-11-17 07:59:34 +00:00
|
|
|
"github.com/animenotifier/notify.moe/arn"
|
2017-06-10 18:45:30 +00:00
|
|
|
)
|
2017-06-05 18:21:06 +00:00
|
|
|
|
2019-05-11 09:59:34 +00:00
|
|
|
var (
|
|
|
|
requestLog = log.New()
|
|
|
|
errorLog = log.New()
|
|
|
|
ipLog = log.New()
|
|
|
|
)
|
2017-06-23 13:51:26 +00:00
|
|
|
|
|
|
|
// Initialize log files
|
|
|
|
func init() {
|
2019-05-07 14:01:51 +00:00
|
|
|
// The request log contains every single request to the server
|
|
|
|
requestLog.AddWriter(log.File("logs/request.log"))
|
|
|
|
|
|
|
|
// The IP log contains the IPs accessing the server
|
|
|
|
ipLog.AddWriter(log.File("logs/ip.log"))
|
|
|
|
|
|
|
|
// The error log contains all failed requests
|
|
|
|
errorLog.AddWriter(log.File("logs/error.log"))
|
|
|
|
errorLog.AddWriter(os.Stderr)
|
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.
|
2019-06-01 04:55:49 +00:00
|
|
|
func Log(next aero.Handler) aero.Handler {
|
|
|
|
return func(ctx aero.Context) error {
|
2017-06-10 18:45:30 +00:00
|
|
|
start := time.Now()
|
2019-06-01 04:55:49 +00:00
|
|
|
err := next(ctx)
|
2017-06-10 18:45:30 +00:00
|
|
|
responseTime := time.Since(start)
|
2017-06-23 13:51:26 +00:00
|
|
|
|
2019-06-01 15:28:22 +00:00
|
|
|
logRequest(ctx, responseTime)
|
2019-06-01 04:55:49 +00:00
|
|
|
return err
|
2017-06-23 13:51:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logs a single request
|
2019-06-01 04:55:49 +00:00
|
|
|
func logRequest(ctx aero.Context, responseTime time.Duration) {
|
2017-06-23 13:51:26 +00:00
|
|
|
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
|
|
|
|
2019-11-17 07:59:34 +00:00
|
|
|
user := arn.GetUserFromContext(ctx)
|
2019-06-01 04:55:49 +00:00
|
|
|
ip := ctx.IP()
|
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 {
|
2019-05-07 14:01:51 +00:00
|
|
|
ipLog.Info("%s = %s", ip, strings.Join(hostNames, ", "))
|
2017-06-23 13:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Log every request
|
2019-05-07 14:01:51 +00:00
|
|
|
id := "id"
|
|
|
|
nick := "guest"
|
2018-07-07 03:42:00 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-01 04:55:49 +00:00
|
|
|
requestLog.Info("%s | %s | %s | %s | %d | %s", nick, id, ip, responseTimeString, ctx.Status(), ctx.Path())
|
2018-07-07 03:42:00 +00:00
|
|
|
|
2017-06-23 13:51:26 +00:00
|
|
|
// Log all requests that failed
|
2019-06-01 04:55:49 +00:00
|
|
|
switch ctx.Status() {
|
2019-06-03 06:06:57 +00:00
|
|
|
case http.StatusOK, http.StatusTemporaryRedirect, http.StatusPermanentRedirect:
|
2017-06-23 13:51:26 +00:00
|
|
|
// Ok.
|
|
|
|
|
|
|
|
default:
|
2019-06-01 04:55:49 +00:00
|
|
|
errorLog.Error("%s | %s | %s | %s | %d | %s", nick, id, ip, responseTimeString, ctx.Status(), ctx.Path())
|
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.
|
2019-06-01 04:55:49 +00:00
|
|
|
if responseTime >= 500*time.Millisecond && !strings.HasPrefix(ctx.Path(), "/auth/") && !strings.HasPrefix(ctx.Path(), "/sitemap/") && !strings.HasPrefix(ctx.Path(), "/api/sse/") {
|
|
|
|
errorLog.Error("%s | %s | %s | %s | %d | %s (long response time)", nick, id, ip, responseTimeString, ctx.Status(), ctx.Path())
|
2017-06-16 23:12:28 +00:00
|
|
|
}
|
2017-06-10 18:45:30 +00:00
|
|
|
}
|