2021-11-03 19:02:27 +00:00
|
|
|
package klog
|
2021-11-01 14:38:30 +00:00
|
|
|
|
2021-11-01 16:50:15 +00:00
|
|
|
import (
|
2021-11-02 03:08:23 +00:00
|
|
|
"io"
|
2021-11-01 16:50:15 +00:00
|
|
|
"os"
|
2021-11-07 04:41:46 +00:00
|
|
|
"runtime"
|
2021-11-01 16:50:15 +00:00
|
|
|
"testing"
|
|
|
|
)
|
2021-11-01 14:38:30 +00:00
|
|
|
|
2021-11-01 14:41:11 +00:00
|
|
|
func TestDev(t *testing.T) {
|
2021-11-02 03:08:23 +00:00
|
|
|
LEVEL = Lerror | Ldebug | Lwarn | Linfo
|
2021-11-01 14:38:30 +00:00
|
|
|
PROD = false
|
2021-11-01 14:41:11 +00:00
|
|
|
// log.system = "dev"
|
2021-11-03 05:51:59 +00:00
|
|
|
Error("TestDev")
|
|
|
|
ErrorF(H{"Test": "set"}, "TestDev")
|
|
|
|
Debug("TestDev")
|
|
|
|
DebugF(H{"Test": "set"}, "TestDev")
|
|
|
|
Warn("TestDev")
|
|
|
|
WarnF(H{"Test": "set"}, "TestDev")
|
|
|
|
Info("TestDev")
|
|
|
|
InfoF(H{"Test": "set"}, "TestDev")
|
2021-11-01 14:41:11 +00:00
|
|
|
}
|
2021-11-01 14:38:30 +00:00
|
|
|
|
2021-11-01 14:41:11 +00:00
|
|
|
func TestProd(t *testing.T) {
|
|
|
|
PROD = true
|
|
|
|
// log.system = "prod"
|
2021-11-03 05:51:59 +00:00
|
|
|
Error("TestProd")
|
|
|
|
ErrorF(H{"Test": "set"}, "TestProd")
|
|
|
|
Debug("TestProd")
|
|
|
|
DebugF(H{"Test": "set"}, "TestProd")
|
|
|
|
Warn("TestProd")
|
|
|
|
WarnF(H{"Test": "set"}, "TestProd")
|
|
|
|
Info("TestProd")
|
|
|
|
InfoF(H{"Test": "set"}, "TestProd")
|
2021-11-01 14:38:30 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 14:41:11 +00:00
|
|
|
func TestSubDev(t *testing.T) {
|
|
|
|
PROD = false
|
|
|
|
l := Sub("TestSubDev")
|
2021-11-03 05:51:59 +00:00
|
|
|
l.Error("TestSubDev")
|
|
|
|
l.ErrorF(H{"Test": "set"}, "TestSubDev")
|
|
|
|
l.Debug("TestSubDev")
|
|
|
|
l.DebugF(H{"Test": "set"}, "TestSubDev")
|
|
|
|
l.Warn("TestSubDev")
|
|
|
|
l.WarnF(H{"Test": "set"}, "TestSubDev")
|
|
|
|
l.Info("TestSubDev")
|
|
|
|
l.InfoF(H{"Test": "set"}, "TestSubDev")
|
2021-11-01 14:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubProd(t *testing.T) {
|
2021-11-01 14:38:30 +00:00
|
|
|
PROD = true
|
2021-11-01 14:41:11 +00:00
|
|
|
l := Sub("TestSubProd")
|
2021-11-03 05:51:59 +00:00
|
|
|
l.Error("TestSubProd")
|
|
|
|
l.ErrorF(H{"Test": "set"}, "TestSubProd")
|
|
|
|
l.Debug("TestSubProd")
|
|
|
|
l.DebugF(H{"Test": "set"}, "TestSubProd")
|
|
|
|
l.Warn("TestSubProd")
|
|
|
|
l.WarnF(H{"Test": "set"}, "TestSubProd")
|
|
|
|
l.Info("TestSubProd")
|
|
|
|
l.InfoF(H{"Test": "set"}, "TestSubProd")
|
2021-11-01 14:41:11 +00:00
|
|
|
}
|
2021-11-01 14:38:30 +00:00
|
|
|
|
2021-11-02 07:55:52 +00:00
|
|
|
func TestColoring(t *testing.T) {
|
|
|
|
l := Sub("color")
|
2021-11-05 15:29:34 +00:00
|
|
|
l.Error(l.M("Hello", BgHiGreen, Italic, FgWhite), ", ", l.M("world", FgBlue, BgHiYellow))
|
|
|
|
l.Error(l.M("Hello", FgHiGreen), ", ", l.M("world", Underline, FgMagenta))
|
|
|
|
Error(M("Hello", FgHiGreen), ", ", M("world", Underline, FgMagenta))
|
2021-11-02 07:55:52 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 16:50:15 +00:00
|
|
|
func TestToFile(t *testing.T) {
|
|
|
|
PROD = true
|
|
|
|
l := Sub("TestToFile")
|
|
|
|
|
|
|
|
f, err := os.OpenFile("dist/test.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
l.SetOutput(f)
|
|
|
|
l.SetErrOutput(f)
|
|
|
|
|
2021-11-03 05:51:59 +00:00
|
|
|
l.Error("TestToFile")
|
|
|
|
l.Debug("TestToFile")
|
|
|
|
l.Warn("TestToFile")
|
|
|
|
l.Info("TestToFile")
|
2021-11-02 07:26:10 +00:00
|
|
|
|
|
|
|
l.SetColor(true)
|
2021-11-03 05:51:59 +00:00
|
|
|
l.Error("TestToFile colored")
|
|
|
|
l.Debug("TestToFile colored")
|
|
|
|
l.Warn("TestToFile colored")
|
|
|
|
l.Info("TestToFile colored")
|
2021-11-01 14:38:30 +00:00
|
|
|
}
|
2021-11-02 03:08:23 +00:00
|
|
|
|
|
|
|
func TestApplyChild(t *testing.T) {
|
|
|
|
PROD = true
|
|
|
|
l := Sub("parent")
|
|
|
|
l1 := l.Sub("child1")
|
|
|
|
l2 := l.Sub("child2")
|
|
|
|
l3 := l.Sub("child3")
|
|
|
|
l1.Info("child1")
|
|
|
|
l2.Info("child2")
|
|
|
|
l3.Info("child3")
|
|
|
|
l.SetOutputAll(io.Discard)
|
2021-11-05 07:41:54 +00:00
|
|
|
|
2021-11-02 03:08:23 +00:00
|
|
|
l1.Info("child1-discard")
|
|
|
|
l2.Info("child2-discard")
|
|
|
|
l3.Info("child3-discard")
|
|
|
|
SetOutputAll(os.Stdout)
|
2021-11-05 07:41:54 +00:00
|
|
|
|
2021-11-02 03:08:23 +00:00
|
|
|
l1.Info("child1-stdout")
|
|
|
|
l2.Info("child2-stdout")
|
|
|
|
l3.Info("child3-stdout")
|
2021-11-02 07:26:10 +00:00
|
|
|
SetColorAll(false)
|
2021-11-05 07:41:54 +00:00
|
|
|
|
2021-11-02 07:26:10 +00:00
|
|
|
l1.Info("child1-nocolor")
|
|
|
|
l2.Info("child2-nocolor")
|
|
|
|
l3.Info("child3-nocolor")
|
2021-11-02 03:08:23 +00:00
|
|
|
}
|
2021-11-03 15:05:38 +00:00
|
|
|
|
|
|
|
func TestDefaultLogger(t *testing.T) {
|
|
|
|
l := DefaultLogger()
|
|
|
|
l.Error("TestDefaultLogger")
|
|
|
|
l.Debug("TestDefaultLogger")
|
|
|
|
l.Warn("TestDefaultLogger")
|
|
|
|
l.Info("TestDefaultLogger")
|
|
|
|
}
|
2021-11-05 07:46:23 +00:00
|
|
|
|
|
|
|
func BenchmarkDefault(b *testing.B) {
|
|
|
|
SetErrOutput(io.Discard)
|
|
|
|
SetOutput(io.Discard)
|
|
|
|
LEVEL = 15
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
PROD = true
|
|
|
|
Error("err in", "BenchmarkDefault: ", i)
|
|
|
|
ErrorF(H{"test": "fields"}, "err in", "BenchmarkDefault: ", i)
|
|
|
|
Debug("err in", "BenchmarkDefault: ", i)
|
|
|
|
DebugF(H{"test": "fields"}, "err in", "BenchmarkDefault: ", i)
|
|
|
|
Warn("err in", "BenchmarkDefault: ", i)
|
|
|
|
WarnF(H{"test": "fields"}, "err in", "BenchmarkDefault: ", i)
|
|
|
|
}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
PROD = false
|
|
|
|
Error("err in", "BenchmarkDefault: ", i)
|
|
|
|
ErrorF(H{"test": "fields"}, "err in", "BenchmarkDefault: ", i)
|
|
|
|
Debug("err in", "BenchmarkDefault: ", i)
|
|
|
|
DebugF(H{"test": "fields"}, "err in", "BenchmarkDefault: ", i)
|
|
|
|
Warn("err in", "BenchmarkDefault: ", i)
|
|
|
|
WarnF(H{"test": "fields"}, "err in", "BenchmarkDefault: ", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkSub1(b *testing.B) {
|
|
|
|
LEVEL = 15
|
|
|
|
l := Sub("Sub1")
|
|
|
|
l.SetErrOutput(io.Discard)
|
|
|
|
l.SetOutput(io.Discard)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
PROD = true
|
|
|
|
l.Error("err in", "BenchmarkDefault: ", i)
|
|
|
|
l.ErrorF(H{"test": "fields"}, "err in", "BenchmarkSub1: ", i)
|
|
|
|
l.Debug("err in", "BenchmarkDefault: ", i)
|
|
|
|
l.DebugF(H{"test": "fields"}, "err in", "BenchmarkSub1: ", i)
|
|
|
|
l.Warn("err in", "BenchmarkDefault: ", i)
|
|
|
|
l.WarnF(H{"test": "fields"}, "err in", "BenchmarkSub1: ", i)
|
|
|
|
}
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
PROD = false
|
|
|
|
l.Error("err in", "BenchmarkDefault: ", i)
|
|
|
|
l.ErrorF(H{"test": "fields"}, "err in", "BenchmarkSub1: ", i)
|
|
|
|
l.Debug("err in", "BenchmarkDefault: ", i)
|
|
|
|
l.DebugF(H{"test": "fields"}, "err in", "BenchmarkSub1: ", i)
|
|
|
|
l.Warn("err in", "BenchmarkDefault: ", i)
|
|
|
|
l.WarnF(H{"test": "fields"}, "err in", "BenchmarkSub1: ", i)
|
|
|
|
}
|
|
|
|
}
|
2021-11-05 15:29:34 +00:00
|
|
|
|
|
|
|
func TestIntercept(t *testing.T) {
|
|
|
|
l := Sub("inter")
|
|
|
|
l.Info("original")
|
|
|
|
p := l.GetPrinter()
|
|
|
|
l.SetPrinter(func(w io.Writer, d *Ldata, l *Logger) {
|
|
|
|
d.Message += " intercepted"
|
|
|
|
p(w, d, l)
|
|
|
|
})
|
|
|
|
l.Info("original")
|
|
|
|
}
|
2021-11-07 04:41:46 +00:00
|
|
|
|
|
|
|
func BenchmarkStackSTD(b *testing.B) {
|
|
|
|
f := func() []byte {
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
for {
|
|
|
|
n := runtime.Stack(buf, false)
|
|
|
|
if n < len(buf) {
|
|
|
|
return buf[:n]
|
|
|
|
}
|
|
|
|
buf = make([]byte, 2*len(buf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LEVEL = 15
|
|
|
|
l := Sub("StackSTD")
|
|
|
|
l.SetErrOutput(io.Discard)
|
|
|
|
l.SetOutput(io.Discard)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
l.Info(f())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStackSTDStr(b *testing.B) {
|
|
|
|
LEVEL = 15
|
|
|
|
l := Sub("StackSTD")
|
|
|
|
l.SetErrOutput(io.Discard)
|
|
|
|
l.SetOutput(io.Discard)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
l.Info(stack())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStackCust(b *testing.B) {
|
|
|
|
LEVEL = 15
|
|
|
|
l := Sub("StackCust")
|
|
|
|
l.SetErrOutput(io.Discard)
|
|
|
|
l.SetOutput(io.Discard)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
l.Info(Stack(3))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkStackCustStr(b *testing.B) {
|
|
|
|
LEVEL = 15
|
|
|
|
l := Sub("StackCust")
|
|
|
|
l.SetErrOutput(io.Discard)
|
|
|
|
l.SetOutput(io.Discard)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
l.Info(string(Stack(3)))
|
|
|
|
}
|
|
|
|
}
|