@@ -3,34 +3,86 @@ package aurestlogging
3
3
import (
4
4
"context"
5
5
aulogging "github.com/StephanHCB/go-autumn-logging"
6
+ auloggingapi "github.com/StephanHCB/go-autumn-logging/api"
6
7
aurestclientapi "github.com/StephanHCB/go-autumn-restclient/api"
7
8
aurestnontripping "github.com/StephanHCB/go-autumn-restclient/implementation/errors/nontrippingerror"
8
9
"time"
9
10
)
10
11
12
+ // RequestLoggingOptions allows overriding the log functions used.
13
+ //
14
+ // This allows easily changing the log level when setting up request logging.
15
+ //
16
+ // important: do not cache the LeveledLoggingImplementation, create one each time, or some loggers may use
17
+ // cached values.
18
+ type RequestLoggingOptions struct {
19
+ BeforeRequest func (ctx context.Context ) auloggingapi.LeveledLoggingImplementation
20
+ Success func (ctx context.Context ) auloggingapi.LeveledLoggingImplementation
21
+ Failure func (ctx context.Context ) auloggingapi.LeveledLoggingImplementation
22
+ }
23
+
24
+ func Debug (ctx context.Context ) auloggingapi.LeveledLoggingImplementation {
25
+ return aulogging .Logger .Ctx (ctx ).Debug ()
26
+ }
27
+
28
+ func Info (ctx context.Context ) auloggingapi.LeveledLoggingImplementation {
29
+ return aulogging .Logger .Ctx (ctx ).Info ()
30
+ }
31
+
32
+ func Warn (ctx context.Context ) auloggingapi.LeveledLoggingImplementation {
33
+ return aulogging .Logger .Ctx (ctx ).Warn ()
34
+ }
35
+
11
36
type RequestLoggingImpl struct {
12
37
Wrapped aurestclientapi.Client
38
+ Options RequestLoggingOptions
39
+ }
40
+
41
+ func NewWithOptions (wrapped aurestclientapi.Client , opts RequestLoggingOptions ) aurestclientapi.Client {
42
+ instance := & RequestLoggingImpl {
43
+ Wrapped : wrapped ,
44
+ Options : RequestLoggingOptions {
45
+ BeforeRequest : Debug ,
46
+ Success : Info ,
47
+ Failure : Warn ,
48
+ },
49
+ }
50
+ if opts .BeforeRequest != nil {
51
+ instance .Options .BeforeRequest = opts .BeforeRequest
52
+ }
53
+ if opts .Success != nil {
54
+ instance .Options .Success = opts .Success
55
+ }
56
+ if opts .Failure != nil {
57
+ instance .Options .Failure = opts .Failure
58
+ }
59
+ return instance
13
60
}
14
61
15
62
func New (wrapped aurestclientapi.Client ) aurestclientapi.Client {
16
63
return & RequestLoggingImpl {
17
64
Wrapped : wrapped ,
65
+ Options : RequestLoggingOptions {
66
+ BeforeRequest : Debug ,
67
+ Success : Info ,
68
+ Failure : Warn ,
69
+ },
18
70
}
19
71
}
20
72
21
73
func (c * RequestLoggingImpl ) Perform (ctx context.Context , method string , requestUrl string , requestBody interface {}, response * aurestclientapi.ParsedResponse ) error {
22
- aulogging . Logger . Ctx (ctx ). Debug ( ).Printf ("downstream %s %s..." , method , requestUrl )
74
+ c . Options . BeforeRequest (ctx ).Printf ("downstream %s %s..." , method , requestUrl )
23
75
before := time .Now ()
24
76
err := c .Wrapped .Perform (ctx , method , requestUrl , requestBody , response )
25
77
millis := time .Now ().Sub (before ).Milliseconds ()
26
78
if err != nil {
27
79
if aurestnontripping .Is (err ) {
28
- aulogging . Logger . Ctx (ctx ). Warn ( ).WithErr (err ).Printf ("downstream %s %s -> %d FAILED (%d ms) (nontripping)" , method , requestUrl , response .Status , millis )
80
+ c . Options . Failure (ctx ).WithErr (err ).Printf ("downstream %s %s -> %d FAILED (%d ms) (nontripping)" , method , requestUrl , response .Status , millis )
29
81
} else {
30
- aulogging . Logger . Ctx (ctx ). Warn ( ).WithErr (err ).Printf ("downstream %s %s -> %d FAILED (%d ms)" , method , requestUrl , response .Status , millis )
82
+ c . Options . Failure (ctx ).WithErr (err ).Printf ("downstream %s %s -> %d FAILED (%d ms)" , method , requestUrl , response .Status , millis )
31
83
}
32
84
} else {
33
- aulogging . Logger . Ctx (ctx ). Info ( ).Printf ("downstream %s %s -> %d OK (%d ms)" , method , requestUrl , response .Status , millis )
85
+ c . Options . Success (ctx ).Printf ("downstream %s %s -> %d OK (%d ms)" , method , requestUrl , response .Status , millis )
34
86
}
35
87
return err
36
88
}
0 commit comments