klog/log_test.go

147 lines
3.4 KiB
Go

package log
import (
"io"
"os"
"testing"
c "kumoly.io/core/log/color"
)
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 TestCustTmpl(t *testing.T) {
PROD = true
l := Sub("TestCustTmpl")
CustFormater := NewLogFormater()
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"}}`
l.SetTmpl(CustFormater, nil)
l.Reload()
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()
}
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))
Error(M("Hello", c.FgHiGreen), ", ", M("world", c.Underline, c.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.Reload()
l.Error("TestToFile")
l.Debug("TestToFile")
l.Warn("TestToFile")
l.Info("TestToFile")
l.SetColor(true)
l.Reload()
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)
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")
SetColorAll(false)
ReloadAll()
l1.Info("child1-nocolor")
l2.Info("child2-nocolor")
l3.Info("child3-nocolor")
}