package klog import ( "io" "os" "testing" ) func TestDev(t *testing.T) { LEVEL = Lerror | Ldebug | Lwarn | Linfo PROD = false // log.system = "dev" 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") } func TestProd(t *testing.T) { PROD = true // log.system = "prod" 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") } func TestSubDev(t *testing.T) { PROD = false l := Sub("TestSubDev") 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") } func TestSubProd(t *testing.T) { PROD = true l := Sub("TestSubProd") 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") } func TestColoring(t *testing.T) { l := Sub("color") 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)) } 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) l.Error("TestToFile") l.Debug("TestToFile") l.Warn("TestToFile") l.Info("TestToFile") l.SetColor(true) l.Error("TestToFile colored") l.Debug("TestToFile colored") l.Warn("TestToFile colored") l.Info("TestToFile colored") } 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) l1.Info("child1-discard") l2.Info("child2-discard") l3.Info("child3-discard") SetOutputAll(os.Stdout) l1.Info("child1-stdout") l2.Info("child2-stdout") l3.Info("child3-stdout") SetColorAll(false) l1.Info("child1-nocolor") l2.Info("child2-nocolor") l3.Info("child3-nocolor") } func TestDefaultLogger(t *testing.T) { l := DefaultLogger() l.Error("TestDefaultLogger") l.Debug("TestDefaultLogger") l.Warn("TestDefaultLogger") l.Info("TestDefaultLogger") } 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) } } 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") }