mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +00:00
Update to 8.0.4 | New: transfer a message to the request logger
Former-commit-id: 2bab3c9f28f7e9edd5d85e579349f70388af871d
This commit is contained in:
@@ -21,7 +21,7 @@ type requestLoggerMiddleware struct {
|
||||
//
|
||||
// Receives an optional configuation.
|
||||
func New(cfg ...Config) context.Handler {
|
||||
c := DefaultConfiguration()
|
||||
c := DefaultConfig()
|
||||
if len(cfg) > 0 {
|
||||
c = cfg[0]
|
||||
}
|
||||
@@ -48,6 +48,7 @@ func (l *requestLoggerMiddleware) ServeHTTP(ctx context.Context) {
|
||||
startTime = time.Now()
|
||||
|
||||
ctx.Next()
|
||||
|
||||
//no time.Since in order to format it well after
|
||||
endTime = time.Now()
|
||||
latency = endTime.Sub(startTime)
|
||||
@@ -68,27 +69,44 @@ func (l *requestLoggerMiddleware) ServeHTTP(ctx context.Context) {
|
||||
path = ctx.Path()
|
||||
}
|
||||
|
||||
var message interface{}
|
||||
if ctxKey := l.config.MessageContextKey; ctxKey != "" {
|
||||
message = ctx.Values().Get(ctxKey)
|
||||
}
|
||||
|
||||
// print the logs
|
||||
if logFunc := l.config.LogFunc; logFunc != nil {
|
||||
logFunc(endTime, latency, status, ip, method, path)
|
||||
logFunc(endTime, latency, status, ip, method, path, message)
|
||||
return
|
||||
}
|
||||
endTimeFormatted := endTime.Format("2006/01/02 - 15:04:05")
|
||||
if l.config.Columns {
|
||||
output := Columnize(endTimeFormatted, latency, status, ip, method, path)
|
||||
output := Columnize(endTimeFormatted, latency, status, ip, method, path, message)
|
||||
ctx.Application().Logger().Out.Write([]byte(output))
|
||||
return
|
||||
}
|
||||
// no new line, the framework's logger is responsible how to render each log.
|
||||
ctx.Application().Logger().Infof("%s | %v %4v %s %s %s", endTimeFormatted, status, latency, ip, method, path)
|
||||
line := fmt.Sprintf("%s | %v %4v %s %s %s", endTimeFormatted, status, latency, ip, method, path)
|
||||
if message != nil {
|
||||
line += fmt.Sprintf(" %v", message)
|
||||
}
|
||||
ctx.Application().Logger().Info(line)
|
||||
}
|
||||
|
||||
// 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) string {
|
||||
func Columnize(nowFormatted string, latency time.Duration, status, ip, method, path string, message 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)
|
||||
}
|
||||
|
||||
outputC := []string{
|
||||
"Time | Status | Latency | IP | Method | Path",
|
||||
fmt.Sprintf("%s | %v | %4v | %s | %s | %s", nowFormatted, status, latency, ip, method, path),
|
||||
titles,
|
||||
line,
|
||||
}
|
||||
output := columnize.SimpleFormat(outputC) + "\n"
|
||||
return output
|
||||
|
||||
Reference in New Issue
Block a user