Skip to content

Commit

Permalink
Fix flaky PION_LOG_*, more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joeturki committed Jan 9, 2025
1 parent 8466bde commit 47e0b89
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 2 deletions.
6 changes: 4 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,16 @@ func NewDefaultLoggerFactory() *DefaultLoggerFactory {
}

if strings.ToLower(env) == "all" {
factory.DefaultLogLevel = level
if factory.DefaultLogLevel < level {
factory.DefaultLogLevel = level
}

continue
}

scopes := strings.Split(strings.ToLower(env), ",")
for _, scope := range scopes {
factory.ScopeLevels[scope] = level
factory.ScopeLevels[strings.TrimSpace(scope)] = level
}
}

Expand Down
169 changes: 169 additions & 0 deletions logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,50 @@ func testErrorLevel(t *testing.T, logger *logging.DefaultLeveledLogger) {
}
}

func testTraceLevel(t *testing.T, logger *logging.DefaultLeveledLogger) {
t.Helper()

var outBuf bytes.Buffer
logger.WithOutput(&outBuf)

traceMsg := "trace message"
logger.Trace(traceMsg)
if !strings.Contains(outBuf.String(), traceMsg) {
t.Errorf("Expected to find %q in %q, but didn't", traceMsg, outBuf.String())
}
logger.Tracef(traceMsg) // nolint: govet
if !strings.Contains(outBuf.String(), traceMsg) {
t.Errorf("Expected to find %q in %q, but didn't", traceMsg, outBuf.String())
}
}

func testInfoLevel(t *testing.T, logger *logging.DefaultLeveledLogger) {
t.Helper()

var outBuf bytes.Buffer
logger.WithOutput(&outBuf)

infoMsg := "info message"
logger.Info(infoMsg)
if !strings.Contains(outBuf.String(), infoMsg) {
t.Errorf("Expected to find %q in %q, but didn't", infoMsg, outBuf.String())
}
logger.Infof(infoMsg) // nolint: govet
if !strings.Contains(outBuf.String(), infoMsg) {
t.Errorf("Expected to find %q in %q, but didn't", infoMsg, outBuf.String())
}
}

func testAllLevels(t *testing.T, logger *logging.DefaultLeveledLogger) {
t.Helper()

testDebugLevel(t, logger)
testWarnLevel(t, logger)
testErrorLevel(t, logger)
testTraceLevel(t, logger)
testInfoLevel(t, logger)
}

func TestDefaultLoggerFactory(t *testing.T) {
factory := logging.DefaultLoggerFactory{
Writer: os.Stderr,
Expand Down Expand Up @@ -115,6 +159,96 @@ func TestDefaultLogger(t *testing.T) {
testErrorLevel(t, logger)
}

func TestNewDefaultLoggerFactory(t *testing.T) {
factory := logging.NewDefaultLoggerFactory()

disabled := factory.NewLogger("DISABLE")
errorLevel := factory.NewLogger("ERROR")
warnLevel := factory.NewLogger("WARN")
infoLevel := factory.NewLogger("INFO")
debugLevel := factory.NewLogger("DEBUG")
traceLevel := factory.NewLogger("TRACE")

disabledLogger, ok := disabled.(*logging.DefaultLeveledLogger)
if !ok {
t.Error("Missing disabled logger")
}

errorLogger, ok := errorLevel.(*logging.DefaultLeveledLogger)
if !ok {
t.Error("Missing error logger")
}

warnLogger, ok := warnLevel.(*logging.DefaultLeveledLogger)
if !ok {
t.Error("Missing warn logger")
}

infoLogger, ok := infoLevel.(*logging.DefaultLeveledLogger)
if !ok {
t.Error("Missing info logger")
}

debugLogger, ok := debugLevel.(*logging.DefaultLeveledLogger)
if !ok {
t.Error("Missing debug logger")
}

traceLogger, ok := traceLevel.(*logging.DefaultLeveledLogger)
if !ok {
t.Error("Missing trace logger")
}

testNoDebugLevel(t, disabledLogger)
testNoDebugLevel(t, errorLogger)
testNoDebugLevel(t, warnLogger)
testNoDebugLevel(t, infoLogger)
testNoDebugLevel(t, debugLogger)
testNoDebugLevel(t, traceLogger)
}

func TestNewDefaultLoggerFactoryLogAll(t *testing.T) {
t.Setenv("PION_LOG_ERROR", "all")
t.Setenv("PION_LOG_WARN", "all")
t.Setenv("PION_LOG_INFO", "all")
t.Setenv("PION_LOG_DEBUG", "all")
t.Setenv("PION_LOG_TRACE", "all")

factory := logging.NewDefaultLoggerFactory()

testAPI, ok := factory.NewLogger("test").(*logging.DefaultLeveledLogger)
if !ok {
t.Error("Invalid logger factory type")
}

testAllLevels(t, testAPI)
}

func TestNewDefaultLoggerFactorySpecifcScopes(t *testing.T) {
t.Setenv("PION_LOG_DEBUG", "feature, rtp-logger")

factory := logging.NewDefaultLoggerFactory()

feature, ok := factory.NewLogger("feature").(*logging.DefaultLeveledLogger)
if !ok {
t.Error("Invalid logger factory type")
}

rtp, ok := factory.NewLogger("rtp-logger").(*logging.DefaultLeveledLogger)
if !ok {
t.Error("Invalid logger factory type")
}

noScope, ok := factory.NewLogger("no-scope").(*logging.DefaultLeveledLogger)
if !ok {
t.Error("Invalid logger factory type")
}

testDebugLevel(t, feature)
testDebugLevel(t, rtp)
testNoDebugLevel(t, noScope)
}

func TestSetLevel(t *testing.T) {
logger := logging.
NewDefaultLeveledLoggerForScope("testSetLevel", logging.LogLevelWarn, os.Stderr)
Expand All @@ -123,3 +257,38 @@ func TestSetLevel(t *testing.T) {
logger.SetLevel(logging.LogLevelDebug)
testDebugLevel(t, logger)
}

func TestLogLevel(t *testing.T) {
logLevel := logging.LogLevelDisabled

logLevel.Set(logging.LogLevelError)
if logLevel.Get() != logging.LogLevelError {
t.Error("LogLevel was not set to LogLevelError")
}
}

func TestLogLevelString(t *testing.T) {
expected := map[logging.LogLevel]string{
logging.LogLevelDisabled: "Disabled",
logging.LogLevelError: "Error",
logging.LogLevelWarn: "Warn",
logging.LogLevelInfo: "Info",
logging.LogLevelDebug: "Debug",
logging.LogLevelTrace: "Trace",
logging.LogLevel(999): "UNKNOWN",
}

for level, expectedStr := range expected {
if level.String() != expectedStr {
t.Errorf("Expected %q, got %q", expectedStr, level.String())
}
}
}

func TestNewDefaultLoggerStderr(t *testing.T) {
logger := logging.NewDefaultLeveledLoggerForScope("test", logging.LogLevelWarn, nil)

testNoDebugLevel(t, logger)
testWarnLevel(t, logger)
testErrorLevel(t, logger)
}

0 comments on commit 47e0b89

Please sign in to comment.