فهرست منبع

Fixed enable/disable colored log

Signed-off-by: Vishal Rana <vr@labstack.com>
Vishal Rana 10 سال پیش
والد
کامیت
7921d4bb75
4فایلهای تغییر یافته به همراه38 افزوده شده و 47 حذف شده
  1. 3 3
      gytes/gytes.go
  2. 0 1
      log/.envrc
  3. 29 37
      log/log.go
  4. 6 6
      log/log_test.go

+ 3 - 3
gytes/gytes.go

@@ -37,8 +37,8 @@ func (g *Gytes) Format(b uint64) string {
 		return strconv.FormatUint(b, 10) + " B"
 	}
 	bb := float64(b)
-	uu := float64(unit)
-	x := math.Floor(math.Log(bb) / math.Log(uu))
+	uunit := float64(unit)
+	x := math.Floor(math.Log(bb) / math.Log(uunit))
 	pre := make([]byte, 1, 2)
 	pre[0] = "KMGTPE"[uint8(x)-1]
 	if g.iec {
@@ -46,7 +46,7 @@ func (g *Gytes) Format(b uint64) string {
 		pre[1] = 'i'
 	}
 	// TODO: Improve performance?
-	return fmt.Sprintf("%.02f %sB", bb/math.Pow(uu, x), pre)
+	return fmt.Sprintf("%.02f %sB", bb/math.Pow(uunit, x), pre)
 
 }
 

+ 0 - 1
log/.envrc

@@ -1 +0,0 @@
-export GOPATH=$GOPATH:`pwd`

+ 29 - 37
log/log.go

@@ -18,47 +18,33 @@ type (
 		err    io.Writer
 		prefix string
 		sync.Mutex
-		// lock bool
 	}
 	Level uint8
 )
 
 const (
-	trace = iota
-	debug
-	info
-	notice
-	warn
-	err
-	fatal
-	off = 10
+	TRACE = iota
+	DEBUG
+	INFO
+	NOTICE
+	WARN
+	ERROR
+	FATAL
+	OFF = 10
 )
 
 var (
 	global = New("-")
-	levels = []string{
-		color.Cyan("TRACE"),
-		color.Blue("DEBUG"),
-		color.Green("INFO"),
-		color.Magenta("NOTICE"),
-		color.Yellow("WARN"),
-		color.Red("ERROR"),
-		color.RedBg("FATAL"),
-	}
+	levels []string
 )
 
 func New(prefix string) (l *Logger) {
 	l = &Logger{
-		level:  info,
-		out:    colorable.NewColorableStdout(),
-		err:    colorable.NewColorableStderr(),
+		level:  INFO,
 		prefix: prefix,
 	}
-
-	if isatty.IsTerminal(os.Stdout.Fd()) {
-		color.Enable()
-	}
-
+	l.SetOutput(colorable.NewColorableStdout())
+	l.err = colorable.NewColorableStderr()
 	return
 }
 
@@ -73,41 +59,51 @@ func (l *Logger) SetLevel(v Level) {
 func (l *Logger) SetOutput(w io.Writer) {
 	l.out = w
 	l.err = w
+	color.Disable()
 
 	switch w := w.(type) {
 	case *os.File:
 		if isatty.IsTerminal(w.Fd()) {
 			color.Enable()
 		}
+		levels = []string{
+			color.Cyan("TRACE"),
+			color.Blue("DEBUG"),
+			color.Green("INFO"),
+			color.Magenta("NOTICE"),
+			color.Yellow("WARN"),
+			color.Red("ERROR"),
+			color.RedBg("FATAL"),
+		}
 	}
 }
 
 func (l *Logger) Trace(msg interface{}, args ...interface{}) {
-	l.log(trace, l.out, msg, args...)
+	l.log(TRACE, l.out, msg, args...)
 }
 
 func (l *Logger) Debug(msg interface{}, args ...interface{}) {
-	l.log(debug, l.out, msg, args...)
+	l.log(DEBUG, l.out, msg, args...)
 }
 
 func (l *Logger) Info(msg interface{}, args ...interface{}) {
-	l.log(info, l.out, msg, args...)
+	l.log(INFO, l.out, msg, args...)
 }
 
 func (l *Logger) Notice(msg interface{}, args ...interface{}) {
-	l.log(notice, l.out, msg, args...)
+	l.log(NOTICE, l.out, msg, args...)
 }
 
 func (l *Logger) Warn(msg interface{}, args ...interface{}) {
-	l.log(warn, l.out, msg, args...)
+	l.log(WARN, l.out, msg, args...)
 }
 
 func (l *Logger) Error(msg interface{}, args ...interface{}) {
-	l.log(err, l.err, msg, args...)
+	l.log(ERROR, l.err, msg, args...)
 }
 
 func (l *Logger) Fatal(msg interface{}, args ...interface{}) {
-	l.log(fatal, l.err, msg, args...)
+	l.log(FATAL, l.err, msg, args...)
 }
 
 func SetPrefix(p string) {
@@ -161,7 +157,3 @@ func (l *Logger) log(v Level, w io.Writer, msg interface{}, args ...interface{})
 		fmt.Fprintf(w, f, args...)
 	}
 }
-
-func init() {
-	color.Disable()
-}

+ 6 - 6
log/log_test.go

@@ -4,14 +4,14 @@ import "testing"
 
 func TestLog(t *testing.T) {
 	l := New("test")
-	test(l, trace, t)
-	test(global, trace, t)
-	test(l, notice, t)
-	test(global, notice, t)
+	test(l, TRACE, t)
+	test(global, TRACE, t)
+	test(l, NOTICE, t)
+	test(global, NOTICE, t)
 }
 
-func test(l *Logger, v Level, t *testing.T) {
-	l.SetLevel(trace)
+func test(l *Logger, v level, t *testing.T) {
+	l.SetLevel(TRACE)
 	l.Trace("trace")
 	l.Debug("debug")
 	l.Info("info")