Added a few logs
This commit is contained in:
parent
1454cdc239
commit
22f75038f5
44
logger.go
44
logger.go
@ -1,20 +1,36 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
// loggerConfig := zap.NewDevelopmentConfig()
|
import (
|
||||||
// loggerConfig.OutputPaths = append(loggerConfig.OutputPaths, "logs/server.log")
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
// logger, _ := loggerConfig.Build()
|
"github.com/aerogo/aero"
|
||||||
|
"github.com/aerogo/log"
|
||||||
|
)
|
||||||
|
|
||||||
// logTime := func(ctx *aero.Context, next func()) {
|
func init() {
|
||||||
// start := time.Now()
|
err := log.NewChannel("error")
|
||||||
// next()
|
err.AddOutput(log.File("logs/error.log"))
|
||||||
// responseTime := time.Since(start)
|
err.AddOutput(os.Stderr)
|
||||||
|
|
||||||
// if ctx.StatusCode == 200 {
|
web := log.NewChannel("web")
|
||||||
// logger.Info(ctx.URI(), zap.Duration("responseTime", responseTime), zap.Int("statusCode", ctx.StatusCode))
|
web.AddOutput(log.File("logs/request.log"))
|
||||||
// } else {
|
|
||||||
// logger.Warn(ctx.URI(), zap.Duration("responseTime", responseTime), zap.Int("statusCode", ctx.StatusCode))
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 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())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user