mirror of
https://github.com/kataras/iris.git
synced 2025-12-21 11:57:02 +00:00
add accesslog middleware (rel: #1601)
This commit is contained in:
@@ -7,8 +7,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12/context"
|
||||
|
||||
"github.com/ryanuber/columnize"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -120,12 +118,6 @@ func (l *requestLoggerMiddleware) ServeHTTP(ctx *context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if l.config.Columns {
|
||||
endTimeFormatted := endTime.Format("2006/01/02 - 15:04:05")
|
||||
output := Columnize(endTimeFormatted, latency, status, ip, method, path, message, headerMessage)
|
||||
_, _ = ctx.Application().Logger().Printer.Write([]byte(output))
|
||||
return
|
||||
}
|
||||
// no new line, the framework's logger is responsible how to render each log.
|
||||
line := fmt.Sprintf("%v %4v %s %s %s", status, latency, ip, method, path)
|
||||
if message != nil {
|
||||
@@ -158,26 +150,3 @@ func (l *requestLoggerMiddleware) ServeHTTP(ctx *context.Context) {
|
||||
ctx.GetCurrentRoute().Trace(ctx.Application().Logger().Printer, ctx.HandlerIndex(-1))
|
||||
}
|
||||
}
|
||||
|
||||
// Columnize formats the given arguments as columns and returns the formatted output,
|
||||
// note that it appends a new line to the end.
|
||||
func Columnize(nowFormatted string, latency time.Duration, status, ip, method, path string, message interface{}, headerMessage interface{}) string {
|
||||
titles := "Time | Status | Latency | IP | Method | Path"
|
||||
line := fmt.Sprintf("%s | %v | %4v | %s | %s | %s", nowFormatted, status, latency, ip, method, path)
|
||||
if message != nil {
|
||||
titles += " | Message"
|
||||
line += fmt.Sprintf(" | %v", message)
|
||||
}
|
||||
|
||||
if headerMessage != nil {
|
||||
titles += " | HeaderMessage"
|
||||
line += fmt.Sprintf(" | %v", headerMessage)
|
||||
}
|
||||
|
||||
outputC := []string{
|
||||
titles,
|
||||
line,
|
||||
}
|
||||
output := columnize.SimpleFormat(outputC) + "\n"
|
||||
return output
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user