go_goutils/logging/muiltilogger_linux_test.go

122 lines
3.5 KiB
Go
Raw Normal View History

2022-01-16 06:55:29 -05:00
package logging
import (
`os`
`testing`
)
// TestMultiLogger tests GetMultiLogger and MultiLogger methods.
func TestMultiLogger(t *testing.T) {
var l *MultiLogger
var tempfile *os.File
var tempfilePath string
var keepLog bool
var ltype string = "MultiLogger"
var prefix string
var testPrefix string
var err error
if tempfile, err = os.CreateTemp("", ".LOGGINGTEST_*"); err != nil {
t.Fatalf("error when creating temporary log file '%v': %v", tempfile.Name(), err.Error())
}
tempfilePath = tempfile.Name()
// We can close the handler immediately; we don't need it since the FileLogger opens its own.
if err = tempfile.Close(); err != nil {
t.Fatalf("error when closing handler for temporary log file '%v': %v", tempfile.Name(), err.Error())
}
l = GetMultiLogger(true, TestLogPrefix)
if err = l.AddStdLogger("StdLogger", false, true, logFlags); err != nil {
t.Fatalf("error when adding StdLogger to MultiLogger: %v", err.Error())
}
if err = l.AddFileLogger("FileLogger", logFlags, tempfilePath); err != nil {
t.Fatalf("error when adding FileLogger to MultiLogger: %v", err.Error())
}
if err = l.AddDefaultLogger("DefaultLogger", logFlags, tempfilePath); err != nil {
t.Fatalf("error when adding default logger to MultiLogger: %v", err.Error())
}
if err = l.AddSysdLogger("SystemDLogger"); err != nil {
t.Fatalf("error when adding SystemDLogger to MultiLogger: %v", err.Error())
}
if err = l.AddSyslogLogger("SyslogLogger"); err != nil {
t.Fatalf("error when adding SyslogLogger to MultiLogger: %v", err.Error())
}
t.Logf("Logger %v passed Setup. Logger: %#v", ltype, l)
if err = l.Alert(testAlert, ltype); err != nil {
t.Fatalf("error for Alert: %v", err.Error())
}
if err = l.Crit(testCrit, ltype); err != nil {
t.Fatalf("error for Crit: %v", err.Error())
}
if err = l.Debug(testDebug, ltype); err != nil {
t.Fatalf("error for Debug: %v", err.Error())
}
if err = l.Emerg(testEmerg, ltype); err != nil {
t.Fatalf("error for Emerg: %v", err.Error())
}
if err = l.Err(testErr, ltype); err != nil {
t.Fatalf("error for Err: %v", err.Error())
}
if err = l.Info(testInfo, ltype); err != nil {
t.Fatalf("error for Alert: %v", err.Error())
}
if err = l.Notice(testNotice, ltype); err != nil {
t.Fatalf("error for Notice: %v", err.Error())
}
if err = l.Warning(testWarning, ltype); err != nil {
t.Fatalf("error for Warning: %v", err.Error())
}
if prefix, err = l.GetPrefix(); err != nil {
t.Fatalf("error when fetching prefix: %v", err.Error())
}
if ltype == "StdLogger" || ltype == "FileLogger" { // StdLogger (and thus FileLogger) adds a space at the end.
testPrefix = TestLogPrefix + " "
} else {
testPrefix = TestLogPrefix
}
if prefix != testPrefix {
t.Fatalf("true prefix ('%v') does not match TestLogPrefix ('%v')", prefix, TestLogPrefix)
}
if err = l.SetPrefix(TestLogAltPrefix); err != nil {
t.Fatalf("error when setting prefix to %v: %v", TestLogAltPrefix, err.Error())
} else {
_ = l.SetPrefix(TestLogPrefix)
}
if err = l.DoDebug(false); err != nil {
t.Fatalf("error when changing debug to false: %v", err.Error())
} else {
_ = l.DoDebug(true)
}
if err = l.Shutdown(); err != nil {
t.Fatalf("Error when running Shutdown: %v", err.Error())
}
_, keepLog = os.LookupEnv(EnvVarKeepLog)
if !keepLog {
if err = os.Remove(tempfilePath); err != nil {
t.Fatalf("error when removing temporary log file '%v': %v", tempfilePath, err.Error())
}
}
t.Logf("Logger %v passed all logging targets.", ltype)
}