Skip to content

Commit d5524a5

Browse files
authored
Merge pull request #113 from 0x4b53/slog
Replace LogFunc with the standard slog.Logger
2 parents 571b9af + 9929978 commit d5524a5

File tree

14 files changed

+499
-293
lines changed

14 files changed

+499
-293
lines changed

README.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ can be changed by calling chainable methods.
8888

8989
```go
9090
server := NewServer("amqp://guest:guest@localhost:5672").
91-
WithDebugLogger(log.Printf).
92-
WithErrorLogger(log.Printf).
91+
WithLogger(logger).
9392
WithTLS(&tls.Config{})
9493
```
9594

@@ -115,10 +114,10 @@ request := NewRequest().
115114
116115
response, err := client.Send(request)
117116
if err != nil {
118-
log.Fatal(err.Error())
117+
slog.Error(err.Error())
119118
}
120119
121-
log.Print(string(response.Body))
120+
slog.Info(string(response.Body))
122121
```
123122
124123
The client will not connect while being created, instead this happens when the
@@ -139,8 +138,7 @@ Example of available methods for chaining.
139138

140139
```go
141140
client := NewClient("amqp://guest:guest@localhost:5672").
142-
WithDebugLogger(log.Printf).
143-
WithErrorLogger(log.Printf).
141+
WithLogger(logger).
144142
WithDialConfig(amqp.Config{}).
145143
WithTLS(&tls.Config{}).
146144
WithReplyToConsumerArgs(amqp.Table{}).
@@ -399,24 +397,30 @@ server.ListenAndServe()
399397

400398
## Logging
401399

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

406-
You can provide your own logging function for both error and debug on both the
407-
client and the server.
406+
The library will log using two different levels: `slog.LevelDebug` and
407+
`slog.LevelInfo`.
408+
409+
If you want to use something other than `slog` for logging, you can implement a
410+
`slog.Handler` wrapper that wraps your preferred logging implementation.
408411

409412
```go
410-
debugLogger := log.New(os.Stdout, "DEBUG - ", log.LstdFlags)
411-
errorLogger := log.New(os.Stdout, "ERROR - ", log.LstdFlags)
413+
logger := slog.New(
414+
slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
415+
Level: slog.LevelDebug,
416+
}),
417+
)
412418

413419
server := NewServer(url).
414-
WithErrorLogger(errorLogger.Printf).
415-
WithDebugLogger(debugLogger.Printf)
420+
WithLogger(logger)
416421

417422
client := NewClient(url).
418-
WithErrorLogger(errorLogger.Printf).
419-
WithDebugLogger(debugLogger.Printf)
423+
WithLogger(logger)
420424
```
421425

422426
This is perfect when using a logger which supports debugging as a separate

benchmark_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package amqprpc
22

33
import (
44
"context"
5-
"log"
65
"testing"
76
"time"
87

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

2120
confirmingClient := NewClient(testURL).
22-
WithTimeout(3 * time.Minute).
23-
WithErrorLogger(log.Printf)
21+
WithTimeout(3 * time.Minute)
2422

2523
defer confirmingClient.Stop()
2624

2725
fastClient := NewClient(testURL).
28-
WithErrorLogger(log.Printf).
2926
WithTimeout(3 * time.Minute).
3027
WithConfirmMode(false)
3128

0 commit comments

Comments
 (0)