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): require destination in calls to NewLogger #15262

Merged
merged 9 commits into from
Mar 6, 2023

Conversation

mark-rushakoff
Copy link
Member

Description

The previous implementation of log.NewLogger hardcoded the output to os.Stdout. This change updates the signature from NewLogger() to NewLogger(dest io.Writer), so that callers have to be explicit whether they want logs to go to stdout, stderr, or somewhere else.

This is useful particularly in client/pruning.Cmd(), where a new logger was instantiated; that logger is no longer hardcoded to os.Stdout, but instead to a (*cobra.Command).OutOrStdout() for more flexibility in tests that may use that command.


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)

Prior to this change, NewLogger defaulted to sending logs to os.Stdout.
This change updates the method to accept an io.Writer indicating where
logs should go, so that consumers of the SDK could choose to send logs
to stderr, as that is the more typical IO stream for logs.

This is a temporary commit that changes the NewLogger signature but adds
a NewLoggerToStdout function. The next commit will remove
NewLoggerToStdout (preferring NewLogger(os.Stdout) and update the
referenced version of the log module.
Prior to this change, log.NewLogger() was hardcoded to write to stdout.
This is not always desirable: depending on where the logger is
instantiated, it may need to be associated with a (*cobra.Command)'s out
or err stream, or importers of the SDK may want to send logs to stderr
instead of stdout. Additionally, this removes any ambiguity about where
logs may go, when reading the code.

Finally, this allows an easy migration to defaulting logs to stderr,
should we choose to do that in the future.
@mark-rushakoff mark-rushakoff requested a review from a team as a code owner March 3, 2023 21:50
@github-actions github-actions bot added C:CLI C:Confix Issues and PR related to Confix C:Cosmovisor Issues and PR related to Cosmovisor C:Hubl Tool: Hubl C:log C:Rosetta Issues and PR related to Rosetta C:Store C:x/circuit C:x/evidence C:x/feegrant C:x/nft C:x/upgrade labels Mar 3, 2023
@julienrbrt
Copy link
Member

julienrbrt commented Mar 3, 2023

Does not make it too verbose?
The majority of time you want it to go to stdout anyway.
Cannot we have a new function that takes it as an argument?
Like NewLoggerWithOutput or something alike?
We have as well NewCustomLogger for this use case too right?

@mark-rushakoff
Copy link
Member Author

Does not make it too verbose?

A new logger should be instantiated once in a main function and propagated down.

In tests, where we do instantiate many loggers, we have NewTestLogger(t *testing.T).

The majority of time you want it to go to stdout anyway.

No, logs go to stderr. stdout is for information that one would save to a file or pipe to another process. The choice to default logs to stdout is unusual.

We have as well NewCustomLogger for this use case too right?

The custom logger should be an exceptional case.

@mark-rushakoff
Copy link
Member Author

Looks like I've missed a dependency update that I'll have to return to later. Putting this in draft mode until then.

@mark-rushakoff mark-rushakoff marked this pull request as draft March 3, 2023 22:03
@julienrbrt
Copy link
Member

No, logs go to stderr. stdout is for information that one would save to a file or pipe to another process. The choice to default logs to stdout is unusual.

TIL, I am always used to pipe logs normally 🤔

In tests, where we do instantiate many loggers, we have NewTestLogger(t *testing.T).
...
The custom logger should be an exceptional case.

Yeah, my bad. this makes sense! Just noticed CMT logger as well let you choose where it writes too

@julienrbrt julienrbrt mentioned this pull request Mar 6, 2023
19 tasks
There were CI failures due to depending on a mismatched pairing of the
log module and the main SDK module.
@github-actions github-actions bot added the C:orm label Mar 6, 2023
I missed this earlier due to the linux build tag.
@mark-rushakoff mark-rushakoff marked this pull request as ready for review March 6, 2023 16:09
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!

@mark-rushakoff mark-rushakoff added the A:automerge Automatically merge PR once all prerequisites pass. label Mar 6, 2023
@mark-rushakoff mark-rushakoff merged commit f151bf6 into main Mar 6, 2023
@mark-rushakoff mark-rushakoff deleted the mr/new-logger-dst branch March 6, 2023 19:05
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
A:automerge Automatically merge PR once all prerequisites pass. C:CLI C:Confix Issues and PR related to Confix C:Cosmovisor Issues and PR related to Cosmovisor C:Hubl Tool: Hubl C:log C:orm C:Rosetta Issues and PR related to Rosetta C:Store C:x/circuit C:x/evidence C:x/feegrant C:x/nft C:x/upgrade
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants