-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogging_log_test.go
54 lines (41 loc) · 1.14 KB
/
logging_log_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//go:build !go1.21
package fauna_test
import (
"bytes"
"context"
"fmt"
"net/http"
"os"
"testing"
"github.com/fauna/fauna-go/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLogLogger(t *testing.T) {
t.Run("should be able to provide a custom logger", func(t *testing.T) {
buf := new(bytes.Buffer)
client := fauna.NewClient("secret", fauna.DefaultTimeouts(), fauna.WithLogger(CustomLogger{
Output: buf,
}), fauna.URL(fauna.EndpointLocal))
assert.NotNil(t, client)
query, queryErr := fauna.FQL(`42`, nil)
require.NoError(t, queryErr)
res, err := client.Query(query)
require.NoError(t, err)
var value int
err = res.Unmarshal(&value)
require.NoError(t, err)
require.Equal(t, 42, value)
assert.NotEmpty(t, buf)
})
}
type CustomLogger struct {
fauna.Logger
Output *bytes.Buffer
}
func (c CustomLogger) Info(msg string) {
_, _ = fmt.Fprint(os.Stdout, msg)
}
func (c CustomLogger) LogResponse(_ context.Context, requestBody []byte, res *http.Response) {
_, _ = fmt.Fprintf(c.Output, "URL: %s\nStatus: %s\nBody: %s\n", res.Request.URL.String(), res.Status, string(requestBody))
}