-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogrus.go
140 lines (136 loc) · 4.02 KB
/
logrus.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
// Package fxlogrus provides a logger for [uber-go/fx](https://github.com/uber-go/fx) based
// on [sirupsen/logrus](https://github.com/sirupsen/logrus). All non-errors are logged as debug to keep
// the logs quiet, errors are still logged as errors.
package fxlogrus
import (
"strings"
"github.com/sirupsen/logrus"
"go.uber.org/fx/fxevent"
)
// LogrusLogger log formatter for the fx application container
type LogrusLogger struct {
Logger *logrus.Logger
}
// LogEvent handles a log event for fx application container
func (l *LogrusLogger) LogEvent(event fxevent.Event) {
switch e := event.(type) {
case *fxevent.OnStartExecuting:
l.Logger.WithFields(logrus.Fields{
"callee": e.FunctionName,
"caller": e.CallerName,
}).Debug("on start hook executing")
case *fxevent.OnStartExecuted:
if e.Err != nil {
l.Logger.WithFields(logrus.Fields{
"callee": e.FunctionName,
"caller": e.CallerName,
}).Errorf("on start hook failed: %v", e.Err)
} else {
l.Logger.WithFields(logrus.Fields{
"callee": e.FunctionName,
"caller": e.CallerName,
"runtime": e.Runtime.String(),
}).Debug("on start hook executed")
}
case *fxevent.OnStopExecuting:
l.Logger.WithFields(logrus.Fields{
"callee": e.FunctionName,
"caller": e.CallerName,
}).Debug("on stop hook executing")
case *fxevent.OnStopExecuted:
if e.Err != nil {
l.Logger.WithFields(logrus.Fields{
"callee": e.FunctionName,
"caller": e.CallerName,
}).Errorf("on stop hook failed: %v", e.Err)
} else {
l.Logger.WithFields(logrus.Fields{
"callee": e.FunctionName,
"caller": e.CallerName,
"runtime": e.Runtime.String(),
}).Debug("on stop hook executed")
}
case *fxevent.Supplied:
l.Logger.WithFields(logrus.Fields{
"type": e.TypeName,
"module": e.ModuleName,
}).Debugf("supplied: %v", e.Err)
case *fxevent.Provided:
for _, rtype := range e.OutputTypeNames {
l.Logger.WithFields(logrus.Fields{
"constructor": e.ConstructorName,
"module": e.ModuleName,
"type": rtype,
}).Debug("provided")
}
if e.Err != nil {
l.Logger.WithFields(logrus.Fields{
"module": e.ModuleName,
}).Errorf("error encountered while applying options: %v", e.Err)
}
case *fxevent.Replaced:
for _, rtype := range e.OutputTypeNames {
l.Logger.WithFields(logrus.Fields{
"module": e.ModuleName,
"type": rtype,
}).Debug("replaced")
}
if e.Err != nil {
l.Logger.WithFields(logrus.Fields{
"module": e.ModuleName,
}).Errorf("error encountered while replacing: %v", e.Err)
}
case *fxevent.Decorated:
for _, rtype := range e.OutputTypeNames {
l.Logger.WithFields(logrus.Fields{
"module": e.ModuleName,
"type": rtype,
}).Debug("decorated")
}
if e.Err != nil {
l.Logger.WithFields(logrus.Fields{
"module": e.ModuleName,
}).Errorf("error encountered while applying options: %v", e.Err)
}
case *fxevent.Invoking:
// Do not log stack as it will make logs hard to read.
l.Logger.WithFields(logrus.Fields{
"function": e.FunctionName,
"module": e.ModuleName,
}).Debug("invoking")
case *fxevent.Invoked:
if e.Err != nil {
l.Logger.WithFields(logrus.Fields{
"stack": e.Trace,
"function": e.FunctionName,
"module": e.ModuleName,
}).Errorf("invoke failed: %v", e.Err)
}
case *fxevent.Stopping:
l.Logger.Debugf("received signal: %s", strings.ToUpper(e.Signal.String()))
case *fxevent.Stopped:
if e.Err != nil {
l.Logger.Errorf("received signal: %v", e.Err)
}
case *fxevent.RollingBack:
l.Logger.Errorf("start failed, rolling back: %v", e.StartErr)
case *fxevent.RolledBack:
if e.Err != nil {
l.Logger.Errorf("rollback failed: %v", e.Err)
}
case *fxevent.Started:
if e.Err != nil {
l.Logger.Errorf("start failed: %v", e.Err)
} else {
l.Logger.Debug("started")
}
case *fxevent.LoggerInitialized:
if e.Err != nil {
l.Logger.Errorf("custom logger initialization failed: %v", e.Err)
} else {
l.Logger.WithFields(logrus.Fields{
"function": e.ConstructorName,
}).Debug("initialized custom fxevent.Logger")
}
}
}