Improved logger
This commit is contained in:
parent
45f85d9a1e
commit
3cd8b403c5
@ -23,7 +23,11 @@ func IPToHost() aero.Middleware {
|
|||||||
func GetHostsForIP(ip string) []string {
|
func GetHostsForIP(ip string) []string {
|
||||||
hosts, found := ipToHosts.Get(ip)
|
hosts, found := ipToHosts.Get(ip)
|
||||||
|
|
||||||
if !found || hosts == nil {
|
if !found {
|
||||||
|
hosts = findHostsForIP(ip)
|
||||||
|
}
|
||||||
|
|
||||||
|
if hosts == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,17 +35,19 @@ func GetHostsForIP(ip string) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Finds all host names for the given IP
|
// Finds all host names for the given IP
|
||||||
func findHostsForIP(ip string) {
|
func findHostsForIP(ip string) []string {
|
||||||
hosts, err := net.LookupAddr(ip)
|
hosts, err := net.LookupAddr(ip)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(hosts) == 0 {
|
if len(hosts) == 0 {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache host names
|
// Cache host names
|
||||||
ipToHosts.Set(ip, hosts, cache.DefaultExpiration)
|
ipToHosts.Set(ip, hosts, cache.DefaultExpiration)
|
||||||
|
|
||||||
|
return hosts
|
||||||
}
|
}
|
||||||
|
@ -12,52 +12,62 @@ import (
|
|||||||
"github.com/animenotifier/notify.moe/utils"
|
"github.com/animenotifier/notify.moe/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Log middleware logs every request into logs/request.log and errors into logs/error.log.
|
var request = log.New()
|
||||||
func Log() aero.Middleware {
|
var err = log.New()
|
||||||
request := log.New()
|
|
||||||
|
// Initialize log files
|
||||||
|
func init() {
|
||||||
request.AddOutput(log.File("logs/request.log"))
|
request.AddOutput(log.File("logs/request.log"))
|
||||||
|
|
||||||
err := log.New()
|
|
||||||
err.AddOutput(log.File("logs/error.log"))
|
err.AddOutput(log.File("logs/error.log"))
|
||||||
err.AddOutput(os.Stderr)
|
err.AddOutput(os.Stderr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log middleware logs every request into logs/request.log and errors into logs/error.log.
|
||||||
|
func Log() aero.Middleware {
|
||||||
return func(ctx *aero.Context, next func()) {
|
return func(ctx *aero.Context, next func()) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
next()
|
next()
|
||||||
responseTime := time.Since(start)
|
responseTime := time.Since(start)
|
||||||
responseTimeString := strconv.Itoa(int(responseTime.Nanoseconds()/1000000)) + " ms"
|
|
||||||
responseTimeString = strings.Repeat(" ", 8-len(responseTimeString)) + responseTimeString
|
|
||||||
|
|
||||||
user := utils.GetUser(ctx)
|
go logRequest(ctx, responseTime)
|
||||||
ip := ctx.RealIP()
|
}
|
||||||
|
}
|
||||||
hostName := ""
|
|
||||||
hostNames := GetHostsForIP(ip)
|
// Logs a single request
|
||||||
|
func logRequest(ctx *aero.Context, responseTime time.Duration) {
|
||||||
if len(hostNames) != 0 {
|
responseTimeString := strconv.Itoa(int(responseTime.Nanoseconds()/1000000)) + " ms"
|
||||||
hostName = hostNames[0]
|
responseTimeString = strings.Repeat(" ", 8-len(responseTimeString)) + responseTimeString
|
||||||
}
|
|
||||||
|
user := utils.GetUser(ctx)
|
||||||
// Log every request
|
ip := ctx.RealIP()
|
||||||
if user != nil {
|
|
||||||
request.Info(user.Nick, ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI())
|
hostName := "<unknown host>"
|
||||||
} else {
|
hostNames := GetHostsForIP(ip)
|
||||||
request.Info("[guest]", ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI())
|
|
||||||
}
|
if len(hostNames) != 0 {
|
||||||
|
hostName = hostNames[0]
|
||||||
// Log all requests that failed
|
}
|
||||||
switch ctx.StatusCode {
|
|
||||||
case http.StatusOK, http.StatusFound, http.StatusMovedPermanently, http.StatusPermanentRedirect, http.StatusTemporaryRedirect:
|
// Log every request
|
||||||
// Ok.
|
if user != nil {
|
||||||
|
request.Info(user.Nick, ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI())
|
||||||
default:
|
} else {
|
||||||
err.Error(http.StatusText(ctx.StatusCode), ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI())
|
request.Info("[guest]", ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notify us about long requests.
|
// Log all requests that failed
|
||||||
// However ignore requests under /auth/ because those depend on 3rd party servers.
|
switch ctx.StatusCode {
|
||||||
if responseTime >= 200*time.Millisecond && !strings.HasPrefix(ctx.URI(), "/auth/") {
|
case http.StatusOK, http.StatusFound, http.StatusMovedPermanently, http.StatusPermanentRedirect, http.StatusTemporaryRedirect:
|
||||||
err.Error("Long response time", ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI())
|
// Ok.
|
||||||
}
|
|
||||||
|
default:
|
||||||
|
err.Error(http.StatusText(ctx.StatusCode), ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI())
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify us about long requests.
|
||||||
|
// However ignore requests under /auth/ because those depend on 3rd party servers.
|
||||||
|
if responseTime >= 200*time.Millisecond && !strings.HasPrefix(ctx.URI(), "/auth/") {
|
||||||
|
err.Error("Long response time", ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user