1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 02:17:05 +00:00

accesslog: log error bodies and communicate with the recover middleware

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-09-10 14:47:14 +03:00
parent cf0338d342
commit ae67987f55
7 changed files with 142 additions and 15 deletions

View File

@@ -3,6 +3,7 @@ package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/accesslog"
"github.com/kataras/iris/v12/middleware/recover"
)
func main() {
@@ -16,13 +17,18 @@ func main() {
ac := accesslog.File("./access.log")
ac.TimeFormat = "2006-01-02 15:04:05"
// ac.KeepMultiLineError = false // set to false to print errors as one line.
// Optionally run logging after response has sent:
// ac.Async = true
broker := ac.Broker() // <- IMPORTANT
app := iris.New()
app.UseRouter(ac.Handler)
app.UseRouter(recover.New())
app.OnErrorCode(iris.StatusNotFound, notFoundHandler)
app.Get("/panic", testPanic)
app.Get("/", indexHandler)
app.Get("/profile/{username}", profileHandler)
app.Post("/read_body", readBodyHandler)
@@ -36,6 +42,22 @@ func main() {
app.Listen(":8080")
}
func notFoundHandler(ctx iris.Context) {
ctx.Application().Logger().Infof("Not Found Handler for: %s", ctx.Path())
suggestPaths := ctx.FindClosest(3)
if len(suggestPaths) == 0 {
ctx.WriteString("The page you're looking does not exist.")
return
}
ctx.HTML("Did you mean?<ul>")
for _, s := range suggestPaths {
ctx.HTML(`<li><a href="%s">%s</a></li>`, s, s)
}
ctx.HTML("</ul>")
}
func indexHandler(ctx iris.Context) {
ctx.HTML("<h1>Index</h1>")
}
@@ -55,6 +77,10 @@ func readBodyHandler(ctx iris.Context) {
ctx.JSON(iris.Map{"message": "OK", "data": request})
}
func testPanic(ctx iris.Context) {
panic("PANIC HERE")
}
func logsHandler(b *accesslog.Broker) iris.Handler {
return func(ctx iris.Context) {
// accesslog.Skip(ctx) // or inline skip.