Skip to content

Commit

Permalink
Using buffer in log
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Apr 24, 2016
1 parent f834370 commit 4fae226
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
# IntelliJ
.idea
*.iml

vendor
4 changes: 2 additions & 2 deletions color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var (
whiteBg = outer(WhtBg)

reset = outer(R)
bolt = outer(B)
bold = outer(B)
dim = outer(D)
italic = outer(I)
underline = outer(U)
Expand Down Expand Up @@ -210,7 +210,7 @@ func (c *Color) Reset(msg interface{}, styles ...string) string {
}

func (c *Color) Bold(msg interface{}, styles ...string) string {
return bolt(msg, styles, c)
return bold(msg, styles, c)
}

func (c *Color) Dim(msg interface{}, styles ...string) string {
Expand Down
18 changes: 18 additions & 0 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package: github.com/labstack/gommon
import:
- package: github.com/valyala/fasttemplate
- package: github.com/mattn/go-colorable
- package: github.com/mattn/go-isatty
- package: github.com/stretchr/testify
subpackages:
- assert
31 changes: 22 additions & 9 deletions log/log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package log

import (
"bytes"
"fmt"
"io"
"os"
Expand All @@ -20,13 +21,14 @@ import (

type (
Logger struct {
prefix string
level uint8
output io.Writer
template *fasttemplate.Template
levels []string
color color.Color
mutex sync.Mutex
prefix string
level uint8
output io.Writer
template *fasttemplate.Template
levels []string
color *color.Color
bufferPool sync.Pool
mutex sync.Mutex
}
)

Expand All @@ -50,6 +52,12 @@ func New(prefix string) (l *Logger) {
level: INFO,
prefix: prefix,
template: l.newTemplate(defaultFormat),
color: color.New(),
bufferPool: sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 256))
},
},
}
l.initLevels()
l.SetOutput(colorable.NewColorableStdout())
Expand Down Expand Up @@ -249,7 +257,9 @@ func Fatalf(format string, args ...interface{}) {
func (l *Logger) log(v uint8, format string, args ...interface{}) {
l.mutex.Lock()
defer l.mutex.Unlock()

buf := l.bufferPool.Get().(*bytes.Buffer)
buf.Reset()
defer l.bufferPool.Put(buf)
_, file, line, _ := runtime.Caller(3)

if v >= l.level {
Expand All @@ -264,7 +274,7 @@ func (l *Logger) log(v uint8, format string, args ...interface{}) {
length := runtime.Stack(stack, true)
message = message + "\n" + string(stack[:length])
}
l.template.ExecuteFunc(l.output, func(w io.Writer, tag string) (int, error) {
_, err := l.template.ExecuteFunc(buf, func(w io.Writer, tag string) (int, error) {
switch tag {
case "time_rfc3339":
return w.Write([]byte(time.Now().Format(time.RFC3339)))
Expand All @@ -284,5 +294,8 @@ func (l *Logger) log(v uint8, format string, args ...interface{}) {
return w.Write([]byte(fmt.Sprintf("[unknown tag %s]", tag)))
}
})
if err == nil {
l.output.Write(buf.Bytes())
}
}
}

0 comments on commit 4fae226

Please sign in to comment.