1
0
mirror of https://github.com/kataras/iris.git synced 2026-03-04 15:36:03 +00:00

accesslog: add CSV format

relative to: https://github.com/kataras/iris/issues/1601
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-09-12 12:34:59 +03:00
parent a30bbb61f7
commit 2b342a5122
11 changed files with 448 additions and 133 deletions

View File

@@ -74,6 +74,7 @@
* [Request Logger](logging/request-logger/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)
* [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)

View File

@@ -0,0 +1,5 @@
Timestamp,Latency,Code,Method,Path,IP,Req Values,In,Out,Request,Response
1599900695933,0s,200,GET,/,::1,sleep=35ms,575,81,,Index
1599900696207,0s,404,GET,/notfound,::1,,572,92,,Not Found
1599900696693,0s,200,GET,/,::1,,564,81,,Index
1599900697988,1s,200,GET,/,::1,sleep=1s,573,81,,Index

View File

@@ -0,0 +1,34 @@
package main
import (
"time"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/accesslog"
)
func main() {
app := iris.New()
ac := accesslog.File("access_log.csv")
ac.SetFormatter(&accesslog.CSV{
AutoFlush: true,
Header: true,
// DateScript: "FROM_UNIX",
LatencyRound: time.Second,
})
app.UseRouter(ac.Handler)
app.Get("/", index)
app.Listen(":8080")
}
func index(ctx iris.Context) {
if sleepDur := ctx.URLParam("sleep"); sleepDur != "" {
if d, err := time.ParseDuration(sleepDur); err == nil {
time.Sleep(d)
}
}
ctx.WriteString("Index")
}