1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-03 18:27:07 +00:00

Update to 8.0.3 | Request logger improvements, error handlers improvements. Read HISTORY.md

Former-commit-id: fb5eca0dc955d8c07fdba35b47068008565dbbd1
This commit is contained in:
kataras
2017-07-16 13:58:10 +03:00
parent 91bb768d4a
commit 56aa3de645
11 changed files with 339 additions and 61 deletions

View File

@@ -1,21 +1,78 @@
package logger
import (
"time"
"github.com/kataras/iris/context"
)
// The SkipperFunc signature, used to serve the main request without logs.
// See `Configuration` too.
type SkipperFunc func(ctx context.Context) bool
// Config are the options of the logger middlweare
// contains 4 bools
// Status, IP, Method, Path
// if set to true then these will print
type Config struct {
// Status displays status code (bool)
//
// Defaults to true
Status bool
// IP displays request's remote address (bool)
//
// Defaults to true
IP bool
// Method displays the http method (bool)
//
// Defaults to true
Method bool
// Path displays the request path (bool)
//
// Defaults to true
Path bool
// Columns will display the logs as well formatted columns (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 ouput result as columns.
//
// Defaults to true
Columns bool
// LogFunc is the writer which logs are written to,
// if missing the logger middleware uses the app.Logger().Infof instead.
LogFunc func(now time.Time, latency time.Duration, status, ip, method, path string)
// Skippers used to skip the logging i.e by `ctx.Path()` and serve
// the next/main handler immediately.
Skippers []SkipperFunc
// the Skippers as one function in order to reduce the time needed to
// combine them at serve time.
skip SkipperFunc
}
// DefaultConfiguration returns an options which all properties are true except EnableColors
// DefaultConfiguration returns a default configuration
// that have all boolean fields to true,
// LogFunc to nil,
// and Skippers to nil.
func DefaultConfiguration() Config {
return Config{true, true, true, true}
return Config{true, true, true, true, true, nil, nil, nil}
}
// AddSkipper adds a skipper to the configuration.
func (c *Config) AddSkipper(sk SkipperFunc) {
c.Skippers = append(c.Skippers, sk)
c.buildSkipper()
}
func (c *Config) buildSkipper() {
if len(c.Skippers) == 0 {
return
}
skippersLocked := c.Skippers[0:]
c.skip = func(ctx context.Context) bool {
for _, s := range skippersLocked {
if s(ctx) {
return true
}
}
return false
}
}

View File

@@ -2,25 +2,49 @@
package logger
import (
"fmt"
"strconv"
"time"
"github.com/kataras/iris/context"
"github.com/ryanuber/columnize"
)
type requestLoggerMiddleware struct {
config Config
}
// New creates and returns a new request logger middleware.
// Do not confuse it with the framework's Logger.
// This is for the http requests.
//
// Receives an optional configuation.
func New(cfg ...Config) context.Handler {
c := DefaultConfiguration()
if len(cfg) > 0 {
c = cfg[0]
}
c.buildSkipper()
l := &requestLoggerMiddleware{config: c}
return l.ServeHTTP
}
// Serve serves the middleware
func (l *requestLoggerMiddleware) ServeHTTP(ctx context.Context) {
// skip logs and serve the main request immediately
if l.config.skip != nil {
if l.config.skip(ctx) {
ctx.Next()
return
}
}
//all except latency to string
var status, ip, method, path string
var latency time.Duration
var startTime, endTime time.Time
path = ctx.Path()
method = ctx.Method()
startTime = time.Now()
ctx.Next()
@@ -36,29 +60,36 @@ func (l *requestLoggerMiddleware) ServeHTTP(ctx context.Context) {
ip = ctx.RemoteAddr()
}
if !l.config.Method {
method = ""
if l.config.Method {
method = ctx.Method()
}
if !l.config.Path {
path = ""
if l.config.Path {
path = ctx.Path()
}
//finally print the logs, no new line, the framework's logger is responsible how to render each log.
ctx.Application().Logger().Infof("%v %4v %s %s %s", status, latency, ip, method, path)
// print the logs
if logFunc := l.config.LogFunc; logFunc != nil {
logFunc(endTime, latency, status, ip, method, path)
return
}
endTimeFormatted := endTime.Format("2006/01/02 - 15:04:05")
if l.config.Columns {
output := Columnize(endTimeFormatted, latency, status, ip, method, path)
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)
}
// New creates and returns a new request logger middleware.
// Do not confuse it with the framework's Logger.
// This is for the http requests.
//
// Receives an optional configuation.
func New(cfg ...Config) context.Handler {
c := DefaultConfiguration()
if len(cfg) > 0 {
c = cfg[0]
// 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 {
outputC := []string{
"Time | Status | Latency | IP | Method | Path",
fmt.Sprintf("%s | %v | %4v | %s | %s | %s", nowFormatted, status, latency, ip, method, path),
}
l := &requestLoggerMiddleware{config: c}
return l.ServeHTTP
output := columnize.SimpleFormat(outputC) + "\n"
return output
}