add an accesslog simple example

This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-09-12 13:45:00 +03:00
parent 2b342a5122
commit 7d5789c3de
No known key found for this signature in database
GPG Key ID: 5DBE766BD26A54E7
4 changed files with 45 additions and 5 deletions

View File

@ -72,6 +72,7 @@
* [Sitemap](routing/sitemap/main.go) * [Sitemap](routing/sitemap/main.go)
* Logging * Logging
* [Request Logger](logging/request-logger/main.go) * [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: log request & response and more](logging/request-logger/accesslog)
* [AccessLog: custom fields and template](logging/request-logger/accesslog-template/main.go) * [AccessLog: custom fields and template](logging/request-logger/accesslog-template/main.go)
* [AccessLog: CSV Format](logging/request-logger/accesslog-csv/main.go) * [AccessLog: CSV Format](logging/request-logger/accesslog-csv/main.go)

View 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")
}

View File

@ -15,6 +15,7 @@ func main() {
app := iris.New() app := iris.New()
ac := accesslog.File("./access.log").AddOutput(app.Logger().Printer) ac := accesslog.File("./access.log").AddOutput(app.Logger().Printer)
defer ac.Close()
// 1. Register a field. // 1. Register a field.
ac.AddFields(func(ctx iris.Context, fields *accesslog.Fields) { ac.AddFields(func(ctx iris.Context, fields *accesslog.Fields) {

View File

@ -30,7 +30,6 @@ func makeAccessLog() *accesslog.AccessLog {
// Initialize a new access log middleware. // Initialize a new access log middleware.
// Accepts an `io.Writer`. // Accepts an `io.Writer`.
ac := accesslog.New(w) ac := accesslog.New(w)
defer ac.Close()
// ac.TimeFormat = "2006-01-02 15:04:05" // default // ac.TimeFormat = "2006-01-02 15:04:05" // default
// Example of adding more than one field to the logger. // Example of adding more than one field to the logger.
@ -94,11 +93,7 @@ func makeAccessLog() *accesslog.AccessLog {
func main() { func main() {
ac := makeAccessLog() ac := makeAccessLog()
defer ac.Close() defer ac.Close()
iris.RegisterOnInterrupt(func() {
ac.Close()
})
app := iris.New() app := iris.New()
// Register the middleware (UseRouter to catch http errors too). // Register the middleware (UseRouter to catch http errors too).