75 lines
1.4 KiB
Go
75 lines
1.4 KiB
Go
|
package colog
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
type JSONFormatTest struct {
|
||
|
entry Entry
|
||
|
prefix string
|
||
|
flags int
|
||
|
tfmt string
|
||
|
lnum bool
|
||
|
output string
|
||
|
}
|
||
|
|
||
|
var JSONFormatTests = []JSONFormatTest{
|
||
|
{
|
||
|
entry: Entry{
|
||
|
Level: LInfo,
|
||
|
Message: []byte("some message"),
|
||
|
},
|
||
|
output: `{"level":"info","message":"some message"}` + "\n",
|
||
|
},
|
||
|
{
|
||
|
entry: Entry{
|
||
|
Time: TTime,
|
||
|
Level: LDebug,
|
||
|
Message: []byte("some message"),
|
||
|
},
|
||
|
flags: log.Ldate,
|
||
|
output: `{"level":"debug","time":"2015/8/1","message":"some message"}` + "\n",
|
||
|
},
|
||
|
{
|
||
|
entry: Entry{
|
||
|
Time: TTime,
|
||
|
Level: LError,
|
||
|
File: "/src/file.go",
|
||
|
Line: 142,
|
||
|
Message: []byte("some message"),
|
||
|
},
|
||
|
flags: log.Ldate | log.Llongfile,
|
||
|
output: `{"level":"error","time":"2015/8/1","file":"/src/file.go","line":142,"message":"some message"}` + "\n",
|
||
|
},
|
||
|
{
|
||
|
entry: Entry{
|
||
|
Time: TTime,
|
||
|
Level: LDebug,
|
||
|
Message: []byte("some message"),
|
||
|
},
|
||
|
lnum: true,
|
||
|
flags: log.Ldate,
|
||
|
output: `{"level":"1","time":"2015/8/1","message":"some message"}` + "\n",
|
||
|
},
|
||
|
}
|
||
|
|
||
|
func TestJSONFormatter(t *testing.T) {
|
||
|
for _, tt := range JSONFormatTests {
|
||
|
f := JSONFormatter{
|
||
|
Flag: tt.flags,
|
||
|
LevelAsNum: tt.lnum,
|
||
|
TimeFormat: tt.tfmt,
|
||
|
}
|
||
|
|
||
|
b, err := f.Format(&tt.entry)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
if string(b) != tt.output {
|
||
|
t.Errorf("Unexpected JSON formatter output: %s", b)
|
||
|
}
|
||
|
}
|
||
|
}
|