Skip to content

Commit

Permalink
log: ensure that the test log scopes works with secondary loggers
Browse files Browse the repository at this point in the history
This patch ensures that the file redirection performed by log.Scope /
log.ScopeWithoutShowLogs is effective with secondary loggers.

Release note: None
  • Loading branch information
knz committed Mar 2, 2020
1 parent 589cfa1 commit e2da8bd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/util/log/secondary_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func NewSecondaryLogger(
if thatLogger != l {
continue
}
secondaryLogRegistry.mu.loggers = append(secondaryLogRegistry.mu.loggers[:i], secondaryLogRegistry.mu.loggers[i+1:])
secondaryLogRegistry.mu.loggers = append(secondaryLogRegistry.mu.loggers[:i], secondaryLogRegistry.mu.loggers[i+1:]...)
return
}
}()
Expand Down
33 changes: 24 additions & 9 deletions pkg/util/log/test_log_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,39 @@ func calledDuringPanic() bool {
// dirTestOverride sets the default value for the logging output directory
// for use in tests.
func dirTestOverride(expected, newDir string) error {
mainLog.mu.Lock()
defer mainLog.mu.Unlock()
if err := mainLog.dirTestOverride(expected, newDir); err != nil {
return err
}
// Same with secondary loggers.
secondaryLogRegistry.mu.Lock()
defer secondaryLogRegistry.mu.Unlock()
for _, l := range secondaryLogRegistry.mu.loggers {
if err := l.logger.dirTestOverride(expected, newDir); err != nil {
return err
}
}
return nil
}

func (l *loggerT) dirTestOverride(expected, newDir string) error {
l.mu.Lock()
defer l.mu.Unlock()

mainLog.logDir.Lock()
l.logDir.Lock()
// The following check is intended to catch concurrent uses of
// Scope() or TestLogScope.Close(), which would be invalid.
if mainLog.logDir.name != expected {
mainLog.logDir.Unlock()
if l.logDir.name != expected {
l.logDir.Unlock()
return errors.Errorf("unexpected logDir setting: set to %q, expected %q",
mainLog.logDir.name, expected)
l.logDir.name, expected)
}
mainLog.logDir.name = newDir
mainLog.logDir.Unlock()
l.logDir.name = newDir
l.logDir.Unlock()

// When we change the directory we close the current logging
// output, so that a rotation to the new directory is forced on
// the next logging event.
return mainLog.closeFileLocked()
return l.closeFileLocked()
}

func (l *loggerT) closeFileLocked() error {
Expand Down

0 comments on commit e2da8bd

Please sign in to comment.