accesslog: add FileUnbuffered helper

This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-12-29 16:38:24 +02:00
parent 3e6fd79906
commit a2d0b86815
No known key found for this signature in database
GPG Key ID: 5DBE766BD26A54E7
2 changed files with 14 additions and 2 deletions

2
go.mod
View File

@ -21,7 +21,7 @@ require (
github.com/json-iterator/go v1.1.10
github.com/kataras/blocks v0.0.4
github.com/kataras/golog v0.1.6
github.com/kataras/jwt v0.0.8
github.com/kataras/jwt v0.0.9
github.com/kataras/neffos v0.0.18
github.com/kataras/pio v0.0.10
github.com/kataras/sitemap v0.0.5

View File

@ -323,6 +323,18 @@ func New(w io.Writer) *AccessLog {
//
// It panics on error.
func File(path string) *AccessLog {
f := mustOpenFile(path)
return New(bufio.NewReadWriter(bufio.NewReader(f), bufio.NewWriter(f)))
}
// FileUnbuffered same as File but it does not buffers the data,
// it flushes the loggers contents as soon as possible.
func FileUnbuffered(path string) *AccessLog {
f := mustOpenFile(path)
return New(f)
}
func mustOpenFile(path string) *os.File {
// Note: we add os.RDWR in order to be able to read from it,
// some formatters (e.g. CSV) needs that.
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
@ -330,7 +342,7 @@ func File(path string) *AccessLog {
panic(err)
}
return New(bufio.NewReadWriter(bufio.NewReader(f), bufio.NewWriter(f)))
return f
}
// Broker creates or returns the broker.