Browse Source

Print log level and prefix for no format api

Signed-off-by: Vishal Rana <vr@labstack.com>
Vishal Rana 9 years ago
parent
commit
28bf9248c3
2 changed files with 10 additions and 8 deletions
  1. 7 4
      log/log.go
  2. 3 4
      log/log_test.go

+ 7 - 4
log/log.go

@@ -6,9 +6,10 @@ import (
 	"os"
 	"sync"
 
-	"github.com/labstack/gommon/color"
 	"github.com/mattn/go-colorable"
 	"github.com/mattn/go-isatty"
+
+	"github.com/labstack/gommon/color"
 )
 
 type (
@@ -18,6 +19,7 @@ type (
 		prefix string
 		mu     sync.Mutex
 	}
+
 	Level uint8
 )
 
@@ -184,11 +186,12 @@ func (l *Logger) log(v Level, format string, args ...interface{}) {
 	defer l.mu.Unlock()
 
 	if v >= l.level {
-		if format == "" {
-			fmt.Println(args...)
+		if format == "" && len(args) > 0 {
+			args[0] = fmt.Sprintf("%s|%s|%s", levels[v], l.prefix, args[0])
+			fmt.Fprintln(l.out, args...)
 		} else {
 			// TODO: Improve performance
-			f := fmt.Sprintf("%s|%s|%v\n", levels[v], l.prefix, format)
+			f := fmt.Sprintf("%s|%s|%s\n", levels[v], l.prefix, format)
 			fmt.Fprintf(l.out, f, args...)
 		}
 	}

+ 3 - 4
log/log_test.go

@@ -4,9 +4,10 @@ import (
 	"bytes"
 	"testing"
 
-	"github.com/stretchr/testify/assert"
 	"os"
 	"os/exec"
+
+	"github.com/stretchr/testify/assert"
 )
 
 func TestLog(t *testing.T) {
@@ -18,14 +19,13 @@ func TestLog(t *testing.T) {
 	assert.Contains(t, b.String(), "debugf")
 	assert.Contains(t, b.String(), "warn")
 	assert.Contains(t, b.String(), "warnf")
-	// assert.Contains(t, b.String(), "fatal")
 
 	b.Reset()
 	SetOutput(b)
 	test(global, WARN, t)
 	assert.NotContains(t, b.String(), "info")
 	assert.Contains(t, b.String(), "warn")
-	// assert.Contains(t, b.String(), "fatal")
+	println(b.String())
 }
 
 func TestFatal(t *testing.T) {
@@ -69,5 +69,4 @@ func test(l *Logger, v Level, t *testing.T) {
 	l.Warnf("warn%s", "f")
 	l.Error("error")
 	l.Errorf("error%s", "f")
-	// l.Fatal("fatal")
 }