1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-08 12:31:58 +00:00

add accesslog middleware (rel: #1601)

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-09-06 10:38:48 +03:00
parent bf9f7617e2
commit 0be856e54c
16 changed files with 339 additions and 347 deletions

View File

@@ -48,20 +48,11 @@ type Config struct {
// Defaults to false.
TraceRoute bool
// Columns will display the logs as a formatted columns-rows text (bool).
// If custom `LogFunc` has been provided then this field is useless and users should
// use the `Columinize` function of the logger to get the output result as columns.
//
// Defaults to false.
Columns bool
// MessageContextKeys if not empty,
// the middleware will try to fetch
// the contents with `ctx.Values().Get(MessageContextKey)`
// and if available then these contents will be
// appended as part of the logs (with `%v`, in order to be able to set a struct too),
// if Columns field was set to true then
// a new column will be added named 'Message'.
//
// Defaults to empty.
MessageContextKeys []string
@@ -71,8 +62,6 @@ type Config struct {
// the contents with `ctx.Values().Get(MessageHeaderKey)`
// and if available then these contents will be
// appended as part of the logs (with `%v`, in order to be able to set a struct too),
// if Columns field was set to true then
// a new column will be added named 'HeaderMessage'.
//
// Defaults to empty.
MessageHeaderKeys []string
@@ -93,7 +82,7 @@ type Config struct {
}
// DefaultConfig returns a default config
// that have all boolean fields to true except `Columns`,
// that have all boolean fields to true,
// all strings are empty,
// LogFunc and Skippers to nil as well.
func DefaultConfig() Config {
@@ -105,7 +94,6 @@ func DefaultConfig() Config {
PathAfterHandler: false,
Query: false,
TraceRoute: false,
Columns: false,
LogFunc: nil,
LogFuncCtx: nil,
Skippers: nil,

View File

@@ -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
}