Skip to content

Commit

Permalink
feat(cloudslog): bridge log/slog with cloudzap
Browse files Browse the repository at this point in the history
A new empty package is added: cloudslog that is intended to contain
handler(s) and funtions related to the new std lib slog package.

This change also replaces the default log/slog logger to a one that
instead logs to our default zap logger. This is useful if you use libs
or sdks that do not rely on cloudrunner and/or zap.
  • Loading branch information
Edholm committed Oct 27, 2023
1 parent eece5d2 commit cbb25c3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cloudslog/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package cloudslog provides primitives for structured logging with the standard library log/slog package.
package cloudslog
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
go.opentelemetry.io/otel/sdk v1.19.0
go.opentelemetry.io/otel/sdk/metric v1.19.0
go.uber.org/zap v1.26.0
go.uber.org/zap/exp v0.2.0
golang.org/x/oauth2 v0.13.0
golang.org/x/sync v0.4.0
google.golang.org/api v0.147.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
go.uber.org/zap/exp v0.2.0 h1:FtGenNNeCATRB3CmB/yEUnjEFeJWpB/pMcy7e2bKPYs=
go.uber.org/zap/exp v0.2.0/go.mod h1:t0gqAIdh1MfKv9EwN/dLwfZnJxe9ITAZN78HEWPFWDQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand Down
14 changes: 14 additions & 0 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"runtime/debug"
Expand All @@ -21,6 +22,7 @@ import (
"go.einride.tech/cloudrunner/cloudzap"
"go.einride.tech/protobuf-sensitive/protosensitive"
"go.uber.org/zap"
"go.uber.org/zap/exp/zapslog"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -103,6 +105,8 @@ func Run(fn func(context.Context) error, options ...Option) (err error) {
if err != nil {
return fmt.Errorf("cloudrunner.Run: %w", err)
}
// Set the global default log/slog logger to write to our zap logger
slog.SetDefault(newSlogger(logger))
run.loggerMiddleware.Logger = logger
ctx = cloudzap.WithLogger(ctx, logger)
if err := cloudprofiler.Start(run.config.Profiler); err != nil {
Expand Down Expand Up @@ -196,3 +200,13 @@ func (b buildSettingsMarshaler) MarshalLogObject(encoder zapcore.ObjectEncoder)
}
return nil
}

// newSlogger returns a slog logger in which the underlying handler writes to the given zap logger.
// this func is kept here instead of in the cloudslog package to avoid having a api surface
// that encompasses zap in that package.
func newSlogger(zl *zap.Logger) *slog.Logger {
slogHandler := zapslog.NewHandler(zl.Core(), &zapslog.HandlerOptions{
AddSource: true, // same as zap's AddCaller
})
return slog.New(slogHandler)
}

0 comments on commit cbb25c3

Please sign in to comment.