/
logging.go
162 lines (141 loc) · 5.04 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package event
import (
"context"
"time"
cs "github.com/cloudtrust/common-service"
"github.com/cloudtrust/common-service/database"
"github.com/cloudtrust/common-service/log"
"github.com/cloudtrust/keycloak-bridge/api/event/fb"
)
// Logging middleware for the mux component.
type muxComponentLoggingMW struct {
logger log.Logger
next MuxComponent
}
// MakeMuxComponentLoggingMW makes a logging middleware for the mux component.
func MakeMuxComponentLoggingMW(log log.Logger) func(MuxComponent) MuxComponent {
return func(next MuxComponent) MuxComponent {
return &muxComponentLoggingMW{
logger: log,
next: next,
}
}
}
// muxComponentLoggingMW implements MuxComponent.
func (m *muxComponentLoggingMW) Event(ctx context.Context, eventType string, obj []byte) error {
defer func(begin time.Time) {
m.logger.Info(ctx, "unit", "Event", "type", eventType, "correlation_id", ctx.Value(cs.CtContextCorrelationID).(string), "took", time.Since(begin))
}(time.Now())
return m.next.Event(ctx, eventType, obj)
}
// Logging middleware for the event component.
type componentLoggingMW struct {
logger log.Logger
next Component
}
// MakeComponentLoggingMW makes a logging middleware for the event component.
func MakeComponentLoggingMW(log log.Logger) func(Component) Component {
return func(next Component) Component {
return &componentLoggingMW{
logger: log,
next: next,
}
}
}
// componentLoggingMW implements Component.
func (m *componentLoggingMW) Event(ctx context.Context, event *fb.Event) error {
defer func(begin time.Time) {
m.logger.Info(ctx, "unit", "Event", "correlation_id", ctx.Value(cs.CtContextCorrelationID).(string), "took", time.Since(begin))
}(time.Now())
return m.next.Event(ctx, event)
}
// Logging middleware for the admin event component.
type adminComponentLoggingMW struct {
logger log.Logger
next AdminComponent
}
// MakeAdminComponentLoggingMW makes a logging middleware for the admin event component.
func MakeAdminComponentLoggingMW(log log.Logger) func(AdminComponent) AdminComponent {
return func(next AdminComponent) AdminComponent {
return &adminComponentLoggingMW{
logger: log,
next: next,
}
}
}
// adminComponentLoggingMW implements AdminComponent.
func (m *adminComponentLoggingMW) AdminEvent(ctx context.Context, adminEvent *fb.AdminEvent) error {
defer func(begin time.Time) {
m.logger.Info(ctx, "unit", "AdminEvent", "correlation_id", ctx.Value(cs.CtContextCorrelationID).(string), "took", time.Since(begin))
}(time.Now())
return m.next.AdminEvent(ctx, adminEvent)
}
// Logging middleware for the console module.
type consoleModuleLoggingMW struct {
logger log.Logger
next ConsoleModule
}
// MakeConsoleModuleLoggingMW makes a logging middleware for the console module.
func MakeConsoleModuleLoggingMW(log log.Logger) func(ConsoleModule) ConsoleModule {
return func(next ConsoleModule) ConsoleModule {
return &consoleModuleLoggingMW{
logger: log,
next: next,
}
}
}
// consoleModuleLoggingMW implements ConsoleModule.
func (m *consoleModuleLoggingMW) Print(ctx context.Context, mp map[string]string) error {
defer func(begin time.Time) {
m.logger.Debug(ctx, "method", "Print", "args", mp, "took", time.Since(begin))
}(time.Now())
return m.next.Print(ctx, mp)
}
// Logging middleware for the statistic module.
type statisticModuleLoggingMW struct {
logger log.Logger
next StatisticModule
}
// MakeStatisticModuleLoggingMW makes a logging middleware for the statistic module.
func MakeStatisticModuleLoggingMW(log log.Logger) func(StatisticModule) StatisticModule {
return func(next StatisticModule) StatisticModule {
return &statisticModuleLoggingMW{
logger: log,
next: next,
}
}
}
// statisticModuleLoggingMW implements StatisticModule.
func (m *statisticModuleLoggingMW) Stats(ctx context.Context, mp map[string]string) error {
defer func(begin time.Time) {
m.logger.Debug(ctx, "method", "Stats", "args", mp, "took", time.Since(begin))
}(time.Now())
return m.next.Stats(ctx, mp)
}
// Logging middleware for the statistic module.
type eventsDBModuleLoggingMW struct {
logger log.Logger
next database.EventsDBModule
}
// MakeEventsDBModuleLoggingMW makes a logging middleware for the statistic module.
func MakeEventsDBModuleLoggingMW(log log.Logger) func(database.EventsDBModule) database.EventsDBModule {
return func(next database.EventsDBModule) database.EventsDBModule {
return &eventsDBModuleLoggingMW{
logger: log,
next: next,
}
}
}
// statisticModuleLoggingMW implements StatisticModule.
func (m *eventsDBModuleLoggingMW) Store(ctx context.Context, mp map[string]string) error {
defer func(begin time.Time) {
m.logger.Debug(ctx, "method", "Store", "args", mp, "took", time.Since(begin))
}(time.Now())
return m.next.Store(ctx, mp)
}
func (m *eventsDBModuleLoggingMW) ReportEvent(ctx context.Context, apiCall string, origin string, values ...string) error {
defer func(begin time.Time) {
m.logger.Info(ctx, "method", "ReportEvent", "args", apiCall, origin, values, "took", time.Since(begin))
}(time.Now())
return m.next.ReportEvent(ctx, apiCall, origin, values...)
}