go_goutils/logging/funcs_test.go

197 lines
5.0 KiB
Go
Raw Normal View History

package logging
2022-01-16 06:55:29 -05:00
import (
`os`
`testing`
)
/*
TestStdLogger tests functionality for StdLogger.
*/
func TestStdLogger(t *testing.T) {
var l *StdLogger
var ltype string = "StdLogger"
var prefix string
var err error
l = &StdLogger{
EnableDebug: true,
Prefix: TestLogPrefix,
LogFlags: logFlags,
EnableStdOut: false,
EnableStdErr: true,
}
if err = l.Setup(); err != nil {
t.Fatalf("error when running Setup: %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 prefix != (TestLogPrefix + " ") { // StdLogger adds a space at the end.
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 if l.EnableDebug {
t.Fatalf("did not properly set Debug filter state")
} else {
_ = l.DoDebug(true)
}
if err = l.Shutdown(); err != nil {
t.Fatalf("Error when running Shutdown: %v", err.Error())
}
t.Logf("Logger %v passed all logging targets.", ltype)
}
/*
TestFileLogger tests functionality for FileLogger.
If the appropriate env var is set (see the EnvVarKeepLog constant), the temporary log file that is created will not be cleaned up.
*/
func TestFileLogger(t *testing.T) {
var l *FileLogger
var ltype string = "FileLogger"
var prefix string
var tempfile *os.File
var tempfilePath string
var keepLog bool
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 = &FileLogger{
StdLogger: StdLogger{
EnableDebug: true,
Prefix: TestLogPrefix,
LogFlags: logFlags,
},
Path: tempfilePath,
}
if err = l.Setup(); err != nil {
t.Fatalf("error when running Setup: %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 prefix != (TestLogPrefix + " ") { // StdLogger (and thus FileLogger) adds a space at the end.
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 if l.EnableDebug {
t.Fatalf("did not properly set Debug filter state")
} 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)
}