Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(log): associate test logger with testing.T instance #15261

Merged
merged 5 commits into from
Mar 3, 2023

Conversation

mark-rushakoff
Copy link
Member

Description

This PR has two commits: the first refactors NewTestLogger to accept a testing.T, and the next replaces widespread test usage of log.NewLogger() with log.NewTestLogger(t).

The primary advantage of a logger instance associated with a single test, is that logging is captured by default, but only shown per test's failure.

You will still see all the test logs when running with go test -v, but you won't need to re-run a failing test with -v in order to see associated logs.

TODO: after this PR is merged, remove replace statements in a few go.mod files.


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

Rename from NewTestingLogger to NewTestLogger, as many tests will
require this -- the latter name is shorter and a more common name.

Instead of a globally shared logger, use zerolog's TestWriter to
associate loggers with individual tests.

This breaks the log module's direct dependency on the testing package,
as we no longer inspect the testing package's Verbose function.

When running go test -v, you will still see all logging messages, but
they will be associated with the test using that logger instance. When
running just go test without the -v flag, you will only see logging
messages on failed tests (the default behavior of calls to t.Log).

Also server/mock.SetupApp into the test package, so that the non-test
mock package does not have a direct dependency on testing.
There were several tests using log.NewLogger, which would direct output
to stdout. While this somewhat works in tests, the test logger is a
better match for the intent -- it uses (*testing.T).Logf so that the
output is hidden by default, shown on test failure, or always shown when
opting in through go test -v.

There were more tests in cosmosvisor that called NewLogger, but I didn't
touch those because of their apparent complexity in setting up buffered
pipes and so on.
@mark-rushakoff mark-rushakoff requested a review from a team as a code owner March 3, 2023 18:45
@github-prbot github-prbot requested review from a team and tac0turtle and removed request for a team March 3, 2023 18:45
@github-actions github-actions bot added C:Cosmovisor Issues and PR related to Cosmovisor C:log C:x/upgrade labels Mar 3, 2023
Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm!

@@ -154,6 +154,9 @@ require (
nhooyr.io/websocket v1.8.6 // indirect
)

// Short-lived replace for an outstanding PR of log.
Copy link
Member

@julienrbrt julienrbrt Mar 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What we do now (for tools actively in dev) is to use the commit of the PR to create a pseudo version, so it limits the number of PRs.
We have a script to update it everywhere easily ./scripts/go-update-dep-all.sh cosmossdk.io/log@commit

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I just pushed 0ed8a41 which references commit 74c894f from this PR.

Copy link
Member

@julienrbrt julienrbrt Mar 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one got forgotten I think.

x/upgrade/go.mod Outdated
@@ -175,6 +175,9 @@ require (
sigs.k8s.io/yaml v1.3.0 // indirect
)

// Short-lived replacement for an outstanding log PR.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

@mark-rushakoff mark-rushakoff enabled auto-merge (squash) March 3, 2023 19:45
@mark-rushakoff mark-rushakoff merged commit fab029b into main Mar 3, 2023
@mark-rushakoff mark-rushakoff deleted the mr/test-logging branch March 3, 2023 20:26
@julienrbrt julienrbrt mentioned this pull request Mar 6, 2023
19 tasks
larry0x pushed a commit to larry0x/cosmos-sdk that referenced this pull request May 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:Cosmovisor Issues and PR related to Cosmovisor C:log C:x/upgrade
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants