1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-21 20:07:04 +00:00
Gerasimos (Makis) Maropoulos
2020-08-18 05:42:48 +03:00
parent 5481b9a6c1
commit 1192e6f787
6 changed files with 83 additions and 42 deletions

View File

@@ -24,6 +24,7 @@ type requestLoggerMiddleware struct {
// This is for the http requests.
//
// Receives an optional configuation.
// Usage: app.UseRouter(logger.New()).
func New(cfg ...Config) context.Handler {
c := DefaultConfig()
if len(cfg) > 0 {
@@ -35,6 +36,13 @@ func New(cfg ...Config) context.Handler {
return l.ServeHTTP
}
func (l *requestLoggerMiddleware) getPath(ctx *context.Context) string {
if l.config.Query {
return ctx.Request().URL.RequestURI()
}
return ctx.Path()
}
// Serve serves the middleware
func (l *requestLoggerMiddleware) ServeHTTP(ctx *context.Context) {
// skip logs and serve the main request immediately
@@ -51,16 +59,7 @@ func (l *requestLoggerMiddleware) ServeHTTP(ctx *context.Context) {
var startTime, endTime time.Time
startTime = time.Now()
ctx.Next()
// no time.Since in order to format it well after
endTime = time.Now()
latency = endTime.Sub(startTime)
if l.config.Status {
status = strconv.Itoa(ctx.GetStatusCode())
}
// Before Next.
if l.config.IP {
ip = ctx.RemoteAddr()
}
@@ -70,11 +69,21 @@ func (l *requestLoggerMiddleware) ServeHTTP(ctx *context.Context) {
}
if l.config.Path {
if l.config.Query {
path = ctx.Request().URL.RequestURI()
} else {
path = ctx.Path()
}
path = l.getPath(ctx)
}
ctx.Next()
// no time.Since in order to format it well after
endTime = time.Now()
latency = endTime.Sub(startTime)
if l.config.PathAfterHandler /* we don't care if Path is disabled */ {
path = l.getPath(ctx)
}
if l.config.Status {
status = strconv.Itoa(ctx.GetStatusCode())
}
var message interface{}
@@ -125,12 +134,11 @@ func (l *requestLoggerMiddleware) ServeHTTP(ctx *context.Context) {
line += fmt.Sprintf(" %v", headerMessage)
}
// if context.StatusCodeNotSuccessful(ctx.GetStatusCode()) {
// ctx.Application().Logger().Warn(line)
// } else {
ctx.Application().Logger().Info(line)
// }
if context.StatusCodeNotSuccessful(ctx.GetStatusCode()) {
ctx.Application().Logger().Warn(line)
} else {
ctx.Application().Logger().Info(line)
}
}
// Columnize formats the given arguments as columns and returns the formatted output,