klog/log_test.go

155 lines
3.6 KiB
Go
Raw Normal View History

2021-11-01 14:38:30 +00:00
package log
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"
"testing"
2021-11-02 07:55:52 +00:00
2021-11-03 18:56:46 +00:00
c "kumoly.io/lib/klog/color"
2021-11-01 16:50:15 +00:00
)
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-01 14:41:11 +00:00
func TestCustTmpl(t *testing.T) {
PROD = true
l := Sub("TestCustTmpl")
CustFormater := NewLogFormater()
2021-11-03 05:51:59 +00:00
CustFormater.InfoTmplStr = `{{Time}} [{{ M "INFO" 104 93 9}} ]` +
`{{if .System}}({{M .System 96}}){{end}} ` +
`Hello, {{.Fields.Name}}. {{.Message}}{{"\n"}}`
CustFormater.ErrTmplStr = `{{Time}} [{{ M "ERROR" 91 43 1}}]` +
`{{if .System}}({{M .System 96}}){{end}} ` +
`NO! {{.Fields.Name}} {{M .Message 31 3}} {{"\n"}}`
2021-11-01 16:50:15 +00:00
l.SetTmpl(CustFormater, nil)
l.Reload()
2021-11-03 05:51:59 +00:00
l.ErrorF(H{"Name": "Brandon"}, "TestCustTmpl")
l.InfoF(H{"Name": "Brandon"}, "TestCustTmpl")
SetTmpl(CustFormater, nil)
Reload()
ErrorF(H{"Name": "Brandon"}, "TestCustTmpl std")
InfoF(H{"Name": "Brandon"}, "TestCustTmpl std")
//reset
SetTmpl(NewLogFormater(), nil)
Reload()
2021-11-01 16:50:15 +00:00
}
2021-11-02 07:55:52 +00:00
func TestColoring(t *testing.T) {
l := Sub("color")
l.Error(l.M("Hello", c.BgHiGreen, c.Italic, c.FgWhite), ", ", l.M("world", c.FgBlue, c.BgHiYellow))
l.Error(l.M("Hello", c.FgHiGreen), ", ", l.M("world", c.Underline, c.FgMagenta))
2021-11-03 05:51:59 +00:00
Error(M("Hello", c.FgHiGreen), ", ", M("world", c.Underline, c.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)
l.Reload()
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)
l.Reload()
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)
l.ReloadAll()
l1.Info("child1-discard")
l2.Info("child2-discard")
l3.Info("child3-discard")
SetOutputAll(os.Stdout)
ReloadAll()
l1.Info("child1-stdout")
l2.Info("child2-stdout")
l3.Info("child3-stdout")
2021-11-02 07:26:10 +00:00
SetColorAll(false)
ReloadAll()
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")
}