Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
MVP for logging refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Apr 14, 2022
1 parent 754f074 commit 54ec506
Show file tree
Hide file tree
Showing 29 changed files with 154 additions and 341 deletions.
5 changes: 3 additions & 2 deletions cachestore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cachestore
import (
"context"

"github.com/BuxOrg/bux/logger"
"github.com/coocood/freecache"
"github.com/mrz1836/go-cache"
"github.com/newrelic/go-agent/v3/newrelic"
Expand All @@ -20,7 +21,7 @@ type (
debug bool // For extra logs and additional debug information
engine Engine // Cachestore engine (redis or mcache)
freecache *freecache.Cache // Driver (client) for local in-memory storage
logger Logger // Internal logging
logger logger.Interface // Internal logging
newRelicEnabled bool // If NewRelic is enabled (parent application)
redis *cache.Client // Current redis client (read & write)
redisConfig *RedisConfig // Configuration for a new redis client
Expand All @@ -43,7 +44,7 @@ func NewClient(ctx context.Context, opts ...ClientOps) (ClientInterface, error)

// Set logger if not set
if client.options.logger == nil {
client.options.logger = newBasicLogger(client.IsDebug())
client.options.logger = logger.NewLogger(client.IsDebug())
}

// EMPTY! Engine was NOT set, show warning and use in-memory cache
Expand Down
3 changes: 2 additions & 1 deletion cachestore/client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"strings"

"github.com/BuxOrg/bux/logger"
"github.com/coocood/freecache"
"github.com/mrz1836/go-cache"
"github.com/newrelic/go-agent/v3/newrelic"
Expand Down Expand Up @@ -108,7 +109,7 @@ func WithFreeCacheConnection(client *freecache.Cache) ClientOps {
}

// WithLogger will set the custom logger interface
func WithLogger(customLogger Logger) ClientOps {
func WithLogger(customLogger logger.Interface) ClientOps {
return func(c *clientOptions) {
if customLogger != nil {
c.logger = customLogger
Expand Down
3 changes: 2 additions & 1 deletion cachestore/client_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/BuxOrg/bux/logger"
"github.com/BuxOrg/bux/tester"
"github.com/coocood/freecache"
"github.com/mrz1836/go-cache"
Expand Down Expand Up @@ -250,7 +251,7 @@ func TestWithLogger(t *testing.T) {

t.Run("test applying option", func(t *testing.T) {
options := &clientOptions{}
customClient := newBasicLogger(true)
customClient := logger.NewLogger(true)
opt := WithLogger(customClient)
opt(options)
assert.Equal(t, customClient, options.logger)
Expand Down
6 changes: 0 additions & 6 deletions cachestore/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import (
"github.com/mrz1836/go-cache"
)

// Logger is the logger interface for debug messages
type Logger interface {
Info(ctx context.Context, message string, params ...interface{})
Warn(ctx context.Context, message string, params ...interface{})
}

// LockService are the locking related methods
type LockService interface {
ReleaseLock(ctx context.Context, lockKey, secret string) (bool, error)
Expand Down
55 changes: 0 additions & 55 deletions cachestore/logger.go

This file was deleted.

13 changes: 7 additions & 6 deletions chainstate/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"github.com/BuxOrg/bux/logger"
"github.com/mrz1836/go-mattercloud"
"github.com/mrz1836/go-nownodes"
"github.com/mrz1836/go-whatsonchain"
Expand All @@ -20,11 +21,11 @@ type (

// clientOptions holds all the configuration for the client
clientOptions struct {
config *syncConfig // Configuration for broadcasting and other chain-state actions
debug bool // For extra logs and additional debug information
logger Logger // Internal logger interface
newRelicEnabled bool // If NewRelic is enabled (parent application)
userAgent string // Custom user agent for outgoing HTTP Requests
config *syncConfig // Configuration for broadcasting and other chain-state actions
debug bool // For extra logs and additional debug information
logger logger.Interface // Internal logger interface
newRelicEnabled bool // If NewRelic is enabled (parent application)
userAgent string // Custom user agent for outgoing HTTP Requests
}

// syncConfig holds all the configuration about the different sync processes
Expand Down Expand Up @@ -69,7 +70,7 @@ func NewClient(ctx context.Context, opts ...ClientOps) (ClientInterface, error)

// Set logger if not set
if client.options.logger == nil {
client.options.logger = newBasicLogger(client.IsDebug())
client.options.logger = logger.NewLogger(client.IsDebug())
}

// Start Minercraft
Expand Down
3 changes: 2 additions & 1 deletion chainstate/client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"github.com/BuxOrg/bux/logger"
"github.com/mrz1836/go-mattercloud"
"github.com/mrz1836/go-nownodes"
"github.com/mrz1836/go-whatsonchain"
Expand Down Expand Up @@ -213,7 +214,7 @@ func WithCustomMiners(miners []*minercraft.Miner) ClientOps {
}

// WithLogger will set a custom logger
func WithLogger(customLogger Logger) ClientOps {
func WithLogger(customLogger logger.Interface) ClientOps {
return func(c *clientOptions) {
if customLogger != nil {
c.logger = customLogger
Expand Down
3 changes: 2 additions & 1 deletion chainstate/client_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

"github.com/BuxOrg/bux/logger"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tonicpow/go-minercraft"
Expand Down Expand Up @@ -469,7 +470,7 @@ func TestWithLogger(t *testing.T) {
options := &clientOptions{
config: &syncConfig{},
}
customClient := newBasicLogger(true)
customClient := logger.NewLogger(true)
opt := WithLogger(customClient)
opt(options)
assert.Equal(t, customClient, options.logger)
Expand Down
5 changes: 0 additions & 5 deletions chainstate/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ type HTTPInterface interface {
Do(req *http.Request) (*http.Response, error)
}

// Logger is the logger interface for debug messages
type Logger interface {
Info(ctx context.Context, message string, params ...interface{})
}

// ChainService is the chain related methods
type ChainService interface {
Broadcast(ctx context.Context, id, txHex string, timeout time.Duration) error
Expand Down
48 changes: 0 additions & 48 deletions chainstate/logger.go

This file was deleted.

7 changes: 3 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/tonicpow/go-paymail"
"github.com/tonicpow/go-paymail/server"
glogger "gorm.io/gorm/logger"
)

type (
Expand All @@ -33,7 +32,7 @@ type (
encryptionKey string // Encryption key for encrypting sensitive information (IE: paymail xPub) (hex encoded key)
itc bool // (Incoming Transactions Check) True will check incoming transactions via Miners (real-world)
iuc bool // (Input UTXO Check) True will check input utxos when saving transactions
logger glogger.Interface // Internal logging
logger logger.Interface // Internal logging
models *modelOptions // Configuration options for the loaded models
newRelic *newRelicOptions // Configuration options for NewRelic
notifications *notificationsOptions // Configuration options for Notifications
Expand Down Expand Up @@ -279,7 +278,7 @@ func (c *Client) Datastore() datastore.ClientInterface {
}

// Logger will return the Logger if it exists
func (c *Client) Logger() glogger.Interface {
func (c *Client) Logger() logger.Interface {
return c.options.logger
}

Expand Down Expand Up @@ -407,7 +406,7 @@ func (c *Client) Notifications() notifications.ClientInterface {
return nil
}

// SetNotificationsClient will overwrite the Notifications client with the given client
// SetNotificationsClient will overwrite the notification's client with the given client
func (c *Client) SetNotificationsClient(client notifications.ClientInterface) {
c.options.notifications.client = client
}
Expand Down
36 changes: 10 additions & 26 deletions client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/BuxOrg/bux/cachestore"
"github.com/BuxOrg/bux/chainstate"
"github.com/BuxOrg/bux/datastore"
"github.com/BuxOrg/bux/logger"
"github.com/BuxOrg/bux/notifications"
"github.com/BuxOrg/bux/taskmanager"
"github.com/coocood/freecache"
Expand All @@ -18,7 +19,6 @@ import (
"github.com/tonicpow/go-paymail/server"
"github.com/vmihailenco/taskq/v3"
"go.mongodb.org/mongo-driver/mongo"
"gorm.io/gorm/logger"
)

// ClientOps allow functional options to be supplied that overwrite default client options.
Expand Down Expand Up @@ -178,18 +178,10 @@ func WithNewRelic(app *newrelic.Application) ClientOps {
c.newRelic.app = app

// Enable New relic on other services
if c.chainstate != nil {
c.chainstate.options = append(c.chainstate.options, chainstate.WithNewRelic())
}
if c.cacheStore != nil {
c.cacheStore.options = append(c.cacheStore.options, cachestore.WithNewRelic())
}
if c.dataStore != nil {
c.dataStore.options = append(c.dataStore.options, datastore.WithNewRelic())
}
if c.taskManager != nil {
c.taskManager.options = append(c.taskManager.options, taskmanager.WithNewRelic())
}
c.cacheStore.options = append(c.cacheStore.options, cachestore.WithNewRelic())
c.chainstate.options = append(c.chainstate.options, chainstate.WithNewRelic())
c.dataStore.options = append(c.dataStore.options, datastore.WithNewRelic())
c.taskManager.options = append(c.taskManager.options, taskmanager.WithNewRelic())

// Enable the service
c.newRelic.enabled = true
Expand Down Expand Up @@ -249,19 +241,11 @@ func WithLogger(customLogger logger.Interface) ClientOps {
if customLogger != nil {
c.logger = customLogger

// Enable debugging on other services
if c.chainstate != nil {
c.chainstate.options = append(c.chainstate.options, chainstate.WithLogger(c.logger))
}
if c.taskManager != nil {
c.taskManager.options = append(c.taskManager.options, taskmanager.WithLogger(c.logger))
}
if c.dataStore != nil {
c.dataStore.options = append(c.dataStore.options, datastore.WithLogger(c.logger))
}
if c.cacheStore != nil {
c.cacheStore.options = append(c.cacheStore.options, cachestore.WithLogger(c.logger))
}
// Enable the logger on all services
c.cacheStore.options = append(c.cacheStore.options, cachestore.WithLogger(c.logger))
c.chainstate.options = append(c.chainstate.options, chainstate.WithLogger(c.logger))
c.dataStore.options = append(c.dataStore.options, datastore.WithLogger(&datastore.DatabaseLogWrapper{Interface: c.logger}))
c.taskManager.options = append(c.taskManager.options, taskmanager.WithLogger(c.logger))
}
}
}
Expand Down
Loading

0 comments on commit 54ec506

Please sign in to comment.