mirror of
https://github.com/kataras/iris.git
synced 2025-03-14 08:26:26 +01:00
add an accesslog simple example
This commit is contained in:
parent
2b342a5122
commit
7d5789c3de
|
@ -72,6 +72,7 @@
|
|||
* [Sitemap](routing/sitemap/main.go)
|
||||
* Logging
|
||||
* [Request Logger](logging/request-logger/main.go)
|
||||
* [AccessLog: simple example](logging/request-logger/accesslog-simple/main.go)
|
||||
* [AccessLog: log request & response and more](logging/request-logger/accesslog)
|
||||
* [AccessLog: custom fields and template](logging/request-logger/accesslog-template/main.go)
|
||||
* [AccessLog: CSV Format](logging/request-logger/accesslog-csv/main.go)
|
||||
|
|
43
_examples/logging/request-logger/accesslog-simple/main.go
Normal file
43
_examples/logging/request-logger/accesslog-simple/main.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/middleware/accesslog"
|
||||
)
|
||||
|
||||
// Default line format:
|
||||
// Time|Latency|Code|Method|Path|IP|Path Params Query Fields|Bytes Received|Bytes Sent|Request|Response|
|
||||
//
|
||||
// Read the example and its comments carefully.
|
||||
func makeAccessLog() *accesslog.AccessLog {
|
||||
// Initialize a new access log middleware.
|
||||
ac := accesslog.File("./access.log")
|
||||
|
||||
// Defaults to true. Change to false for better performance.
|
||||
ac.RequestBody = false
|
||||
ac.ResponseBody = false
|
||||
ac.BytesReceived = false
|
||||
ac.BytesSent = false
|
||||
|
||||
// Defaults to false.
|
||||
ac.Async = false
|
||||
|
||||
return ac
|
||||
}
|
||||
|
||||
func main() {
|
||||
ac := makeAccessLog()
|
||||
defer ac.Close()
|
||||
|
||||
app := iris.New()
|
||||
// Register the middleware (UseRouter to catch http errors too).
|
||||
app.UseRouter(ac.Handler)
|
||||
|
||||
app.Get("/", indexHandler)
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func indexHandler(ctx iris.Context) {
|
||||
ctx.WriteString("OK")
|
||||
}
|
|
@ -15,6 +15,7 @@ func main() {
|
|||
|
||||
app := iris.New()
|
||||
ac := accesslog.File("./access.log").AddOutput(app.Logger().Printer)
|
||||
defer ac.Close()
|
||||
|
||||
// 1. Register a field.
|
||||
ac.AddFields(func(ctx iris.Context, fields *accesslog.Fields) {
|
||||
|
|
|
@ -30,7 +30,6 @@ func makeAccessLog() *accesslog.AccessLog {
|
|||
// Initialize a new access log middleware.
|
||||
// Accepts an `io.Writer`.
|
||||
ac := accesslog.New(w)
|
||||
defer ac.Close()
|
||||
// ac.TimeFormat = "2006-01-02 15:04:05" // default
|
||||
|
||||
// Example of adding more than one field to the logger.
|
||||
|
@ -94,11 +93,7 @@ func makeAccessLog() *accesslog.AccessLog {
|
|||
|
||||
func main() {
|
||||
ac := makeAccessLog()
|
||||
|
||||
defer ac.Close()
|
||||
iris.RegisterOnInterrupt(func() {
|
||||
ac.Close()
|
||||
})
|
||||
|
||||
app := iris.New()
|
||||
// Register the middleware (UseRouter to catch http errors too).
|
||||
|
|
Loading…
Reference in New Issue
Block a user