cosmos / cosmos-sdk

:chains: A Framework for Building High Value Public Blockchains :sparkles:
https://cosmos.network/
Apache License 2.0
6.19k stars 3.58k forks source link

Cosmovisor: be able to silence cosmovisor logs when running commands #15359

Closed freak12techno closed 1 year ago

freak12techno commented 1 year ago

Summary

I have some tools that are running some cosmovisor subcommands and are expecting a valid JSON in a response, and when I use the binary itself without cosmovisor, it returns a valid json, but with cosmovisor, it adds its own logs, making the result an invalid JSON. Example: querying for version (note the running app line):

$ cosmovisor run version --long --output json
10:47PM INF running app args=["version","--long","--output","json"] module=cosmovisor path=/home/validator/.gaia/cosmovisor/upgrades/v8-rho/bin/gaiad
{"name":"gaia","server_name":"gaiad","version":"v8.0.1","commit":"890ab3aa2e5788537b0d2ebc9bafdc968340e0e5","build_tags":"netgo ledger,","go":"go version go1.18.5 linux/amd64","build_deps":["cosmossdk.io/api@v0.2.6","cosmossdk.io/core@v0.5.1","cosmossdk.io/depinject@v1.0.0-alpha.3","filippo.io/edwards25519@v1.0.0-rc.1","github.com/99designs/keyring@v1.2.1 =\u003e github.com/cosmos/keyring@v1.2.0","github.com/ChainSafe/go-schnorrkel@v0.0.0-20200405005733-88cbf1b4c40d","github.com/Workiva/go-datastructures@v1.0.53","github.com/armon/go-metrics@v0.4.0","github.com/beorn7/perks@v1.0.1","github.com/bgentry/speakeasy@v0.1.1-0.20220910012023-760eaf8b6816","github.com/btcsuite/btcd@v0.23.0 =\u003e github.com/btcsuite/btcd@v0.22.2","github.com/btcsuite/btcd/btcec/v2@v2.3.2","github.com/cenkalti/backoff/v4@v4.1.3","github.com/cespare/xxhash/v2@v2.1.2","github.com/coinbase/rosetta-sdk-go@v0.7.9","github.com/confio/ics23/go@v0.9.0 =\u003e github.com/confio/ics23/go@v0.9.0","github.com/cosmos/btcutil@v1.0.4","github.com/cosmos/cosmos-db@v0.0.0-20221226095112-f3c38ecb5e32","github.com/cosmos/cosmos-proto@v1.0.0-beta.1","github.com/cosmos/cosmos-sdk@v0.45.14 =\u003e github.com/cosmos/cosmos-sdk@v0.45.14","github.com/cosmos/go-bip39@v1.0.0","github.com/cosmos/iavl@v0.19.5","github.com/cosmos/ibc-go/v3@v3.4.0 =\u003e github.com/cosmos/ibc-go/v3@v3.4.0","github.com/cosmos/ledger-cosmos-go@v0.12.2","github.com/creachadair/taskgroup@v0.3.2","github.com/davecgh/go-spew@v1.1.1","github.com/decred/dcrd/dcrec/secp256k1/v4@v4.0.1","github.com/desertbit/timer@v0.0.0-20180107155436-c41aec40b27f","github.com/dvsekhvalnov/jose2go@v1.5.0","github.com/felixge/httpsnoop@v1.0.1","github.com/fsnotify/fsnotify@v1.6.0","github.com/go-kit/kit@v0.12.0","github.com/go-kit/log@v0.2.1","github.com/go-logfmt/logfmt@v0.5.1","github.com/godbus/dbus@v0.0.0-20190726142602-4481cbc300e2","github.com/gogo/gateway@v1.1.0","github.com/gogo/protobuf@v1.3.3 =\u003e github.com/regen-network/protobuf@v1.3.3-alpha.regen.1","github.com/golang/protobuf@v1.5.2","github.com/golang/snappy@v0.0.4","github.com/google/btree@v1.1.2","github.com/google/orderedcode@v0.0.1","github.com/gorilla/handlers@v1.5.1","github.com/gorilla/mux@v1.8.0","github.com/gorilla/websocket@v1.5.0","github.com/gravity-devs/liquidity@v1.5.3","github.com/grpc-ecosystem/go-grpc-middleware@v1.3.0","github.com/grpc-ecosystem/grpc-gateway@v1.16.0","github.com/grpc-ecosystem/grpc-gateway/v2@v2.0.1","github.com/gsterjov/go-libsecret@v0.0.0-20161001094733-a6f4afe4910c","github.com/gtank/merlin@v0.1.1","github.com/gtank/ristretto255@v0.1.2","github.com/hashicorp/go-immutable-radix@v1.3.1","github.com/hashicorp/golang-lru@v0.5.5-0.20210104140557-80c98217689d","github.com/hashicorp/hcl@v1.0.0","github.com/hdevalence/ed25519consensus@v0.0.0-20220222234857-c00d1f31bab3","github.com/improbable-eng/grpc-web@v0.15.0","github.com/klauspost/compress@v1.15.11","github.com/lib/pq@v1.10.6","github.com/libp2p/go-buffer-pool@v0.1.0","github.com/magiconair/properties@v1.8.6","github.com/mattn/go-colorable@v0.1.13","github.com/mattn/go-isatty@v0.0.16","github.com/matttproud/golang_protobuf_extensions@v1.0.2-0.20181231171920-c182affec369","github.com/mimoo/StrobeGo@v0.0.0-20210601165009-122bf33a46e0","github.com/minio/highwayhash@v1.0.2","github.com/mitchellh/mapstructure@v1.5.0","github.com/mtibben/percent@v0.2.1","github.com/pelletier/go-toml/v2@v2.0.5","github.com/pkg/errors@v0.9.1","github.com/pmezard/go-difflib@v1.0.0","github.com/prometheus/client_golang@v1.14.0","github.com/prometheus/client_model@v0.3.0","github.com/prometheus/common@v0.37.0","github.com/prometheus/procfs@v0.8.0","github.com/rakyll/statik@v0.1.7","github.com/rcrowley/go-metrics@v0.0.0-20201227073835-cf1acfcdf475","github.com/regen-network/cosmos-proto@v0.3.1","github.com/rs/cors@v1.8.2","github.com/rs/zerolog@v1.27.0","github.com/spf13/afero@v1.9.2","github.com/spf13/cast@v1.5.0","github.com/spf13/cobra@v1.6.1","github.com/spf13/jwalterweatherman@v1.1.0","github.com/spf13/pflag@v1.0.5","github.com/spf13/viper@v1.14.0","github.com/strangelove-ventures/packet-forward-middleware/v3@v3.1.1","github.com/stretchr/testify@v1.8.1","github.com/subosito/gotenv@v1.4.1","github.com/syndtr/goleveldb@v1.0.1-0.20210819022825-2ae1ddf74ef7","github.com/tendermint/go-amino@v0.16.0","github.com/tendermint/tendermint@v0.34.26 =\u003e github.com/informalsystems/tendermint@v0.34.26","github.com/tendermint/tm-db@v0.6.7","github.com/tidwall/btree@v1.5.0","github.com/zondax/hid@v0.9.1","github.com/zondax/ledger-go@v0.14.1","golang.org/x/crypto@v0.5.0","golang.org/x/exp@v0.0.0-20221205204356-47842c84f3db","golang.org/x/net@v0.5.0","golang.org/x/sys@v0.4.0","golang.org/x/term@v0.4.0","golang.org/x/text@v0.6.0","google.golang.org/genproto@v0.0.0-20230125152338-dcaf20b6aeaa","google.golang.org/grpc@v1.52.3 =\u003e google.golang.org/grpc@v1.33.2","google.golang.org/protobuf@v1.28.2-0.20220831092852-f930b1dc76e8","gopkg.in/ini.v1@v1.67.0","gopkg.in/yaml.v2@v2.4.0","gopkg.in/yaml.v3@v3.0.1","nhooyr.io/websocket@v1.8.6"],"cosmos_sdk_version":"v0.45.14"}
$ gaiad version --long --output json
{"name":"gaia","server_name":"gaiad","version":"v8.0.1","commit":"890ab3aa2e5788537b0d2ebc9bafdc968340e0e5","build_tags":"netgo ledger,","go":"go version go1.18.5 linux/amd64","build_deps":["cosmossdk.io/api@v0.2.6","cosmossdk.io/core@v0.5.1","cosmossdk.io/depinject@v1.0.0-alpha.3","filippo.io/edwards25519@v1.0.0-rc.1","github.com/99designs/keyring@v1.2.1 =\u003e github.com/cosmos/keyring@v1.2.0","github.com/ChainSafe/go-schnorrkel@v0.0.0-20200405005733-88cbf1b4c40d","github.com/Workiva/go-datastructures@v1.0.53","github.com/armon/go-metrics@v0.4.0","github.com/beorn7/perks@v1.0.1","github.com/bgentry/speakeasy@v0.1.1-0.20220910012023-760eaf8b6816","github.com/btcsuite/btcd@v0.23.0 =\u003e github.com/btcsuite/btcd@v0.22.2","github.com/btcsuite/btcd/btcec/v2@v2.3.2","github.com/cenkalti/backoff/v4@v4.1.3","github.com/cespare/xxhash/v2@v2.1.2","github.com/coinbase/rosetta-sdk-go@v0.7.9","github.com/confio/ics23/go@v0.9.0 =\u003e github.com/confio/ics23/go@v0.9.0","github.com/cosmos/btcutil@v1.0.4","github.com/cosmos/cosmos-db@v0.0.0-20221226095112-f3c38ecb5e32","github.com/cosmos/cosmos-proto@v1.0.0-beta.1","github.com/cosmos/cosmos-sdk@v0.45.14 =\u003e github.com/cosmos/cosmos-sdk@v0.45.14","github.com/cosmos/go-bip39@v1.0.0","github.com/cosmos/iavl@v0.19.5","github.com/cosmos/ibc-go/v3@v3.4.0 =\u003e github.com/cosmos/ibc-go/v3@v3.4.0","github.com/cosmos/ledger-cosmos-go@v0.12.2","github.com/creachadair/taskgroup@v0.3.2","github.com/davecgh/go-spew@v1.1.1","github.com/decred/dcrd/dcrec/secp256k1/v4@v4.0.1","github.com/desertbit/timer@v0.0.0-20180107155436-c41aec40b27f","github.com/dvsekhvalnov/jose2go@v1.5.0","github.com/felixge/httpsnoop@v1.0.1","github.com/fsnotify/fsnotify@v1.6.0","github.com/go-kit/kit@v0.12.0","github.com/go-kit/log@v0.2.1","github.com/go-logfmt/logfmt@v0.5.1","github.com/godbus/dbus@v0.0.0-20190726142602-4481cbc300e2","github.com/gogo/gateway@v1.1.0","github.com/gogo/protobuf@v1.3.3 =\u003e github.com/regen-network/protobuf@v1.3.3-alpha.regen.1","github.com/golang/protobuf@v1.5.2","github.com/golang/snappy@v0.0.4","github.com/google/btree@v1.1.2","github.com/google/orderedcode@v0.0.1","github.com/gorilla/handlers@v1.5.1","github.com/gorilla/mux@v1.8.0","github.com/gorilla/websocket@v1.5.0","github.com/gravity-devs/liquidity@v1.5.3","github.com/grpc-ecosystem/go-grpc-middleware@v1.3.0","github.com/grpc-ecosystem/grpc-gateway@v1.16.0","github.com/grpc-ecosystem/grpc-gateway/v2@v2.0.1","github.com/gsterjov/go-libsecret@v0.0.0-20161001094733-a6f4afe4910c","github.com/gtank/merlin@v0.1.1","github.com/gtank/ristretto255@v0.1.2","github.com/hashicorp/go-immutable-radix@v1.3.1","github.com/hashicorp/golang-lru@v0.5.5-0.20210104140557-80c98217689d","github.com/hashicorp/hcl@v1.0.0","github.com/hdevalence/ed25519consensus@v0.0.0-20220222234857-c00d1f31bab3","github.com/improbable-eng/grpc-web@v0.15.0","github.com/klauspost/compress@v1.15.11","github.com/lib/pq@v1.10.6","github.com/libp2p/go-buffer-pool@v0.1.0","github.com/magiconair/properties@v1.8.6","github.com/mattn/go-colorable@v0.1.13","github.com/mattn/go-isatty@v0.0.16","github.com/matttproud/golang_protobuf_extensions@v1.0.2-0.20181231171920-c182affec369","github.com/mimoo/StrobeGo@v0.0.0-20210601165009-122bf33a46e0","github.com/minio/highwayhash@v1.0.2","github.com/mitchellh/mapstructure@v1.5.0","github.com/mtibben/percent@v0.2.1","github.com/pelletier/go-toml/v2@v2.0.5","github.com/pkg/errors@v0.9.1","github.com/pmezard/go-difflib@v1.0.0","github.com/prometheus/client_golang@v1.14.0","github.com/prometheus/client_model@v0.3.0","github.com/prometheus/common@v0.37.0","github.com/prometheus/procfs@v0.8.0","github.com/rakyll/statik@v0.1.7","github.com/rcrowley/go-metrics@v0.0.0-20201227073835-cf1acfcdf475","github.com/regen-network/cosmos-proto@v0.3.1","github.com/rs/cors@v1.8.2","github.com/rs/zerolog@v1.27.0","github.com/spf13/afero@v1.9.2","github.com/spf13/cast@v1.5.0","github.com/spf13/cobra@v1.6.1","github.com/spf13/jwalterweatherman@v1.1.0","github.com/spf13/pflag@v1.0.5","github.com/spf13/viper@v1.14.0","github.com/strangelove-ventures/packet-forward-middleware/v3@v3.1.1","github.com/stretchr/testify@v1.8.1","github.com/subosito/gotenv@v1.4.1","github.com/syndtr/goleveldb@v1.0.1-0.20210819022825-2ae1ddf74ef7","github.com/tendermint/go-amino@v0.16.0","github.com/tendermint/tendermint@v0.34.26 =\u003e github.com/informalsystems/tendermint@v0.34.26","github.com/tendermint/tm-db@v0.6.7","github.com/tidwall/btree@v1.5.0","github.com/zondax/hid@v0.9.1","github.com/zondax/ledger-go@v0.14.1","golang.org/x/crypto@v0.5.0","golang.org/x/exp@v0.0.0-20221205204356-47842c84f3db","golang.org/x/net@v0.5.0","golang.org/x/sys@v0.4.0","golang.org/x/term@v0.4.0","golang.org/x/text@v0.6.0","google.golang.org/genproto@v0.0.0-20230125152338-dcaf20b6aeaa","google.golang.org/grpc@v1.52.3 =\u003e google.golang.org/grpc@v1.33.2","google.golang.org/protobuf@v1.28.2-0.20220831092852-f930b1dc76e8","gopkg.in/ini.v1@v1.67.0","gopkg.in/yaml.v2@v2.4.0","gopkg.in/yaml.v3@v3.0.1","nhooyr.io/websocket@v1.8.6"],"cosmos_sdk_version":"v0.45.14"}

