mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 18:37:05 +00:00
add accesslog+MVC example
This commit is contained in:
@@ -159,6 +159,16 @@ func sessionHandler(ctx iris.Context) {
|
||||
ctx.WriteString("OK")
|
||||
}
|
||||
|
||||
type user struct {
|
||||
ID string
|
||||
Username string
|
||||
}
|
||||
|
||||
// Log custom structs, they can implement the fmt.Stringer interface too.
|
||||
func (u user) String() string {
|
||||
return u.ID + ":" + u.Username
|
||||
}
|
||||
|
||||
func fieldsHandler(ctx iris.Context) {
|
||||
start := time.Now()
|
||||
// simulate a heavy job...
|
||||
@@ -169,5 +179,13 @@ func fieldsHandler(ctx iris.Context) {
|
||||
logFields := accesslog.GetFields(ctx)
|
||||
logFields.Set("job_latency", end.Round(time.Second))
|
||||
|
||||
// Simulate a database fetch or anything
|
||||
// to get a "user" and log it:
|
||||
u := user{
|
||||
ID: "user-id",
|
||||
Username: "user-name",
|
||||
}
|
||||
logFields.Set("user", u)
|
||||
|
||||
ctx.WriteString("OK")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user