-
Notifications
You must be signed in to change notification settings - Fork 3
/
logging.go
81 lines (74 loc) · 1.82 KB
/
logging.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package streammiddleware
import (
"context"
"time"
"github.com/arquivei/goduck"
"github.com/arquivei/foundationkit/errors"
"github.com/rs/zerolog/log"
)
type streamLogging struct {
next goduck.Stream
}
// WrapWithLogging wraps a @next with a logging that logs when methods are
// called.
func WrapWithLogging(next goduck.Stream) goduck.Stream {
return streamLogging{
next: next,
}
}
func (s streamLogging) Next(ctx context.Context) (response goduck.RawMessage, err error) {
const op = errors.Op("streammiddleware.streamLogging.Next")
defer func(begin time.Time) {
took := time.Since(begin)
if err != nil {
logger := log.Error()
if ctx.Err() != nil {
logger = log.Debug()
}
logger.
Dur("took", took).
Err(errors.E(op, err)).
Msg("Error fetching next message")
} else {
log.Debug().
Dur("took", took).
Int("size", len(response.Bytes())).
Msg("Successfully fetched next message")
}
}(time.Now())
return s.next.Next(ctx)
}
func (s streamLogging) Done(ctx context.Context) (err error) {
const op = errors.Op("streammiddleware.streamLogging.Done")
defer func(begin time.Time) {
took := time.Since(begin)
if err != nil {
log.Error().
Dur("took", took).
Err(errors.E(op, err)).
Msg("Error marking messages as done")
} else {
log.Debug().
Dur("took", took).
Msg("Successfully acked messages")
}
}(time.Now())
return s.next.Done(ctx)
}
func (s streamLogging) Close() (err error) {
const op = errors.Op("streammiddleware.streamLogging.Close")
defer func(begin time.Time) {
took := time.Since(begin)
if err != nil {
log.Error().
Dur("took", took).
Err(errors.E(op, err)).
Msg("Error closing the stream")
} else {
log.Debug().
Dur("took", took).
Msg("Successfully closed the stream")
}
}(time.Now())
return s.next.Close()
}