mirror of
https://github.com/kataras/iris.git
synced 2026-01-10 05:25:58 +00:00
acesslog new example: custom fields and custom template
This commit is contained in:
@@ -73,6 +73,7 @@
|
||||
* Logging
|
||||
* [Request Logger](logging/request-logger/main.go)
|
||||
* [AccessLog: log request & response and more](logging/request-logger/accesslog)
|
||||
* [AccessLog: custom fields and template](logging/request-logger/accesslog-template/main.go)
|
||||
* [AccessLog: listen to logs and render them](logging/request-logger/accesslog-broker/main.go)
|
||||
* [Log Requests to a JSON File](logging/request-logger/request-logger-file-json/main.go)
|
||||
* [Application File Logger](logging/file-logger/main.go)
|
||||
|
||||
43
_examples/logging/request-logger/accesslog-template/main.go
Normal file
43
_examples/logging/request-logger/accesslog-template/main.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/middleware/accesslog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
/*
|
||||
This example will show you how you can
|
||||
register custom fields and log them separately
|
||||
with a custom format through the Template formatter.
|
||||
*/
|
||||
|
||||
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())
|
||||
})
|
||||
// 2. Use Template formatter's `Text` value
|
||||
// to customize the look & feel of a log.
|
||||
// You could also use its `Tmpl` field to
|
||||
// set a *template.Template instance.
|
||||
ac.SetFormatter(&accesslog.Template{
|
||||
Text: `{{.Now.Format .TimeFormat}} {{.Path}} {{.Code}} {{.Fields.Get "IP" }}
|
||||
`,
|
||||
})
|
||||
// Example Output:
|
||||
// 2020-09-10 21:38:13 / 200 ::1
|
||||
|
||||
app.UseRouter(ac.Handler)
|
||||
|
||||
app.Get("/", index)
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func index(ctx iris.Context) {
|
||||
ctx.WriteString("Index")
|
||||
}
|
||||
Reference in New Issue
Block a user