1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +02:00

Merge pull request #482 from zjx20/patch-1

Atomically write logs in the Logger middleware
This commit is contained in:
Vishal Rana 2016-04-24 08:14:40 -07:00
commit 61fabee05e

View File

@ -1,11 +1,13 @@
package middleware
import (
"bytes"
"fmt"
"io"
"net"
"os"
"strconv"
"sync"
"time"
"github.com/labstack/echo"
@ -39,6 +41,7 @@ type (
template *fasttemplate.Template
color *color.Color
bufferPool sync.Pool
}
)
@ -73,6 +76,11 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
if w, ok := config.Output.(*os.File); !ok || !isatty.IsTerminal(w.Fd()) {
config.color.Disable()
}
config.bufferPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 256))
},
}
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
@ -84,7 +92,11 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
}
stop := time.Now()
_, err = config.template.ExecuteFunc(config.Output, func(w io.Writer, tag string) (int, error) {
buffer := config.bufferPool.Get().(*bytes.Buffer)
buffer.Reset()
defer config.bufferPool.Put(buffer)
_, err = config.template.ExecuteFunc(buffer, func(w io.Writer, tag string) (int, error) {
switch tag {
case "time_rfc3339":
return w.Write([]byte(time.Now().Format(time.RFC3339)))
@ -128,6 +140,9 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
return w.Write([]byte(fmt.Sprintf("[unknown tag %s]", tag)))
}
})
if err == nil {
config.Output.Write(buffer.Bytes())
}
return
}
}