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)
// 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)
if !found {
@ -18,10 +18,10 @@ func GetHostsForIP(ip string) []string {
}
if hosts == nil {
return nil
return nil, found
}
return hosts.([]string)
return hosts.([]string), found
}
// Finds all host names for the given IP

View File

@ -12,15 +12,16 @@ import (
"github.com/animenotifier/notify.moe/utils"
)
var request = log.New()
var err = log.New()
var requestLog = log.New()
var errorLog = log.New()
var ipLog = log.New()
// Initialize log files
func init() {
request.AddOutput(log.File("logs/request.log"))
err.AddOutput(log.File("logs/error.log"))
err.AddOutput(os.Stderr)
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"))
}
// 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)
ip := ctx.RealIP()
hostNames, cached := GetHostsForIP(ip)
hostName := "<unknown host>"
hostNames := GetHostsForIP(ip)
if len(hostNames) != 0 {
hostName = hostNames[0]
hostName = strings.TrimSuffix(hostName, ".")
if !cached && len(hostNames) > 0 {
ipLog.Info(ip, strings.Join(hostNames, ", "))
}
// Log every request
@ -65,7 +63,7 @@ func logRequest(ctx *aero.Context, responseTime time.Duration) {
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
switch ctx.StatusCode {
@ -73,12 +71,12 @@ func logRequest(ctx *aero.Context, responseTime time.Duration) {
// Ok.
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.
// 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/") {
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())
}
}