Skip to content

Commit

Permalink
[patch] add formatted store (#23)
Browse files Browse the repository at this point in the history
* fix

* [patch] add formatted store
  • Loading branch information
Yusuke Kato authored Feb 24, 2019
1 parent a3127ba commit 70f3b9f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 12 additions & 6 deletions glg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
package glg

import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
"unsafe"

"github.com/kpango/fastime"
Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit 70f3b9f

Please sign in to comment.