Skip to content

Commit

Permalink
Update readme for slog (#1222)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinn-With-Two-Ns committed Sep 1, 2023
1 parent 7485282 commit 9dc99ed
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,35 @@ See [samples](https://github.com/temporalio/samples-go) to get started.
Documentation is available [here](https://docs.temporal.io).
You can also find the API documentation [here](https://pkg.go.dev/go.temporal.io/sdk).

## Using a custom logger
## Using slog

If using Go version 1.21+ the Go SDK provides built in integration with the standard [slog](https://pkg.go.dev/log) package.

Although the Go SDK does not support most third-party logging solutions natively, [our friends at Banzai Cloud](https://github.com/sagikazarmark) built the [adapter package logur](https://github.com/logur/logur) which makes it possible to use third party loggers with minimal overhead. Here is an example of using logur to support [Logrus](https://github.com/sirupsen/logrus):

```go
package main
import (
"github.com/sirupsen/logrus"
"log/slog"
"os"

"go.temporal.io/sdk/client"
logrusadapter "logur.dev/adapter/logrus"
"logur.dev/logur"
"go.temporal.io/sdk/log"
"go.temporal.io/sdk/worker"
)

func main() {
// feed this logger into Temporal
logger := logur.LoggerToKV(logrusadapter.New(logrus.New()))
clientOptions := client.Options{
Logger: logger,
Logger: log.NewStructuredLogger(
slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
Level: slog.LevelDebug,
}))),
}
temporalClient, err := client.Dial(clientOptions)
// ...
}
```

Most of the popular logging solutions have existing adapters in logur. If you're curious about which adapters are available, here is a helpful [link](https://github.com/logur?q=adapter-).

## Workflow determinism checker

See [contrib/tools/workflowcheck](contrib/tools/workflowcheck) for a tool to detect non-determinism in Workflow Definitions.
Expand Down
6 changes: 5 additions & 1 deletion internal/log/replay_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import (
"go.temporal.io/sdk/log"
)

var _ log.Logger = (*ReplayLogger)(nil)
var _ log.WithLogger = (*ReplayLogger)(nil)
var _ log.WithSkipCallers = (*ReplayLogger)(nil)

// ReplayLogger is Logger implementation that is aware of replay.
type ReplayLogger struct {
logger log.Logger
Expand Down Expand Up @@ -81,7 +85,7 @@ func (l *ReplayLogger) With(keyvals ...interface{}) log.Logger {
return NewReplayLogger(log.With(l.logger, keyvals...), l.isReplay, l.enableLoggingInReplay)
}

func (l *ReplayLogger) AddCallerSkip(depth int) log.Logger {
func (l *ReplayLogger) WithCallerSkip(depth int) log.Logger {
if sl, ok := l.logger.(log.WithSkipCallers); ok {
return NewReplayLogger(sl.WithCallerSkip(depth), l.isReplay, l.enableLoggingInReplay)
}
Expand Down
3 changes: 3 additions & 0 deletions log/with_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func Skip(logger Logger, depth int) Logger {
return logger
}

var _ Logger = (*withLogger)(nil)
var _ WithSkipCallers = (*withLogger)(nil)

type withLogger struct {
logger Logger
keyvals []interface{}
Expand Down

0 comments on commit 9dc99ed

Please sign in to comment.