Przeglądaj źródła

Make New() thread-safe.

Alexey Palazhchenko 9 lat temu
rodzic
commit
33ae297cb8
2 zmienionych plików z 36 dodań i 24 usunięć
  1. 34 22
      log/log.go
  2. 2 2
      log/log_test.go

+ 34 - 22
log/log.go

@@ -15,6 +15,8 @@ import (
 type (
 	Logger struct {
 		level  Level
+		levels []string
+		color  color.Color
 		out    io.Writer
 		prefix string
 		mu     sync.Mutex
@@ -34,7 +36,6 @@ const (
 
 var (
 	global = New("-")
-	levels []string
 )
 
 func New(prefix string) (l *Logger) {
@@ -46,6 +47,26 @@ func New(prefix string) (l *Logger) {
 	return
 }
 
+func (l *Logger) initLevels() {
+	l.levels = []string{
+		l.color.Blue("DEBUG"),
+		l.color.Green("INFO"),
+		l.color.Yellow("WARN"),
+		l.color.Red("ERROR"),
+		l.color.RedBg("FATAL"),
+	}
+}
+
+func (l *Logger) DisableColor() {
+	l.color.Disable()
+	l.initLevels()
+}
+
+func (l *Logger) EnableColor() {
+	l.color.Enable()
+	l.initLevels()
+}
+
 func (l *Logger) SetPrefix(p string) {
 	l.prefix = p
 }
@@ -60,14 +81,11 @@ func (l *Logger) Level() Level {
 
 func (l *Logger) SetOutput(w io.Writer) {
 	l.out = w
-	color.Disable()
+	l.DisableColor()
 
 	if w, ok := w.(*os.File); ok && isatty.IsTerminal(w.Fd()) {
-		color.Enable()
+		l.EnableColor()
 	}
-
-	// NOTE: Reintialize levels to reflect color enable/disable call.
-	initLevels()
 }
 
 func (l *Logger) Print(i ...interface{}) {
@@ -121,6 +139,14 @@ func (l *Logger) Fatalf(format string, args ...interface{}) {
 	os.Exit(1)
 }
 
+func DisableColor() {
+	global.DisableColor()
+}
+
+func EnableColor() {
+	global.EnableColor()
+}
+
 func SetPrefix(p string) {
 	global.SetPrefix(p)
 }
@@ -187,26 +213,12 @@ func (l *Logger) log(v Level, format string, args ...interface{}) {
 
 	if v >= l.level {
 		if format == "" && len(args) > 0 {
-			args[0] = fmt.Sprintf("%s|%s|%s", levels[v], l.prefix, args[0])
+			args[0] = fmt.Sprintf("%s|%s|%s", l.levels[v], l.prefix, args[0])
 			fmt.Fprintln(l.out, args...)
 		} else {
 			// TODO: Improve performance
-			f := fmt.Sprintf("%s|%s|%s\n", levels[v], l.prefix, format)
+			f := fmt.Sprintf("%s|%s|%s\n", l.levels[v], l.prefix, format)
 			fmt.Fprintf(l.out, f, args...)
 		}
 	}
 }
-
-func initLevels() {
-	levels = []string{
-		color.Blue("DEBUG"),
-		color.Green("INFO"),
-		color.Yellow("WARN"),
-		color.Red("ERROR"),
-		color.RedBg("FATAL"),
-	}
-}
-
-func init() {
-	initLevels()
-}

+ 2 - 2
log/log_test.go

@@ -27,7 +27,7 @@ func TestLog(t *testing.T) {
 	l := New("test")
 	b := new(bytes.Buffer)
 	l.SetOutput(b)
-	// l.DisableColor()
+	l.DisableColor()
 	l.SetLevel(INFO)
 	test(l, t)
 	assert.Contains(t, b.String(), "print\n")
@@ -41,7 +41,7 @@ func TestLog(t *testing.T) {
 func TestGlobal(t *testing.T) {
 	b := new(bytes.Buffer)
 	SetOutput(b)
-	// DisableColor()
+	DisableColor()
 	SetLevel(INFO)
 	test(global, t)
 	assert.Contains(t, b.String(), "print\n")