From 22f75038f56ba22fdddca42a685a00b83caedf05 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 10 Jun 2017 20:45:30 +0200 Subject: [PATCH] Added a few logs --- logger.go | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/logger.go b/logger.go index 675f7d06..3b366942 100644 --- a/logger.go +++ b/logger.go @@ -1,20 +1,36 @@ package main -// loggerConfig := zap.NewDevelopmentConfig() -// loggerConfig.OutputPaths = append(loggerConfig.OutputPaths, "logs/server.log") +import ( + "os" + "strconv" + "strings" + "time" -// logger, _ := loggerConfig.Build() + "github.com/aerogo/aero" + "github.com/aerogo/log" +) -// logTime := func(ctx *aero.Context, next func()) { -// start := time.Now() -// next() -// responseTime := time.Since(start) +func init() { + err := log.NewChannel("error") + err.AddOutput(log.File("logs/error.log")) + err.AddOutput(os.Stderr) -// if ctx.StatusCode == 200 { -// logger.Info(ctx.URI(), zap.Duration("responseTime", responseTime), zap.Int("statusCode", ctx.StatusCode)) -// } else { -// logger.Warn(ctx.URI(), zap.Duration("responseTime", responseTime), zap.Int("statusCode", ctx.StatusCode)) -// } -// } + web := log.NewChannel("web") + web.AddOutput(log.File("logs/request.log")) -// app.Use(logTime) + app.Use(func(ctx *aero.Context, next func()) { + start := time.Now() + next() + responseTime := time.Since(start) + responseTimeString := strconv.Itoa(int(responseTime.Nanoseconds()/1000000)) + " ms" + responseTimeString = strings.Repeat(" ", 8-len(responseTimeString)) + responseTimeString + + // Log every request + web.Info(ctx.RealIP(), ctx.StatusCode, responseTimeString, ctx.URI()) + + // Notify us about long requests + if responseTime >= 100*time.Millisecond { + err.Error("Unusually long response time", ctx.RealIP(), ctx.StatusCode, responseTimeString, ctx.URI()) + } + }) +}