Skip to content

Commit

Permalink
[FAB-8065] Properly override logger backend in test
Browse files Browse the repository at this point in the history
The test TestHaltChainProcessing attempts to restore the
log backend by overriding the logger, but the function that
creates the logger, caches previous logger instances by the package name.

Even though we can override the logger instance with a new package name,
it is probably more correct to overwrite the backend itself.

Change-Id: Iba4f6cb6586f41cd3b7a019896a3dac0d8c58d95
Signed-off-by: yacovm <[email protected]>
  • Loading branch information
yacovm committed Feb 5, 2018
1 parent 15bda58 commit a0dd252
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions gossip/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"errors"
"fmt"
"math/rand"
"os"
"strconv"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -606,9 +607,9 @@ func TestHaltChainProcessing(t *testing.T) {
logEntries: make(chan string, 100),
}
logger.SetBackend(logAsserter)
// Restore old logger at the end of the test
// Restore old backend at the end of the test
defer func() {
logger = gutil.GetLogger(gutil.LoggingStateModule, "")
logger.SetBackend(defaultBackend())
}()

mc := &mockCommitter{}
Expand Down Expand Up @@ -1705,3 +1706,12 @@ func (*logBackend) SetLevel(logging.Level, string) {
func (*logBackend) IsEnabledFor(logging.Level, string) bool {
return true
}

func defaultBackend() logging.LeveledBackend {
backend := logging.NewLogBackend(os.Stderr, "", 0)
defaultFormat := "%{color}%{time:2006-01-02 15:04:05.000 MST} [%{module}] %{shortfunc} -> %{level:.4s} %{id:03x}%{color:reset} %{message}"
backendFormatter := logging.NewBackendFormatter(backend, logging.MustStringFormatter(defaultFormat))
be := logging.SetBackend(backendFormatter)
be.SetLevel(logging.WARNING, "")
return be
}

0 comments on commit a0dd252

Please sign in to comment.