Skip to content

Commit

Permalink
Add NATS_LOGGING test helper (#5201)
Browse files Browse the repository at this point in the history
Setting this environment variable forces NATS Server instances running
in the unit tests to log to stdout. Possible values are:

* `NATS_LOGGING=true go test ...` (or any other non-empty value)
* `NATS_LOGGING=debug go test ...`
* `NATS_LOGGING=trace go test ...`

Signed-off-by: Neil Twigg <neil@nats.io>
  • Loading branch information
derekcollison committed Apr 25, 2024
2 parents 9a50c49 + 6261693 commit 8478886
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"testing"
"time"

srvlog "github.com/nats-io/nats-server/v2/logger"
"github.com/nats-io/nats.go"
)

Expand Down Expand Up @@ -86,6 +87,13 @@ func RunServer(opts *Options) *Server {
s.ConfigureLogger()
}

if ll := os.Getenv("NATS_LOGGING"); ll != "" {
log := srvlog.NewTestLogger(fmt.Sprintf("[%s] | ", s), true)
debug := ll == "debug" || ll == "trace"
trace := ll == "trace"
s.SetLoggerV2(log, debug, trace, false)
}

// Run server in Go routine.
s.Start()

Expand Down
8 changes: 8 additions & 0 deletions test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"testing"
"time"

srvlog "github.com/nats-io/nats-server/v2/logger"
"github.com/nats-io/nats-server/v2/server"
)

Expand Down Expand Up @@ -94,6 +95,13 @@ func RunServerCallback(opts *server.Options, callback func(*server.Server)) *ser
s.ConfigureLogger()
}

if ll := os.Getenv("NATS_LOGGING"); ll != "" {
log := srvlog.NewTestLogger(fmt.Sprintf("[%s] | ", s), true)
debug := ll == "debug" || ll == "trace"
trace := ll == "trace"
s.SetLoggerV2(log, debug, trace, false)
}

if callback != nil {
callback(s)
}
Expand Down

0 comments on commit 8478886

Please sign in to comment.