diff --git a/Makefile b/Makefile index dcc4f23..fe07ae4 100644 --- a/Makefile +++ b/Makefile @@ -42,6 +42,12 @@ profile: clean init \ mv ./*.svg bench/ +cpu: + go tool pprof pprof/glg-test.bin pprof/cpu-glg.out + +mem: + go tool pprof --alloc_space pprof/glg-test.bin pprof/mem-glg.out + lint: gometalinter --enable-all . | rg -v comment diff --git a/glg.go b/glg.go index 7ce27ad..0b8a9f0 100644 --- a/glg.go +++ b/glg.go @@ -2,6 +2,8 @@ package glg import ( + "bytes" + "context" "fmt" "io" "net/http" @@ -9,6 +11,7 @@ import ( "path/filepath" "sync" "sync/atomic" + "time" "unsafe" "github.com/kpango/fastime" @@ -20,6 +23,7 @@ type Glg struct { levelCounter *uint32 levelMap sync.Map buffer sync.Pool + ft *fastime.Fastime } // MODE is logging mode (std only, writer only, std & writer) @@ -147,9 +151,10 @@ func New() *Glg { levelCounter: new(uint32), buffer: sync.Pool{ New: func() interface{} { - return make([]byte, bufferSize) + return bytes.NewBuffer(make([]byte, 0, bufferSize)) }, }, + ft: fastime.New().SetFormat(timeFormat).StartTimerD(context.Background(), time.Millisecond), } atomic.StoreUint32(g.levelCounter, uint32(FATAL)) @@ -524,12 +529,12 @@ func (g *Glg) HTTPLogger(name string, handler http.Handler) http.Handler { // HTTPLoggerFunc is simple http access logger func (g *Glg) HTTPLoggerFunc(name string, hf http.HandlerFunc) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - start := fastime.Now() + start := g.ft.Now() hf(w, r) err := g.Logf("Method: %s\tURI: %s\tName: %s\tTime: %s", - r.Method, r.RequestURI, name, fastime.Now().Sub(start).String()) + r.Method, r.RequestURI, name, g.ft.Now().Sub(start).String()) if err != nil { err = g.Error(err) @@ -613,11 +618,11 @@ func (g *Glg) out(level LEVEL, format string, val ...interface{}) error { var ( err error - buf = g.buffer.Get().([]byte) + b = g.buffer.Get().(*bytes.Buffer) log = l.(*logger) ) - buf = append(append(append(append(fastime.Now().AppendFormat(buf[:0], timeFormat), "\t["...), log.tag...), "]:\t"...), format...) + buf := append(append(append(append(append(b.Bytes()[:0], g.ft.FormattedNow()...), "\t["...), log.tag...), "]:\t"...), format...) switch log.writeMode { case writeColorStd: @@ -636,7 +641,8 @@ func (g *Glg) out(level LEVEL, format string, val ...interface{}) error { buf = append(buf, "\n"...) _, err = fmt.Fprintf(io.MultiWriter(log.std, log.writer), *(*string)(unsafe.Pointer(&buf)), val...) } - g.buffer.Put(buf[:0]) + b.Reset() + g.buffer.Put(b) return err } diff --git a/go.mod b/go.mod index 0847b8a..34f0c8b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/kpango/glg -require github.com/kpango/fastime v1.0.3 +require github.com/kpango/fastime v1.0.8 diff --git a/go.sum b/go.sum index 6d3cba3..a374d72 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -github.com/kpango/fastime v1.0.3 h1:brY3CZK7gJR+FruoH0iorbjRfLWzOv3fLfSoWTYX6kA= -github.com/kpango/fastime v1.0.3/go.mod h1:Y5XY5bLG5yc7g2XmMUzc22XYV1XaH+KgUOHkDvLp4SA= +github.com/kpango/fastime v1.0.8 h1:Wif5eocdsIXmMG+8HHfRP/jD6UUl+/OVTJ+sMzvA1+E= +github.com/kpango/fastime v1.0.8/go.mod h1:Y5XY5bLG5yc7g2XmMUzc22XYV1XaH+KgUOHkDvLp4SA=