Преглед изворни кода

Using buffer in log

Signed-off-by: Vishal Rana <vr@labstack.com>
Vishal Rana пре 9 година
родитељ
комит
4fae226dd6
5 измењених фајлова са 51 додато и 15 уклоњено
  1. 1 4
      .gitignore
  2. 2 2
      color/color.go
  3. 18 0
      glide.lock
  4. 8 0
      glide.yaml
  5. 22 9
      log/log.go

+ 1 - 4
.gitignore

@@ -1,4 +1 @@
-# IntelliJ
-.idea
-*.iml
-
+vendor

+ 2 - 2
color/color.go

@@ -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)
@@ -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 {

+ 18 - 0
glide.lock

@@ -0,0 +1,18 @@
+hash: e55ed1b26ebd131b2a6f7bb5bcbac90529cccd68976513e2648f38fe55fdd807
+updated: 2016-04-24T08:29:37.021386935-07:00
+imports:
+- name: github.com/mattn/go-colorable
+  version: bc69d6cebca8dd5675346a34a1c545b67dc74d5e
+- name: github.com/mattn/go-isatty
+  version: 56b76bdf51f7708750eac80fa38b952bb9f32639
+- name: github.com/stretchr/testify
+  version: c5d7a69bf8a2c9c374798160849c071093e41dd1
+  subpackages:
+  - assert
+- name: github.com/valyala/fasttemplate
+  version: 3b874956e03f1636d171bda64b130f9135f42cff
+- name: golang.org/x/sys
+  version: f64b50fbea64174967a8882830d621a18ee1548e
+  subpackages:
+  - unix
+devImports: []

+ 8 - 0
glide.yaml

@@ -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

+ 22 - 9
log/log.go

@@ -1,6 +1,7 @@
 package log
 
 import (
+	"bytes"
 	"fmt"
 	"io"
 	"os"
@@ -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
 	}
 )
 
@@ -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())
@@ -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 {
@@ -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)))
@@ -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())
+		}
 	}
 }