-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
main.go
67 lines (56 loc) · 1.97 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/accesslog"
"github.com/kataras/iris/v12/middleware/requestid"
)
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)
defer ac.Close()
// 1. Register a field.
ac.AddFields(func(ctx iris.Context, fields *accesslog.Fields) {
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}} {{.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)
app.Listen(":8080")
}
func index(ctx iris.Context) {
ctx.WriteString("Index")
}
/* Use a custom *template.Template:
// 2.1 The log line format:
text := `{{.Now.Format .TimeFormat}} {{.Path}} {{.Code}} {{.IP}} {{.Fields.Get "Request ID" }}
`
//
// 2.2 Parse the template, optionally using custom Template Functions.
tmpl := template.Must(template.New("").Funcs(template.FuncMap{
// Custom functions you may want to use inside "text",
// e.g. prefixFields .Fields "my_prefix"
// to get a slice of fields starts with "my_prefix"
// and later, in the template, loop through them and render their values.
// "key": func(input) string { return ... }
}).Parse(text))
//
// 3. Use Template formatter's `Text` value
// or the `Tmpl` field to customize the look & feel of a log.
ac.SetFormatter(&accesslog.Template{
Tmpl: tmpl,
})
*/