mirror of
https://github.com/kataras/iris.git
synced 2026-01-08 12:31:58 +00:00
add accesslog middleware (rel: #1601)
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
2020-08-22 00:44:20|993.3µs|POST|/read_body||200|{"id":10,"name":"Tim","age":22}|{"message":"OK"}|
|
||||
2020-08-22 00:44:30|0s|POST|/read_body||400||error(invalid character 'a' looking for beginning of object key string)||
|
||||
2020-08-22 03:02:41|1ms|GET|/|a=1 b=2|200||<h1>Hello index</h1>|
|
||||
2020-08-22 03:15:29|968.8µs|GET|/public|file=public|404|||
|
||||
2020-08-22 03:03:42|0s|GET|/user/kataras|username=kataras|200||Hello, kataras!|
|
||||
2020-08-22 03:05:40|0s|GET|/user/kataras|username=kataras a_query_parameter=name|200||Hello, kataras!|
|
||||
47
_examples/logging/request-logger/accesslog/main.go
Normal file
47
_examples/logging/request-logger/accesslog/main.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package main // See https://github.com/kataras/iris/issues/1601
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/middleware/accesslog"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
ac := accesslog.File("./access.log")
|
||||
defer ac.Close()
|
||||
iris.RegisterOnInterrupt(func() {
|
||||
ac.Close()
|
||||
})
|
||||
|
||||
// Register the middleware (UseRouter to catch http errors too).
|
||||
app.UseRouter(ac.Handler)
|
||||
//
|
||||
|
||||
// Register some routes...
|
||||
app.HandleDir("/", iris.Dir("./public"))
|
||||
|
||||
app.Get("/user/{username}", userHandler)
|
||||
app.Post("/read_body", readBodyHandler)
|
||||
app.Get("/html_response", htmlResponse)
|
||||
//
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
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 userHandler(ctx iris.Context) {
|
||||
ctx.Writef("Hello, %s!", ctx.Params().Get("username"))
|
||||
}
|
||||
|
||||
func htmlResponse(ctx iris.Context) {
|
||||
ctx.HTML("<h1>HTML Response</h1>")
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<h1>Hello index</h1>
|
||||
Reference in New Issue
Block a user