refine(pkg/log): use strings.Builder instead of bytes.Buffer

pull/536/head
F1renze 5 years ago
parent e0daed38dd
commit dba018a6c4
No known key found for this signature in database
GPG Key ID: FDACE68BA5F003BC
  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