Merge pull request #536 from F1renze/refine/log

refine(pkg/log): use strings.Builder instead of bytes.Buffer
pull/537/head
Terry.Mao 5 years ago committed by GitHub
commit 04948fd5cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      pkg/log/dsn.go
  2. 24
      pkg/log/pattern.go

@ -1,7 +1,6 @@
package log
import (
"bytes"
"fmt"
"strconv"
"strings"
@ -25,14 +24,13 @@ func (f *logFilter) Set(value string) error {
}
func (m verboseModule) String() string {
// FIXME strings.Builder
var buf bytes.Buffer
var b strings.Builder
for k, v := range m {
buf.WriteString(k)
buf.WriteString(strconv.FormatInt(int64(v), 10))
buf.WriteString(",")
b.WriteString(k)
b.WriteString(strconv.FormatInt(int64(v), 10))
b.WriteString(",")
}
return buf.String()
return b.String()
}
// Set sets the value of the named command-line flag.

@ -1,7 +1,6 @@
package log
import (
"bytes"
"fmt"
"io"
"path"
@ -28,7 +27,7 @@ var patternMap = map[string]func(map[string]interface{}) string{
// newPatternRender new pattern render
func newPatternRender(format string) Render {
p := &pattern{
bufPool: sync.Pool{New: func() interface{} { return &bytes.Buffer{} }},
bufPool: sync.Pool{New: func() interface{} { return &strings.Builder{} }},
}
b := make([]byte, 0, len(format))
for i := 0; i < len(format); i++ {
@ -65,32 +64,31 @@ type pattern struct {
// Render implemet Formater
func (p *pattern) Render(w io.Writer, d map[string]interface{}) error {
buf := p.bufPool.Get().(*bytes.Buffer)
builder := p.bufPool.Get().(*strings.Builder)
defer func() {
buf.Reset()
p.bufPool.Put(buf)
builder.Reset()
p.bufPool.Put(builder)
}()
for _, f := range p.funcs {
buf.WriteString(f(d))
builder.WriteString(f(d))
}
_, err := buf.WriteTo(w)
_, err := w.Write([]byte(builder.String()))
return err
}
// Render implemet Formater as string
func (p *pattern) RenderString(d map[string]interface{}) string {
// TODO strings.Builder
buf := p.bufPool.Get().(*bytes.Buffer)
builder := p.bufPool.Get().(*strings.Builder)
defer func() {
buf.Reset()
p.bufPool.Put(buf)
builder.Reset()
p.bufPool.Put(builder)
}()
for _, f := range p.funcs {
buf.WriteString(f(d))
builder.WriteString(f(d))
}
return buf.String()
return builder.String()
}
func textFactory(text string) func(map[string]interface{}) string {

Loading…
Cancel
Save