1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 18:07:01 +00:00

accesslog: add IP in builtin fields, change the format a bit. Default func: remove compression middleware, force-set debug log level, replace the old request logger with the accesslog one, use request id middlewareand keep recovery

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-09-11 09:38:55 +03:00
parent 0f5ec75d54
commit e63d1041d2
10 changed files with 95 additions and 46 deletions

View File

@@ -3,6 +3,7 @@ package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/accesslog"
"github.com/kataras/iris/v12/middleware/requestid"
)
func main() {
@@ -14,21 +15,23 @@ func main() {
app := iris.New()
ac := accesslog.File("./access.log").AddOutput(app.Logger().Printer)
ac.TimeFormat = "2006-01-02 15:04:05"
// 1. Register a field.
ac.AddFields(func(ctx iris.Context, fields *accesslog.Fields) {
fields.Set("IP", ctx.RemoteAddr())
fields.Set("Request ID", ctx.GetID())
})
// 2. Use Template formatter's `Text` value
// to define a log line format.
ac.SetFormatter(&accesslog.Template{
Text: `{{.Now.Format .TimeFormat}} {{.Path}} {{.Code}} {{.Fields.Get "IP" }}
`, /* 2020-09-10 21:38:13 / 200 ::1 */
Text: `{{.Now.Format .TimeFormat}} {{.Path}} {{.Code}} {{.IP}} {{.Fields.Get "Request ID" }}
`, /* 2020-09-11 09:30:10 / 200 ::1 050a0979-c5e4-4c2b-9f08-cb456628edb1 */
})
// 3. Register the middleware. That's all.
app.UseRouter(ac.Handler)
// Register the request id middleware, after the logger, this maps the Context.GetID().
// Remember: the accesslog runs the next handlers before itself to provide some fields.
app.UseRouter(requestid.New())
app.Get("/", index)
@@ -42,7 +45,7 @@ func index(ctx iris.Context) {
/* Use a custom *template.Template:
// 2.1 The log line format:
text := `{{.Now.Format .TimeFormat}} {{.Path}} {{.Code}} {{.Fields.Get "IP" }}
text := `{{.Now.Format .TimeFormat}} {{.Path}} {{.Code}} {{.IP}} {{.Fields.Get "Request ID" }}
`
//
// 2.2 Parse the template, optionally using custom Template Functions.