lib/logger: Add test for stack level (#5430)
This commit is contained in:
parent
597cee67d3
commit
04070b4848
@ -6,6 +6,7 @@ package logger
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -69,6 +70,10 @@ type logger struct {
|
|||||||
var DefaultLogger = New()
|
var DefaultLogger = New()
|
||||||
|
|
||||||
func New() Logger {
|
func New() Logger {
|
||||||
|
return newLogger(os.Stdout)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newLogger(w io.Writer) Logger {
|
||||||
res := &logger{
|
res := &logger{
|
||||||
facilities: make(map[string]string),
|
facilities: make(map[string]string),
|
||||||
debug: make(map[string]struct{}),
|
debug: make(map[string]struct{}),
|
||||||
@ -78,7 +83,7 @@ func New() Logger {
|
|||||||
res.logger = log.New(ioutil.Discard, "", 0)
|
res.logger = log.New(ioutil.Discard, "", 0)
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
res.logger = log.New(os.Stdout, "", DefaultFlags)
|
res.logger = log.New(w, "", DefaultFlags)
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,9 @@
|
|||||||
package logger
|
package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -149,5 +151,18 @@ func TestRecorder(t *testing.T) {
|
|||||||
if lines[0].Message != "hah" {
|
if lines[0].Message != "hah" {
|
||||||
t.Errorf("incorrect line: %s", lines[0].Message)
|
t.Errorf("incorrect line: %s", lines[0].Message)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStackLevel(t *testing.T) {
|
||||||
|
b := new(bytes.Buffer)
|
||||||
|
l := newLogger(b)
|
||||||
|
|
||||||
|
l.SetFlags(log.Lshortfile)
|
||||||
|
l.Infoln("testing")
|
||||||
|
res := b.String()
|
||||||
|
|
||||||
|
if !strings.Contains(res, "logger_test.go:") {
|
||||||
|
t.Logf("%q", res)
|
||||||
|
t.Error("Should identify this file as the source (bad level?)")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user