Skip to content

Commit 7fa2666

Browse files
committed
add accesslog+MVC example
1 parent 0c125ba commit 7fa2666

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

_examples/logging/request-logger/accesslog/access.log.sample

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
2020-09-13 13:38:18 0s 401 GET /admin ::1 0 B 0 B
88
2020-09-13 13:38:19 0s 200 GET /admin ::1 auth=admin:admin 0 B 48 B
99
2020-09-13 13:38:22 0s 200 GET /session ::1 session_id=23fe763f-c9d5-4d65-9e1a-2cc8d23d1aa3 session_test_key=session_test_value auth=admin:admin 0 B 2 B
10-
2020-09-13 13:38:25 2.0001204s 200 GET /fields ::1 job_latency=2s auth=admin:admin 0 B 2 B
10+
2020-09-13 13:38:25 2.0001204s 200 GET /fields ::1 job_latency=2s auth=admin:admin user=user-id:user-name 0 B 2 B

_examples/logging/request-logger/accesslog/main.go

+18
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ func sessionHandler(ctx iris.Context) {
159159
ctx.WriteString("OK")
160160
}
161161

162+
type user struct {
163+
ID string
164+
Username string
165+
}
166+
167+
// Log custom structs, they can implement the fmt.Stringer interface too.
168+
func (u user) String() string {
169+
return u.ID + ":" + u.Username
170+
}
171+
162172
func fieldsHandler(ctx iris.Context) {
163173
start := time.Now()
164174
// simulate a heavy job...
@@ -169,5 +179,13 @@ func fieldsHandler(ctx iris.Context) {
169179
logFields := accesslog.GetFields(ctx)
170180
logFields.Set("job_latency", end.Round(time.Second))
171181

182+
// Simulate a database fetch or anything
183+
// to get a "user" and log it:
184+
u := user{
185+
ID: "user-id",
186+
Username: "user-name",
187+
}
188+
logFields.Set("user", u)
189+
172190
ctx.WriteString("OK")
173191
}

_examples/mvc/basic/main.go

+24-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/kataras/iris/v12"
7+
"github.com/kataras/iris/v12/middleware/accesslog"
78
"github.com/kataras/iris/v12/middleware/recover"
89
"github.com/kataras/iris/v12/sessions"
910

@@ -12,31 +13,40 @@ import (
1213

1314
func main() {
1415
app := iris.New()
15-
app.Use(recover.New())
1616
app.Logger().SetLevel("debug")
17-
// Disable verbose logging of routes for this and its children parties
18-
// when the log level is "debug": app.SetRoutesNoLog(true)
19-
mvc.Configure(app.Party("/basic"), basicMVC)
17+
18+
basic := app.Party("/basic")
19+
{
20+
// Register middlewares to run under the /basic path prefix.
21+
ac := accesslog.File("./basic_access.log")
22+
defer ac.Close()
23+
24+
basic.UseRouter(ac.Handler)
25+
basic.UseRouter(recover.New())
26+
27+
mvc.Configure(basic, basicMVC)
28+
}
2029

2130
app.Listen(":8080")
2231
}
2332

2433
func basicMVC(app *mvc.Application) {
2534
// Disable verbose logging of controllers for this and its children mvc apps
26-
// when the log level is "debug": app.SetControllersNoLog(true)
35+
// when the log level is "debug":
36+
app.SetControllersNoLog(true)
2737

28-
// You can use normal middlewares at MVC apps of course.
29-
app.Router.Use(func(ctx iris.Context) {
30-
ctx.Application().Logger().Infof("Path: %s", ctx.Path())
31-
ctx.Next()
32-
})
38+
// You can still register middlewares at MVC apps of course.
39+
// The app.Router returns the Party that this MVC
40+
// was registered on.
41+
// app.Router.UseRouter/Use/....
3342

3443
// Register dependencies which will be binding to the controller(s),
3544
// can be either a function which accepts an iris.Context and returns a single value (dynamic binding)
3645
// or a static struct value (service).
3746
app.Register(
3847
sessions.New(sessions.Config{}).Start,
3948
&prefixedLogger{prefix: "DEV"},
49+
accesslog.GetFields, // Set custom fields through a controller or controller's methods.
4050
)
4151

4252
// GET: http://localhost:8080/basic
@@ -73,9 +83,9 @@ func (s *prefixedLogger) Log(msg string) {
7383
}
7484

7585
type basicController struct {
76-
Logger LoggerService
77-
78-
Session *sessions.Session
86+
Logger LoggerService // the static logger service attached to this app.
87+
Session *sessions.Session // current HTTP session.
88+
LogFields *accesslog.Fields // accesslog middleware custom fields.
7989
}
8090

8191
func (c *basicController) BeforeActivation(b mvc.BeforeActivation) {
@@ -90,6 +100,7 @@ func (c *basicController) AfterActivation(a mvc.AfterActivation) {
90100

91101
func (c *basicController) Get() string {
92102
count := c.Session.Increment("count", 1)
103+
c.LogFields.Set("count", count)
93104

94105
body := fmt.Sprintf("Hello from basicController\nTotal visits from you: %d", count)
95106
c.Logger.Log(body)

middleware/accesslog/accesslog.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ const (
3131
// GetFields returns the accesslog fields for this request.
3232
// Returns a store which the caller can use to
3333
// set/get/remove custom log fields. Use its `Set` method.
34+
//
35+
// To use with MVC: Register(accesslog.GetFields).
36+
// DI Handlers: ConfigureContainer().RegisterDependency(accesslog.GetFields).
3437
func GetFields(ctx *context.Context) (fields *Fields) {
3538
if v := ctx.Values().Get(fieldsContextKey); v != nil {
3639
fields = v.(*Fields)
@@ -60,7 +63,6 @@ func shouldSkip(ctx *context.Context) bool {
6063
}
6164

6265
type (
63-
6466
// Fields is a type alias for memstore.Store, used to set
6567
// more than one field at serve-time. Same as FieldExtractor.
6668
Fields = memstore.Store

0 commit comments

Comments
 (0)