forked from jsteenb2/mess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
56 lines (45 loc) · 1.19 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
package decorator
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
)
type commandLoggingDecorator[C any] struct {
base CommandHandler[C]
logger *logrus.Entry
}
func (d commandLoggingDecorator[C]) Handle(ctx context.Context, cmd C) (err error) {
handlerType := generateActionName(cmd)
logger := d.logger.WithFields(logrus.Fields{
"command": handlerType,
"command_body": fmt.Sprintf("%#v", cmd),
})
logger.Debug("Executing command")
defer func() {
if err == nil {
logger.Info("Command executed successfully")
} else {
logger.WithError(err).Error("Failed to execute command")
}
}()
return d.base.Handle(ctx, cmd)
}
type queryLoggingDecorator[C any, R any] struct {
base QueryHandler[C, R]
logger *logrus.Entry
}
func (d queryLoggingDecorator[C, R]) Handle(ctx context.Context, cmd C) (result R, err error) {
logger := d.logger.WithFields(logrus.Fields{
"query": generateActionName(cmd),
"query_body": fmt.Sprintf("%#v", cmd),
})
logger.Debug("Executing query")
defer func() {
if err == nil {
logger.Info("Query executed successfully")
} else {
logger.WithError(err).Error("Failed to execute query")
}
}()
return d.base.Handle(ctx, cmd)
}