Skip to content

Commit

Permalink
Merge pull request #113 from 0x4b53/slog
Browse files Browse the repository at this point in the history
Replace LogFunc with the standard slog.Logger
  • Loading branch information
akarl authored Nov 14, 2024
2 parents 571b9af + 9929978 commit d5524a5
Show file tree
Hide file tree
Showing 14 changed files with 499 additions and 293 deletions.
38 changes: 21 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ can be changed by calling chainable methods.

```go
server := NewServer("amqp://guest:guest@localhost:5672").
WithDebugLogger(log.Printf).
WithErrorLogger(log.Printf).
WithLogger(logger).
WithTLS(&tls.Config{})
```

Expand All @@ -115,10 +114,10 @@ request := NewRequest().
response, err := client.Send(request)
if err != nil {
log.Fatal(err.Error())
slog.Error(err.Error())
}
log.Print(string(response.Body))
slog.Info(string(response.Body))
```
The client will not connect while being created, instead this happens when the
Expand All @@ -139,8 +138,7 @@ Example of available methods for chaining.

```go
client := NewClient("amqp://guest:guest@localhost:5672").
WithDebugLogger(log.Printf).
WithErrorLogger(log.Printf).
WithLogger(logger).
WithDialConfig(amqp.Config{}).
WithTLS(&tls.Config{}).
WithReplyToConsumerArgs(amqp.Table{}).
Expand Down Expand Up @@ -399,24 +397,30 @@ server.ListenAndServe()

## Logging

You can specify two optional loggers for debugging and errors or unexpected
behaviour. By default only error logging is turned on and is logged via the log
package's standard logging.
You can specify your own `slog.Logger` instance. By default amqp-rpc will log
errors using the logger from `slog.Default()`. Some logs will contain data
contained in a `amqp.Delivery` or `amqp.Publishing`, including any headers. If
you want to avoid logging some of the fields you can use an `slog.Handler` to
filter out the fields you don't want to log.

You can provide your own logging function for both error and debug on both the
client and the server.
The library will log using two different levels: `slog.LevelDebug` and
`slog.LevelInfo`.

If you want to use something other than `slog` for logging, you can implement a
`slog.Handler` wrapper that wraps your preferred logging implementation.

```go
debugLogger := log.New(os.Stdout, "DEBUG - ", log.LstdFlags)
errorLogger := log.New(os.Stdout, "ERROR - ", log.LstdFlags)
logger := slog.New(
slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
}),
)

server := NewServer(url).
WithErrorLogger(errorLogger.Printf).
WithDebugLogger(debugLogger.Printf)
WithLogger(logger)

client := NewClient(url).
WithErrorLogger(errorLogger.Printf).
WithDebugLogger(debugLogger.Printf)
WithLogger(logger)
```

This is perfect when using a logger which supports debugging as a separate
Expand Down
5 changes: 1 addition & 4 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package amqprpc

import (
"context"
"log"
"testing"
"time"

Expand All @@ -19,13 +18,11 @@ func Benchmark(b *testing.B) {
time.Sleep(1 * time.Second)

confirmingClient := NewClient(testURL).
WithTimeout(3 * time.Minute).
WithErrorLogger(log.Printf)
WithTimeout(3 * time.Minute)

defer confirmingClient.Stop()

fastClient := NewClient(testURL).
WithErrorLogger(log.Printf).
WithTimeout(3 * time.Minute).
WithConfirmMode(false)

Expand Down
Loading

0 comments on commit d5524a5

Please sign in to comment.