Skip to content

Commit

Permalink
Use CTF logging lib
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcauchi committed Dec 13, 2024
1 parent 4fd44a3 commit e6242bd
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
2 changes: 1 addition & 1 deletion sentinel/internal/chain_poller/chain_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewChainPoller(cfg ChainPollerConfig) (*ChainPoller, error) {
return nil, errors.New("chain ID not set")
}

logger := cfg.Logger.With().Str("component", "ChainPoller").Logger().With().Int64("ChainID", cfg.ChainID).Logger()
logger := cfg.Logger.With().Str("Component", "ChainPoller").Logger().With().Int64("ChainID", cfg.ChainID).Logger()

return &ChainPoller{
blockchainClient: cfg.BlockchainClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func NewChainPollerService(cfg ChainPollerServiceConfig) (*ChainPollerService, e
return nil, fmt.Errorf("failed to initialize ChainPoller: %w", err)
}

l := cfg.Logger.With().Str("component", "ChainPollerService").Logger().With().Int64("ChainID", cfg.ChainID).Logger()
l := cfg.Logger.With().Str("Component", "ChainPollerService").Logger().With().Int64("ChainID", cfg.ChainID).Logger()

cfg.Logger = &l

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type SubscriptionManager struct {

// NewSubscriptionManager initializes a new SubscriptionManager.
func NewSubscriptionManager(cfg SubscriptionManagerConfig) *SubscriptionManager {
subscriptionManagerLogger := cfg.Logger.With().Str("component", "SubscriptionManager").Logger()
subscriptionManagerLogger := cfg.Logger.With().Str("Component", "SubscriptionManager").Logger()

return &SubscriptionManager{
registry: make(map[internal.EventKey][]chan api.Log),
Expand Down
18 changes: 18 additions & 0 deletions sentinel/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// File: log.go
package sentinel

import (
"testing"

"github.com/rs/zerolog"
"github.com/smartcontractkit/chainlink-testing-framework/lib/logging"
)

const (
LogLevelEnvVar = "SENTINEL_LOG_LEVEL"
)

// GetLogger instantiates a logger that takes into account the test context and the log level
func GetLogger(t *testing.T, componentName string) zerolog.Logger {
return logging.GetLogger(t, LogLevelEnvVar).With().Str("Component", componentName).Logger()
}
19 changes: 11 additions & 8 deletions sentinel/sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package sentinel
import (
"fmt"
"sync"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -14,7 +15,7 @@ import (

// SentinelConfig holds configuration for the Sentinel.
type SentinelConfig struct {
Logger zerolog.Logger
t *testing.T
}

type AddChainConfig struct {
Expand All @@ -24,18 +25,20 @@ type AddChainConfig struct {
}

type Sentinel struct {
config SentinelConfig
l *zerolog.Logger
mu sync.RWMutex
services map[int64]*chain_poller_service.ChainPollerService // Map of chainID to ChianPollerService
}

// NewSentinel initializes and returns a new Sentinel instance.
func NewSentinel(cfg SentinelConfig) *Sentinel {
cfg.Logger = cfg.Logger.With().Str("component", "Sentinel").Logger()
cfg.Logger.Info().Msg("Initializing Sentinel")
logger := GetLogger(cfg.t, "Sentinel")
logger.Info().Msg("Initializing Sentinel")
logger.Debug().Msg("Initializing Sentinel")
logger.Info().Str("Level", logger.GetLevel().String()).Msg("Initializing Sentinel")
return &Sentinel{
config: cfg,
services: make(map[int64]*chain_poller_service.ChainPollerService),
l: &logger,
}
}

Expand All @@ -51,7 +54,7 @@ func (s *Sentinel) AddChain(acc AddChainConfig) error {
cfg := chain_poller_service.ChainPollerServiceConfig{
PollInterval: acc.PollInterval,
ChainID: acc.ChainID,
Logger: &s.config.Logger,
Logger: s.l,
BlockchainClient: acc.BlockchainClient,
}

Expand All @@ -60,7 +63,7 @@ func (s *Sentinel) AddChain(acc AddChainConfig) error {
return fmt.Errorf("failed to initialize ChainPollerService: %w", err)
}
s.services[cfg.ChainID] = eps
s.config.Logger.Info().Int64("ChainID", cfg.ChainID).Msg("Added new chain")
s.l.Info().Int64("ChainID", cfg.ChainID).Msg("Added new chain")
eps.Start()
return nil
}
Expand All @@ -77,7 +80,7 @@ func (s *Sentinel) RemoveChain(chainID int64) error {

eps.Stop()
delete(s.services, chainID)
s.config.Logger.Info().Msg("Removed chain")
s.l.Info().Msg("Removed chain")
return nil
}

Expand Down
6 changes: 1 addition & 5 deletions sentinel/sentinel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@ import (
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-testing-framework/lib/logging"
"github.com/smartcontractkit/chainlink-testing-framework/sentinel/api"
"github.com/smartcontractkit/chainlink-testing-framework/sentinel/internal"
)

// Helper function to initialize a Sentinel instance for testing.
func setupSentinel(t *testing.T) *Sentinel {
t.Helper()
logger := logging.GetTestLogger(t)
s := NewSentinel(SentinelConfig{
Logger: logger,
})
s := NewSentinel(SentinelConfig{t: t})

// Ensure Sentinel is closed after the test.
t.Cleanup(func() {
Expand Down

0 comments on commit e6242bd

Please sign in to comment.