1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-09 13:05:56 +00:00

accesslog: improvements and new features

relative to: #1601 and #1624
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-09-13 02:56:22 +03:00
parent 7d5789c3de
commit 4845b77177
16 changed files with 612 additions and 261 deletions

View File

@@ -19,9 +19,9 @@ func main() {
ac := accesslog.File("./access.log")
defer ac.Close()
ac.AddOutput(os.Stdout)
ac.RequestBody = true
// Set to false to print errors as one line:
// ac.KeepMultiLineError = false
// Set the "depth" of a panic trace:

View File

@@ -1,5 +1,7 @@
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
1599952696740,0s,200,GET,/,::1,,0,5,,Index
1599952696774,0s,404,GET,/favicon.ico,::1,,0,9,,Not Found
1599952697996,0s,200,GET,/,::1,,0,5,,Index
1599952698130,0s,200,GET,/,::1,,0,5,,Index
1599952698261,0s,200,GET,/,::1,,0,5,,Index
1599952741189,1.01113s,200,GET,/,::1,sleep=1s,0,5,,Index

View File

@@ -10,11 +10,10 @@ import (
func main() {
app := iris.New()
ac := accesslog.File("access_log.csv")
ac.ResponseBody = true
ac.SetFormatter(&accesslog.CSV{
AutoFlush: true,
Header: true,
Header: true,
// DateScript: "FROM_UNIX",
LatencyRound: time.Second,
})
app.UseRouter(ac.Handler)

View File

@@ -13,21 +13,32 @@ 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
// The default configuration:
ac.Delim = '|'
ac.TimeFormat = "2006-01-02 15:04:05"
ac.Async = false
ac.IP = true
ac.BytesReceivedBody = true
ac.BytesSentBody = true
ac.BytesReceived = false
ac.BytesSent = false
ac.BodyMinify = true
ac.RequestBody = true
ac.ResponseBody = false
ac.KeepMultiLineError = true
ac.PanicLog = accesslog.LogHandler
// Defaults to false.
ac.Async = false
// Set Custom Formatter:
ac.SetFormatter(&accesslog.JSON{})
// ac.SetFormatter(&accesslog.CSV{})
// ac.SetFormatter(&accesslog.Template{Text: "{{.Code}}"})
return ac
}
func main() {
ac := makeAccessLog()
defer ac.Close()
defer ac.Close() // Close the underline file.
app := iris.New()
// Register the middleware (UseRouter to catch http errors too).

View File

@@ -1,6 +1,7 @@
package main // See https://github.com/kataras/iris/issues/1601
import (
"bufio"
"time"
"github.com/kataras/iris/v12"
@@ -29,7 +30,7 @@ func makeAccessLog() *accesslog.AccessLog {
// Initialize a new access log middleware.
// Accepts an `io.Writer`.
ac := accesslog.New(w)
ac := accesslog.New(bufio.NewWriter(w))
// ac.TimeFormat = "2006-01-02 15:04:05" // default
// Example of adding more than one field to the logger.
@@ -73,6 +74,8 @@ func makeAccessLog() *accesslog.AccessLog {
ac.ResponseBody = false
ac.BytesReceived = false
ac.BytesSent = false
ac.BytesReceivedBody = false
ac.BytesSentBody = false
Add second output:
ac.AddOutput(app.Logger().Printer)