Would be nice to have the way to omit such logs, see below for suggestions.

I am all in for implementing it by myself as I want to get my hands on cosmos-sdk as a contributor, just saying ;) I'd need some guidance for it though.

Problem Definition

Adding the way to silence cosmovisor logs would make it easier for other tools that rely on a plain output to be a valid JSON to use it. I do not see any disadvantages on being able to set it up.

Proposal

I suggest having an environmental variable (like DAEMON_DISABLE_LOGS) that will silence logs if set to true.

julienrbrt commented 1 year ago

This is already the case. Which Cosmovisor version are you using?

You have to run:

$ cosmovisor version --output json

When using the run command, it will always show logs.

freak12techno commented 1 year ago

@julienrbrt when running your command, it indeed works correctly (though I am running in another issue I've described here https://github.com/cosmos/cosmos-sdk/issues/15360#issuecomment-1464632111, but this is not the subject of this issue). But this only solves part of the problem, while it allows silencing logs querying for version, it still displays them for every other command, for example:

$ cosmovisor run q staking params --output json
11:52PM INF running app args=["q","staking","params","--output","json"] module=cosmovisor path=/home/validator/.bitsongd/cosmovisor/upgrades/v014/bin/bitsongd
{"unbonding_time":"1814400s","max_validators":100,"max_entries":7,"historical_entries":10000,"bond_denom":"ubtsg"}

and this also results in the response not being a valid JSON (while it is a valid JSON if running this command without Cosmovisor).

I assume checking for every command in cosmovisor run <something> if it has --output json is too expensive and complicated, therefore I guess the env variable solution might be nice. What do you think?

This is already the case. Which Cosmovisor version are you using?

1.3.0, also tested with 1.4.0

julienrbrt commented 1 year ago

I think your proposal makes sense. Disabling cosmovisor logger altogether could be an option.

freak12techno commented 1 year ago

I may help implementing it, if needed, just need some guidance on how to do it in a better way. @julienrbrt jfyi

julienrbrt commented 1 year ago

I may help implementing it, if needed, just need some guidance on how to do it in a better way. @julienrbrt jfyi

Sure, if you want to 👍 You need to change the logger created with the NoOp logger when that environment variable is present.

freak12techno commented 1 year ago

@julienrbrt okay I am mostly done, I have one issue though. In tools/cosmovisor/process.go#NewLauncher, it casts logger into zerolog.Logger, which would fail if it's not zerolog.Logger (which isn't true if it's noopLogger), and zerolog.Logger and noopLogger are not compatible as they have different methods and signatures. I see the following solutions here:

What do you think would be better approach, or do you maybe know some better ways to deal with it?

julienrbrt commented 1 year ago

No, you do not need to modify anything in log/*. You can indeed choose to type cast everything when Impl() is used or use zerolog no-op logger with logger := log.NewCustomLogger(zerolog.Nop()). We should not need to have zerolog.Logger type anywhere I think actually, if not yet done, we can change it do log.Logger`.

freak12techno commented 1 year ago

@julienrbrt okay submitted this https://github.com/cosmos/cosmos-sdk/pull/15362, can you review please? also pretty sure we also need to update at least docs here https://docs.cosmos.network/main/tooling/cosmovisor#command-line-arguments-and-environment-variables to add this, not sure where should I put it

julienrbrt commented 1 year ago

@julienrbrt okay submitted this https://github.com/cosmos/cosmos-sdk/pull/15362, can you review please?

also pretty sure we also need to update at least docs here https://docs.cosmos.network/main/tooling/cosmovisor#command-line-arguments-and-environment-variables to add this, not sure where should I put it

I'll have a look, thank you! You can update the README and it will update on the website afterwards.

freak12techno commented 1 year ago

You can update the README and it will update on the website afterwards.

Done, thanks for letting me know.

One thing I am not sure are tests, they fail even without my PR, so it's not something I broke within my changes I assume, so I didn't bother fixing them:

--- FAIL: TestInitTestSuite (0.02s)
    --- FAIL: TestInitTestSuite/TestInitializeCosmovisorNegativeValidation (0.00s)
        init_test.go:60: Clearing environment variables.
        --- FAIL: TestInitTestSuite/TestInitializeCosmovisorNegativeValidation/no_args (0.00s)
            testing.go:1319: race detected during execution of test
        init_test.go:74: Restoring environment variables.
        init_test.go:92: done unsetting DAEMON_HOME
        init_test.go:92: done unsetting DAEMON_NAME
        testing.go:1319: race detected during execution of test
    testing.go:1319: race detected during execution of test
cosmovisor version: (devel)
FAIL
FAIL    cosmossdk.io/tools/cosmovisor/cmd/cosmovisor    0.765s
ok      cosmossdk.io/tools/cosmovisor/errors    (cached)
FAIL
make: *** [test] Error 1