Skip to content

Commit

Permalink
Start using slog
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeralsing committed Nov 19, 2023
1 parent 9475138 commit d1954a5
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 69 deletions.
5 changes: 5 additions & 0 deletions actor/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actor

import (
"fmt"
"log/slog"
"time"

"github.com/asynkron/protoactor-go/ctxext"
Expand All @@ -21,6 +22,10 @@ type mockContext struct {
mock.Mock
}

func (m *mockContext) Logger() *slog.Logger {
return slog.Default()
}

//
// Interface: Context
//
Expand Down
9 changes: 1 addition & 8 deletions actor/future_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"
"time"

"github.com/asynkron/protoactor-go/log"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -73,7 +72,7 @@ func TestFuture_PipeTo_TimeoutSendsError(t *testing.T) {
}

func TestNewFuture_TimeoutNoRace(t *testing.T) {
plog.SetLevel(log.OffLevel)

future := NewFuture(system, 1*time.Microsecond)
a := rootContext.Spawn(PropsFromFunc(func(context Context) {
switch context.Message().(type) {
Expand All @@ -94,8 +93,6 @@ func assertFutureSuccess(future *Future, t *testing.T) interface{} {
func TestFuture_Result_DeadLetterResponse(t *testing.T) {
a := assert.New(t)

plog.SetLevel(log.OffLevel)

future := NewFuture(system, 1*time.Second)
rootContext.Send(future.PID(), &DeadLetterResponse{})
resp, err := future.Result()
Expand All @@ -106,8 +103,6 @@ func TestFuture_Result_DeadLetterResponse(t *testing.T) {
func TestFuture_Result_Timeout(t *testing.T) {
a := assert.New(t)

plog.SetLevel(log.OffLevel)

future := NewFuture(system, 1*time.Second)
resp, err := future.Result()
a.Equal(ErrTimeout, err)
Expand All @@ -117,8 +112,6 @@ func TestFuture_Result_Timeout(t *testing.T) {
func TestFuture_Result_Success(t *testing.T) {
a := assert.New(t)

plog.SetLevel(log.OffLevel)

future := NewFuture(system, 1*time.Second)
rootContext.Send(future.PID(), EchoResponse{})
resp := assertFutureSuccess(future, t)
Expand Down
4 changes: 0 additions & 4 deletions actor/middleware/opentracing/logger.go
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
package opentracing

import "github.com/asynkron/protoactor-go/log"

var logger = log.New(log.ErrorLevel, "[TRACING]")
1 change: 0 additions & 1 deletion actor/pid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ func (sl *ShortLivingActor) Receive(Context) {
}

func TestStopFuture(t *testing.T) {
plog.Debug("hello world")

ID := "UniqueID"
{
Expand Down
22 changes: 0 additions & 22 deletions cluster/cluster_config_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
package cluster

import (
"fmt"
"log/slog"
"time"

"github.com/asynkron/protoactor-go/actor"
Expand All @@ -24,23 +22,3 @@ type ClusterContextConfig struct {
RetryAction func(int) int
requestLogThrottle actor.ShouldThrottle
}

// NewDefaultClusterContextConfig creates a mew ClusterContextConfig with default
// values and returns a pointer to its memory address
func NewDefaultClusterContextConfig() *ClusterContextConfig {
config := ClusterContextConfig{
ActorRequestTimeout: defaultActorRequestTimeout,
RequestsLogThrottlePeriod: defaultRequestsLogThrottlePeriod,
MaxNumberOfEventsInRequestLogThrottledPeriod: defaultMaxNumberOfEvetsInRequestLogThrottledPeriod,
RetryAction: defaultRetryAction,
//TODO: fix this
requestLogThrottle: actor.NewThrottleWithLogger(nil,
int32(defaultMaxNumberOfEvetsInRequestLogThrottledPeriod),
defaultRequestsLogThrottlePeriod,
func(logger *slog.Logger, i int32) {
logger.Info(fmt.Sprintf("Throttled %d Request logs", i))
},
),
}
return &config
}
10 changes: 0 additions & 10 deletions cluster/clusterproviders/etcd/log.go
Original file line number Diff line number Diff line change
@@ -1,11 +1 @@
package etcd

import "github.com/asynkron/protoactor-go/log"

var plog = log.New(log.DefaultLevel, "[CLUSTER] [ETCD]")

// SetLogLevel sets the log level for the logger
// SetLogLevel is safe to be called concurrently
func SetLogLevel(level log.Level) {
plog.SetLevel(level)
}
3 changes: 1 addition & 2 deletions cluster/gossip_actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package cluster

import (
"github.com/opentracing/opentracing-go/log"
"log/slog"
"time"

Expand Down Expand Up @@ -35,7 +34,7 @@ func NewGossipActor(requestTimeout time.Duration, myID string, getBlockedMembers
}

gossipActor.throttler = actor.NewThrottleWithLogger(logger, 3, 60*time.Second, func(logger *slog.Logger, counter int32) {
logger.Debug("[Gossip] Sending GossipRequest", log.Int("throttled", int(counter)))
logger.Debug("[Gossip] Sending GossipRequest", slog.Int("throttled", int(counter)))
})

return &gossipActor
Expand Down
13 changes: 0 additions & 13 deletions cluster/identitylookup/disthash/log.go
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
package disthash

import (
"github.com/asynkron/protoactor-go/log"
)

var plog = log.New(log.DefaultLevel, "[DISTHASH]")

// SetLogLevel sets the log level for the logger.
//
// SetLogLevel is safe to call concurrently
func SetLogLevel(level log.Level) {
plog.SetLevel(level)
}
13 changes: 7 additions & 6 deletions cluster/informer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cluster

import (
"fmt"
"log/slog"
"sync"
"testing"

Expand All @@ -20,7 +21,7 @@ func TestInformer_SetState(t *testing.T) {
ActorStatistics: &ActorStatistics{},
}

i := newInformer("member1", a, 3, 3)
i := newInformer("member1", a, 3, 3, slog.Default())
i.SetState("heartbeat", s)
}

Expand All @@ -35,7 +36,7 @@ func TestInformer_GetState(t *testing.T) {
ActorStatistics: &ActorStatistics{},
}

i := newInformer("member1", a, 3, 3)
i := newInformer("member1", a, 3, 3, slog.Default())
i.SetState("heartbeat", s)

m := i.GetState("heartbeat")
Expand Down Expand Up @@ -65,7 +66,7 @@ func TestInformer_ReceiveState(t *testing.T) {
}
dummyValue, _ := anypb.New(s)

i := newInformer("member1", a, 3, 3)
i := newInformer("member1", a, 3, 3, slog.Default())
i.SetState("heartbeat", s)

remoteState := &GossipState{
Expand Down Expand Up @@ -134,7 +135,7 @@ func TestInformer_SendState(t *testing.T) {
ActorStatistics: &ActorStatistics{},
}

i := newInformer("member1", a, 3, 3)
i := newInformer("member1", a, 3, 3, slog.Default())
i.SetState("heartbeat", s)
// the cluster sees two nodes. itself and member2
i.UpdateClusterTopology(&ClusterTopology{
Expand Down Expand Up @@ -167,7 +168,7 @@ func TestInformer_UpdateClusterTopology(t *testing.T) {
s := &MemberHeartbeat{
ActorStatistics: &ActorStatistics{},
}
i := newInformer("member1", a, 3, 3)
i := newInformer("member1", a, 3, 3, slog.Default())
i.SetState("heartbeat", s)
// the cluster sees two nodes. itself and member2
i.UpdateClusterTopology(&ClusterTopology{
Expand Down Expand Up @@ -199,7 +200,7 @@ func TestInformer_GetMemberStateDelta(t *testing.T) {
ActorStatistics: &ActorStatistics{},
}

i := newInformer("member1", a, 3, 3)
i := newInformer("member1", a, 3, 3, slog.Default())
i.SetState("heartbeat", s)

m := i.GetMemberStateDelta("member1")
Expand Down
10 changes: 10 additions & 0 deletions cluster/pubsub_producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ type mockPublisher struct {
publish func(*PubSubBatch) (*PublishResponse, error)
}

func (m *mockPublisher) Cluster() *Cluster {
//TODO implement me
panic("implement me")
}

func newMockPublisher(publish func(*PubSubBatch) (*PublishResponse, error)) *mockPublisher {
return &mockPublisher{publish: publish}
}
Expand All @@ -316,6 +321,11 @@ type optionalFailureMockPublisher struct {
shouldFail bool
}

func (o *optionalFailureMockPublisher) Cluster() *Cluster {
//TODO implement me
panic("implement me")
}

// newOptionalFailureMockPublisher creates a mock publisher that can be configured to fail or not
func newOptionalFailureMockPublisher(shouldFail bool) *optionalFailureMockPublisher {
return &optionalFailureMockPublisher{shouldFail: shouldFail}
Expand Down
4 changes: 1 addition & 3 deletions cluster/rendezvous_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"fmt"
"runtime"
"testing"

"github.com/asynkron/protoactor-go/log"
)

func Benchmark_Rendezvous_Get(b *testing.B) {
SetLogLevel(log.ErrorLevel)

for _, v := range []int{1, 2, 3, 5, 10, 100, 1000, 2000} {
members := newMembersForTest(v)
ms := newDefaultMemberStrategy(nil, "kind").(*simpleMemberStrategy)
Expand Down

0 comments on commit d1954a5

Please sign in to comment.