mirror of
https://github.com/kataras/iris.git
synced 2026-01-06 11:37:06 +00:00
accesslog: NEW log broker and many more features
some fixes about context clone, fix response recorder concurrent access, fix reload views with only ParseTemplate and more
This commit is contained in:
@@ -72,7 +72,8 @@
|
||||
* [Sitemap](routing/sitemap/main.go)
|
||||
* Logging
|
||||
* [Request Logger](logging/request-logger/main.go)
|
||||
* [Log requests and responses to access.log](logging/request-logger/accesslog)
|
||||
* [AccessLog: log request & response and more](logging/request-logger/accesslog)
|
||||
* [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)
|
||||
* [Application JSON Logger](logging/json-logger/main.go)
|
||||
|
||||
75
_examples/logging/request-logger/accesslog-broker/main.go
Normal file
75
_examples/logging/request-logger/accesslog-broker/main.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/middleware/accesslog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
/*
|
||||
On this example we will make use of the logs broker.
|
||||
A handler will listen for any incoming logs and render
|
||||
those logs as chunks of JSON to the client (e.g. browser) at real-time.
|
||||
Note that this ^ can be done with Server-Sent Events but for the
|
||||
sake of the example we'll do it using Transfer-Encoding: chunked.
|
||||
*/
|
||||
|
||||
ac := accesslog.File("./access.log")
|
||||
ac.TimeFormat = "2006-01-02 15:04:05"
|
||||
ac.Async = true
|
||||
broker := ac.Broker() // <- IMPORTANT
|
||||
|
||||
app := iris.New()
|
||||
app.UseRouter(ac.Handler)
|
||||
|
||||
app.Get("/", indexHandler)
|
||||
app.Get("/profile/{username}", profileHandler)
|
||||
app.Post("/read_body", readBodyHandler)
|
||||
app.Get("/logs", logsHandler(broker))
|
||||
|
||||
// http://localhost:8080/logs to see the logs at real-time.
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func indexHandler(ctx iris.Context) {
|
||||
ctx.HTML("<h1>Index</h1>")
|
||||
}
|
||||
|
||||
func profileHandler(ctx iris.Context) {
|
||||
username := ctx.Params().Get("username")
|
||||
ctx.HTML("Hello, <strong>%s</strong>!", username)
|
||||
}
|
||||
|
||||
func readBodyHandler(ctx iris.Context) {
|
||||
var request interface{}
|
||||
if err := ctx.ReadBody(&request); err != nil {
|
||||
ctx.StopWithPlainError(iris.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(iris.Map{"message": "OK", "data": request})
|
||||
}
|
||||
|
||||
func logsHandler(b *accesslog.Broker) iris.Handler {
|
||||
return func(ctx iris.Context) {
|
||||
accesslog.Skip(ctx) // skip logging this handler, optionally.
|
||||
|
||||
logs := b.NewListener() // <- IMPORTANT
|
||||
|
||||
ctx.Header("Transfer-Encoding", "chunked")
|
||||
notifyClose := ctx.Request().Context().Done()
|
||||
for {
|
||||
select {
|
||||
case <-notifyClose:
|
||||
b.CloseListener(logs) // <- IMPORTANT
|
||||
|
||||
err := ctx.Request().Context().Err()
|
||||
ctx.Application().Logger().Infof("Listener closed [%v], loop end.", err)
|
||||
return
|
||||
case log := <-logs: // <- IMPORTANT
|
||||
ctx.JSON(log, iris.JSON{Indent: " ", UnescapeHTML: true})
|
||||
ctx.ResponseWriter().Flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
e.Reload(true)
|
||||
|
||||
app := iris.New()
|
||||
app.RegisterView(e)
|
||||
|
||||
@@ -11,6 +11,7 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
e.Reload(true)
|
||||
|
||||
app := iris.New()
|
||||
app.RegisterView(e)
|
||||
|
||||
@@ -9,6 +9,7 @@ func main() {
|
||||
return "Hello, " + name + "!"
|
||||
},
|
||||
})
|
||||
e.Reload(true)
|
||||
|
||||
app := iris.New()
|
||||
app.RegisterView(e)
|
||||
|
||||
@@ -17,6 +17,7 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
e.Reload(true)
|
||||
|
||||
app := iris.New()
|
||||
app.RegisterView(e)
|
||||
|
||||
@@ -19,6 +19,7 @@ func main() {
|
||||
return "Hello, " + name + "!"
|
||||
},
|
||||
})
|
||||
e.Reload(true)
|
||||
|
||||
app := iris.New()
|
||||
app.RegisterView(e)
|
||||
|
||||
Reference in New Issue
Block a user