cosmos-sdk: Wrong command output
Summary of Bug
Most of the commands print their outputs to stderr instead of stdout, making it harder to pipe with other utilities (such as jq).
Version
v0.40.1
Steps to Reproduce
- Run almost any command (eg.
simd status) - Try piping that command with
jq
simd status | jq '.'
This will return the default output of the command, instead of the output of jq. A workaround is to redirect stderr to stdout for while running the command:
simd status 2>&1 | jq '.'
Possible solution
Taking a look at the commands code I noticed that, although many commands use the cmd.Print methods, only few correctly set their output using cmd.SetOut. This causes the cmd.Print to print the output using stderr, as per documentation:
// Println is a convenience method to Println to the defined output, fallback to Stderr if not set.
I think that a possible solution could be to add the following lines to the app.go’s initRootCmd function:
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
cmd.SetOut(cmd.OutOrStdout())
}
This would make sure that by default all commands are set to have their output to stdout, with the ability to override it later.
For Admin Use
- Not duplicate issue
- Appropriate labels applied
- Appropriate contributors tagged
- Contributor assigned/self-assigned
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 5
- Comments: 25 (22 by maintainers)
Looks like we need to do some grooming here. Ensure all query-based commands output to STDOUT.
Proposing to close this, since it seems it works in v0.45 and master.
If there’s a strong will to backport this to v0.44, let us know here.
Just chiming in regarding logging to file. Quick workaround for systemd users, add the below to the .service file:
(and yes, everything is currently sent to stderr per this issue)