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 66fc99c commit 5d0ab45
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions _examples/actor-helloworld/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/lithammer/shortuuid/v4 v4.0.0 // indirect
github.com/lmittmann/tint v1.0.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/orcaman/concurrent-map v1.0.0 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
Expand Down
40 changes: 40 additions & 0 deletions _examples/actor-logging/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module actorlogging

go 1.21

replace github.com/asynkron/protoactor-go => ../..

require (
github.com/asynkron/goconsole v0.0.0-20160504192649-bfa12eebf716
github.com/asynkron/protoactor-go v0.0.0-00010101000000-000000000000
)

require (
github.com/Workiva/go-datastructures v1.1.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/lithammer/shortuuid/v4 v4.0.0 // indirect
github.com/lmittmann/tint v1.0.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/orcaman/concurrent-map v1.0.0 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/twmb/murmur3 v1.1.6 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.39.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/sdk v1.16.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.39.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/sys v0.12.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
53 changes: 53 additions & 0 deletions _examples/actor-logging/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
console "github.com/asynkron/goconsole"
"github.com/asynkron/protoactor-go/actor"
"github.com/lmittmann/tint"
"log/slog"
"os"
"time"
)

type (
hello struct{ Who string }
helloActor struct{}
)

func (state *helloActor) Receive(context actor.Context) {
switch msg := context.Message().(type) {
case *hello:
context.Logger().Info("Hello ", slog.String("who", msg.Who))
}
}

func jsonLogging(system *actor.ActorSystem) *slog.Logger {
return slog.New(slog.NewJSONHandler(os.Stdout, nil)).
With("lib", "Proto.Actor").
With("system", system.ID)
}

func consoleLogging(system *actor.ActorSystem) *slog.Logger {
return slog.Default().
With("lib", "Proto.Actor").
With("system", system.ID)
}

func coloredConsoleLogging(system *actor.ActorSystem) *slog.Logger {
return slog.New(tint.NewHandler(os.Stdout, &tint.Options{
Level: slog.LevelDebug,
TimeFormat: time.Kitchen,
})).With("lib", "Proto.Actor").
With("system", system.ID)
}

func main() {

system := actor.NewActorSystem(actor.WithLoggerFactory(jsonLogging))

props := actor.PropsFromProducer(func() actor.Actor { return &helloActor{} })

pid := system.Root.Spawn(props)
system.Root.Send(pid, &hello{Who: "Roger"})
_, _ = console.ReadLine()
}

0 comments on commit 5d0ab45

Please sign in to comment.