From 5d0ab4531e87b8f8d4fcce1b3e61bba779c2a2ab Mon Sep 17 00:00:00 2001 From: Roger Johansson Date: Sun, 19 Nov 2023 20:41:31 +0100 Subject: [PATCH] Start using slog --- _examples/actor-helloworld/go.mod | 1 + _examples/actor-logging/go.mod | 40 +++++++++++++++++++++++ _examples/actor-logging/main.go | 53 +++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 _examples/actor-logging/go.mod create mode 100644 _examples/actor-logging/main.go diff --git a/_examples/actor-helloworld/go.mod b/_examples/actor-helloworld/go.mod index e195722f..080e7e40 100644 --- a/_examples/actor-helloworld/go.mod +++ b/_examples/actor-helloworld/go.mod @@ -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 diff --git a/_examples/actor-logging/go.mod b/_examples/actor-logging/go.mod new file mode 100644 index 00000000..f6bdb302 --- /dev/null +++ b/_examples/actor-logging/go.mod @@ -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 +) diff --git a/_examples/actor-logging/main.go b/_examples/actor-logging/main.go new file mode 100644 index 00000000..d49742f8 --- /dev/null +++ b/_examples/actor-logging/main.go @@ -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() +}