Use IP resolve log

This commit is contained in:
Eduard Urbach 2018-07-07 13:17:30 +09:00
parent e46c9b1586
commit 25b7e39d75
2 changed files with 16 additions and 18 deletions

View File

@ -10,7 +10,7 @@ import (
var ipToHosts = cache.New(60*time.Minute, 30*time.Minute) var ipToHosts = cache.New(60*time.Minute, 30*time.Minute)
// GetHostsForIP returns all host names for the given IP (if cached). // GetHostsForIP returns all host names for the given IP (if cached).
func GetHostsForIP(ip string) []string { func GetHostsForIP(ip string) ([]string, bool) {
hosts, found := ipToHosts.Get(ip) hosts, found := ipToHosts.Get(ip)
if !found { if !found {
@ -18,10 +18,10 @@ func GetHostsForIP(ip string) []string {
} }
if hosts == nil { if hosts == nil {
return nil return nil, found
} }
return hosts.([]string) return hosts.([]string), found
} }
// Finds all host names for the given IP // Finds all host names for the given IP

View File

@ -12,15 +12,16 @@ import (
"github.com/animenotifier/notify.moe/utils" "github.com/animenotifier/notify.moe/utils"
) )
var request = log.New() var requestLog = log.New()
var err = log.New() var errorLog = log.New()
var ipLog = log.New()
// Initialize log files // Initialize log files
func init() { func init() {
request.AddOutput(log.File("logs/request.log")) requestLog.AddOutput(log.File("logs/request.log"))
errorLog.AddOutput(log.File("logs/error.log"))
err.AddOutput(log.File("logs/error.log")) errorLog.AddOutput(os.Stderr)
err.AddOutput(os.Stderr) ipLog.AddOutput(log.File("logs/ip.log"))
} }
// Log middleware logs every request into logs/request.log and errors into logs/error.log. // Log middleware logs every request into logs/request.log and errors into logs/error.log.
@ -47,13 +48,10 @@ func logRequest(ctx *aero.Context, responseTime time.Duration) {
user := utils.GetUser(ctx) user := utils.GetUser(ctx)
ip := ctx.RealIP() ip := ctx.RealIP()
hostNames, cached := GetHostsForIP(ip)
hostName := "<unknown host>" if !cached && len(hostNames) > 0 {
hostNames := GetHostsForIP(ip) ipLog.Info(ip, strings.Join(hostNames, ", "))
if len(hostNames) != 0 {
hostName = hostNames[0]
hostName = strings.TrimSuffix(hostName, ".")
} }
// Log every request // Log every request
@ -65,7 +63,7 @@ func logRequest(ctx *aero.Context, responseTime time.Duration) {
nick = user.Nick nick = user.Nick
} }
request.Info(nick, id, ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI()) requestLog.Info(nick, id, ip, responseTimeString, ctx.StatusCode, ctx.URI())
// Log all requests that failed // Log all requests that failed
switch ctx.StatusCode { switch ctx.StatusCode {
@ -73,12 +71,12 @@ func logRequest(ctx *aero.Context, responseTime time.Duration) {
// Ok. // Ok.
default: default:
err.Error(nick, id, ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI(), ctx.ErrorMessage) errorLog.Error(nick, id, ip, responseTimeString, ctx.StatusCode, ctx.URI(), ctx.ErrorMessage)
} }
// Notify us about long requests. // Notify us about long requests.
// However ignore requests under /auth/ because those depend on 3rd party servers. // However ignore requests under /auth/ because those depend on 3rd party servers.
if responseTime >= 300*time.Millisecond && !strings.HasPrefix(ctx.URI(), "/auth/") && !strings.HasPrefix(ctx.URI(), "/sitemap/") { if responseTime >= 300*time.Millisecond && !strings.HasPrefix(ctx.URI(), "/auth/") && !strings.HasPrefix(ctx.URI(), "/sitemap/") {
err.Error("Long response time", nick, id, ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI()) errorLog.Error("Long response time", nick, id, ip, responseTimeString, ctx.StatusCode, ctx.URI())
} }
} }