Skip to content

Commit 77a1900

Browse files
Improve unit test logs (#797)
<!-- < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < ☺ v ✰ Thanks for creating a PR! You're awesome! ✰ v Please note that maintainers will only review those PRs with a completed PR template. ☺ > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > --> ## Purpose of Changes and their Description This PR refactors the logging setup used in unit tests to produce cleaner, more readable output. ### Before: <img width="1381" alt="Screenshot 2025-04-10 at 16 01 19" src="https://github.com/user-attachments/assets/393f7d25-140a-4e1b-8160-fe15534c2d85" /> ### After: ![Screenshot 2025-04-10 at 13 10 57](https://github.com/user-attachments/assets/8d485047-0225-4012-bd03-a8fee4f65736) ## Are these changes tested and documented? - [x] If tested, please describe how. If not, why tests are not needed. - [ ] If documented, please describe where. If not, describe why docs are not needed. - [x] Added to `Unreleased` section of `CHANGELOG.md`?
2 parents 535e62c + 45f38a7 commit 77a1900

6 files changed

Lines changed: 55 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
103103
* [#775](https://github.com/allora-network/allora-chain/pull/775) Pipe a "y" to key add command in l1_node.sh script
104104
* [#783](https://github.com/allora-network/allora-chain/pull/783) Add API Breaking Changes section to CHANGELOG.md
105105
* [#787](https://github.com/allora-network/allora-chain/pull/785) Bounded Dec: Utility functions to cap value to bounds
106+
* [#797](https://github.com/allora-network/allora-chain/pull/797) Improve unit test logs
106107

107108
### Changed
108109

log/logger.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package log
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"time"
8+
9+
"cosmossdk.io/log"
10+
"github.com/rs/zerolog"
11+
)
12+
13+
// nolint: exhaustruct
14+
func NewTestLogger(t zerolog.TestingLog) log.Logger {
15+
lvl := zerolog.DebugLevel
16+
cw := zerolog.ConsoleWriter{
17+
Out: os.Stderr,
18+
TimeFormat: time.RFC3339Nano,
19+
NoColor: true,
20+
FieldsExclude: []string{"module"},
21+
}
22+
23+
// Clean up formatter: raw message only
24+
cw.FormatLevel = func(i interface{}) string { return "" }
25+
cw.FormatCaller = func(i interface{}) string {
26+
if fullPath, ok := i.(string); ok {
27+
if cwd, err := os.Getwd(); err == nil {
28+
if relPath, err := filepath.Rel(cwd, fullPath); err == nil {
29+
return fmt.Sprintf("[%s]", relPath)
30+
}
31+
}
32+
return fmt.Sprintf("[%s]", fullPath)
33+
}
34+
return "[no-caller]"
35+
}
36+
cw.FormatTimestamp = func(i interface{}) string {
37+
return fmt.Sprintf("[%s]", i)
38+
}
39+
zerolog.TimestampFieldName = "time"
40+
return log.NewCustomLogger(zerolog.New(cw).With().CallerWithSkipFrameCount(4).Timestamp().Logger().Level(lvl))
41+
}

x/emissions/keeper/actor_utils/worker_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
cosmosMath "cosmossdk.io/math"
1010
storetypes "cosmossdk.io/store/types"
1111
"github.com/allora-network/allora-chain/app/params"
12+
alloralog "github.com/allora-network/allora-chain/log"
1213
alloraMath "github.com/allora-network/allora-chain/math"
1314
alloratestutil "github.com/allora-network/allora-chain/test/testutil"
1415
"github.com/allora-network/allora-chain/x/emissions/keeper"
@@ -57,7 +58,7 @@ func (s *WorkerTestSuite) SetupTest() {
5758
storeService := runtime.NewKVStoreService(key)
5859
testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test"))
5960
// Set logger to show logs from the rewards module too
60-
logger := log.NewTestLogger(s.T()).With("module", "rewards")
61+
logger := alloralog.NewTestLogger(s.T()).With("module", "rewards")
6162
ctx := testCtx.Ctx.WithHeaderInfo(header.Info{Time: time.Now()}).WithLogger(logger) // nolint: exhaustruct
6263
encCfg := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{}, bank.AppModuleBasic{}, module.AppModule{})
6364

x/emissions/keeper/inference_synthesis/network_inference_builder_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
storetypes "cosmossdk.io/store/types"
1515
"github.com/allora-network/allora-chain/app/params"
16+
alloralog "github.com/allora-network/allora-chain/log"
1617
alloraMath "github.com/allora-network/allora-chain/math"
1718
alloratestutil "github.com/allora-network/allora-chain/test/testutil"
1819
"github.com/allora-network/allora-chain/x/emissions/keeper"
@@ -59,7 +60,7 @@ func (s *InferenceSynthesisTestSuite) SetupTest() {
5960
storeService := runtime.NewKVStoreService(key)
6061
testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test"))
6162
// Set logger to show logs from the rewards module too
62-
logger := log.NewTestLogger(s.T()).With("module", "inference_synthesis")
63+
logger := alloralog.NewTestLogger(s.T()).With("module", "inference_synthesis")
6364
ctx := testCtx.Ctx.WithHeaderInfo(header.Info{
6465
Height: 1,
6566
Hash: []byte("1"),

x/emissions/keeper/inference_synthesis/weights_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"cosmossdk.io/log"
99
storetypes "cosmossdk.io/store/types"
1010
"github.com/allora-network/allora-chain/app/params"
11+
alloralog "github.com/allora-network/allora-chain/log"
1112
alloraMath "github.com/allora-network/allora-chain/math"
1213
alloratestutil "github.com/allora-network/allora-chain/test/testutil"
1314
"github.com/allora-network/allora-chain/x/emissions/keeper"
@@ -56,7 +57,7 @@ func (s *WeightsTestSuite) SetupTest() {
5657
storeService := runtime.NewKVStoreService(key)
5758
testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test"))
5859
// Set logger to show logs from the rewards module too
59-
logger := log.NewTestLogger(s.T()).With("module", "inference_synthesis")
60+
logger := alloralog.NewTestLogger(s.T()).With("module", "inference_synthesis")
6061
ctx := testCtx.Ctx.WithHeaderInfo(header.Info{
6162
Height: 1,
6263
Hash: []byte("1"),

x/emissions/module/rewards/rewards_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,25 @@ import (
55
"testing"
66
"time"
77

8-
actorutils "github.com/allora-network/allora-chain/x/emissions/keeper/actor_utils"
9-
inferencesynthesis "github.com/allora-network/allora-chain/x/emissions/keeper/inference_synthesis"
10-
118
"cosmossdk.io/core/header"
129
"cosmossdk.io/log"
1310
cosmosMath "cosmossdk.io/math"
1411
storetypes "cosmossdk.io/store/types"
1512
"github.com/allora-network/allora-chain/app/params"
13+
alloralog "github.com/allora-network/allora-chain/log"
1614
alloraMath "github.com/allora-network/allora-chain/math"
1715
alloratestutil "github.com/allora-network/allora-chain/test/testutil"
1816
"github.com/allora-network/allora-chain/x/emissions/keeper"
17+
actorutils "github.com/allora-network/allora-chain/x/emissions/keeper/actor_utils"
18+
inferencesynthesis "github.com/allora-network/allora-chain/x/emissions/keeper/inference_synthesis"
1919
"github.com/allora-network/allora-chain/x/emissions/keeper/msgserver"
2020
"github.com/allora-network/allora-chain/x/emissions/module"
2121
"github.com/allora-network/allora-chain/x/emissions/module/rewards"
2222
"github.com/allora-network/allora-chain/x/emissions/types"
2323
mintkeeper "github.com/allora-network/allora-chain/x/mint/keeper"
2424
mint "github.com/allora-network/allora-chain/x/mint/module"
2525
minttypes "github.com/allora-network/allora-chain/x/mint/types"
26+
2627
"github.com/cometbft/cometbft/crypto/secp256k1"
2728
codecAddress "github.com/cosmos/cosmos-sdk/codec/address"
2829
"github.com/cosmos/cosmos-sdk/runtime"
@@ -67,7 +68,8 @@ func (s *RewardsTestSuite) SetupTest() {
6768
storeService := runtime.NewKVStoreService(key)
6869
testCtx := testutil.DefaultContextWithDB(s.T(), key, storetypes.NewTransientStoreKey("transient_test"))
6970
// Set logger to show logs from the rewards module too
70-
logger := log.NewTestLogger(s.T()).With("module", "rewards")
71+
72+
logger := alloralog.NewTestLogger(s.T()).With("module", "rewards")
7173
ctx := testCtx.Ctx.WithHeaderInfo(header.Info{Time: time.Now()}).WithLogger(logger) // nolint: exhaustruct
7274
encCfg := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{}, bank.AppModuleBasic{}, module.AppModule{})
7375

@@ -1118,7 +1120,7 @@ func (s *RewardsTestSuite) TestIncreasingTaskRewardAlphaIncreasesImportanceOfPre
11181120
// We increase alpha between the trials to prove that their worsening performance decreases regret.
11191121
// This is somewhat counterintuitive, but can be explained by the following passage from the litepaper:
11201122
// "A positive regret implies that the inference of worker j is expected by worker k to outperform
1121-
// the networks previously reported accuracy, whereas a negative regret indicates that the network
1123+
// the network's previously reported accuracy, whereas a negative regret indicates that the network
11221124
// is expected to be more accurate."
11231125
func (s *RewardsTestSuite) TestIncreasingAlphaRegretIncreasesPresentEffectOnRegret() {
11241126
/// SETUP

0 commit comments

Comments
 (0)