-
Notifications
You must be signed in to change notification settings - Fork 1
/
fxevent.go
131 lines (125 loc) · 3.51 KB
/
fxevent.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
package logger
import (
"context"
"strings"
"github.com/go-bricks/bricks/v2/interfaces/cfg"
confkeys "github.com/go-bricks/bricks/v2/interfaces/cfg/keys"
"github.com/go-bricks/bricks/v2/interfaces/log"
"go.uber.org/fx/fxevent"
)
// CreateFxEventLogger is a constructor to create fxevent.Logger
// This one is used by fx itself to output its events
func CreateFxEventLogger(logger log.Logger, cfg cfg.Config) fxevent.Logger {
level := log.InfoLevel
if cfg.Get(confkeys.LogStartStop).IsSet() {
level = log.ParseLevel(cfg.Get(confkeys.LogStartStop).String())
}
return &logWrapper{Logger: logger, startStopLogLevel: level}
}
type logWrapper struct {
log.Logger
startStopLogLevel log.Level
}
func (zw *logWrapper) LogEvent(event fxevent.Event) {
switch e := event.(type) {
case *fxevent.OnStartExecuting:
zw.
WithField("callee", e.FunctionName).
WithField("caller", e.CallerName).
Debug(context.TODO(), "OnStart hook executing")
case *fxevent.OnStartExecuted:
logger := zw.
WithError(e.Err).
WithField("callee", e.FunctionName).
WithField("caller", e.CallerName)
if e.Err != nil {
logger.Error(context.TODO(), "OnStart hook failed")
} else {
logger.
WithField("runtime", e.Runtime.String()).
Debug(context.TODO(), "OnStart hook executed")
}
case *fxevent.OnStopExecuting:
zw.
WithField("callee", e.FunctionName).
WithField("caller", e.CallerName).
Debug(context.TODO(), "OnStop hook executing")
case *fxevent.OnStopExecuted:
logger := zw.
WithError(e.Err).
WithField("callee", e.FunctionName).
WithField("caller", e.CallerName)
if e.Err != nil {
logger.Error(context.TODO(), "OnStop hook failed")
} else {
logger.
WithField("runtime", e.Runtime.String()).
Debug(context.TODO(), "OnStop hook executed")
}
case *fxevent.Supplied:
zw.
WithField("type", e.TypeName).
WithError(e.Err).
Debug(context.TODO(), "supplied")
case *fxevent.Provided:
for _, rtype := range e.OutputTypeNames {
zw.
WithField("constructor", e.ConstructorName).
WithField("type", rtype).
Debug(context.TODO(), "provided")
}
if e.Err != nil {
zw.
WithError(e.Err).
Error(context.TODO(), "error encountered while applying options")
}
case *fxevent.Invoking:
// Do nothing. Will log on Invoked.
case *fxevent.Invoked:
logger := zw.
WithError(e.Err).
WithField("stack", e.Trace).
WithField("function", e.FunctionName)
if e.Err != nil {
logger.Error(context.TODO(), "invoke failed")
} else {
logger.Debug(context.TODO(), "invoked")
}
case *fxevent.Stopping:
zw.
WithField("signal", strings.ToUpper(e.Signal.String())).
Custom(context.TODO(), zw.startStopLogLevel, 0, "received termination signal")
case *fxevent.Stopped:
if e.Err != nil {
zw.WithError(e.Err).Error(context.TODO(), "stop failed")
}
case *fxevent.RollingBack:
zw.
WithError(e.StartErr).
Error(context.TODO(), "start failed, rolling back")
case *fxevent.RolledBack:
if e.Err != nil {
zw.
WithError(e.Err).
Error(context.TODO(), "start failed, rolling back")
}
case *fxevent.Started:
if e.Err != nil {
zw.
WithError(e.Err).
Error(context.TODO(), "start failed")
} else {
zw.Custom(context.TODO(), zw.startStopLogLevel, 0, "service started")
}
case *fxevent.LoggerInitialized:
if e.Err != nil {
zw.
WithError(e.Err).
Error(context.TODO(), "custom logger initialization failed")
} else {
zw.
WithField("function", e.ConstructorName).
Debug(context.TODO(), "initialized custom fxevent.Logger")
}
}
}