-
Notifications
You must be signed in to change notification settings - Fork 11
/
server_logger.go
186 lines (151 loc) · 4.04 KB
/
server_logger.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package apiserver
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"code.cloudfoundry.org/lager"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
)
// Logger wraps a lager.Logger in the echo.Logger interface
type Logger struct {
lvl log.Lvl
lager lager.Logger
action string
}
func (l *Logger) Debug(i ...interface{}) {
l.lager.Debug(l.action, lager.Data{
"detail": fmt.Sprint(i...),
})
}
func (l *Logger) Debugf(format string, i ...interface{}) {
l.lager.Debug(l.action, lager.Data{
"detail": fmt.Sprintf(format, i...),
})
}
func (l *Logger) Debugj(j log.JSON) {
l.lager.Debug(l.action, lager.Data(j))
}
func (l *Logger) Warn(i ...interface{}) {
l.lager.Info(l.action, lager.Data{
"detail": fmt.Sprint(i...),
})
}
func (l *Logger) Warnf(format string, i ...interface{}) {
l.lager.Info(l.action, lager.Data{
"detail": fmt.Sprintf(format, i...),
})
}
func (l *Logger) Warnj(j log.JSON) {
l.lager.Info(l.action, lager.Data(j))
}
func (l *Logger) Error(i ...interface{}) {
l.lager.Error(l.action, errors.New(fmt.Sprint(i...)), lager.Data{})
}
func (l *Logger) Errorf(format string, i ...interface{}) {
l.lager.Error(l.action, fmt.Errorf(format, i...), lager.Data{})
}
func (l *Logger) Errorj(j log.JSON) {
l.lager.Error(l.action, errors.New("error"), lager.Data(j))
}
func (l *Logger) Fatal(i ...interface{}) {
l.lager.Fatal(l.action, errors.New(fmt.Sprint(i...)), lager.Data{})
}
func (l *Logger) Fatalf(format string, i ...interface{}) {
l.lager.Fatal(l.action, fmt.Errorf(format, i...), lager.Data{})
}
func (l *Logger) Fatalj(j log.JSON) {
l.lager.Fatal(l.action, errors.New("fatal"), lager.Data(j))
}
func (l *Logger) Info(i ...interface{}) {
l.lager.Info(l.action, lager.Data{
"detail": fmt.Sprint(i...),
})
}
func (l *Logger) Infof(format string, i ...interface{}) {
l.lager.Info(l.action, lager.Data{
"detail": fmt.Sprintf(format, i...),
})
}
func (l *Logger) Infoj(j log.JSON) {
l.lager.Info(l.action, lager.Data(j))
}
func (l *Logger) Print(i ...interface{}) {
l.lager.Info(l.action, lager.Data{
"detail": fmt.Sprint(i...),
})
}
func (l *Logger) Printf(format string, i ...interface{}) {
l.lager.Info(l.action, lager.Data{
"detail": fmt.Sprintf(format, i...),
})
}
func (l *Logger) Printj(j log.JSON) {
l.lager.Info(l.action, lager.Data(j))
}
func (l *Logger) Panic(i ...interface{}) {
l.lager.Error(l.action, errors.New("panic"), lager.Data{
"detail": fmt.Sprint(i...),
})
panic(fmt.Sprint(i...))
}
func (l *Logger) Panicf(format string, i ...interface{}) {
l.lager.Error(l.action, errors.New("panic"), lager.Data{
"detail": fmt.Sprintf(format, i...),
})
panic(fmt.Sprintf(format, i...))
}
func (l *Logger) Panicj(j log.JSON) {
l.lager.Error(l.action, errors.New("panic"), lager.Data(j))
panic(fmt.Sprintf("%v", j))
}
func (l *Logger) Level() log.Lvl {
return l.lvl
}
func (l *Logger) SetLevel(newLvl log.Lvl) {
l.lvl = newLvl
}
func (l *Logger) Prefix() string {
panic("not implemented")
}
func (l *Logger) SetPrefix(p string) {
panic("not implemented")
}
func (l *Logger) Output() io.Writer {
return os.Stdout
}
func (l *Logger) SetOutput(w io.Writer) {
panic("not implemented")
}
func (l *Logger) SetHeader(_ string) {
panic("not implemented")
}
var _ echo.Logger = &Logger{}
// Write writes len(p) bytes from p to the underlying data stream.
// It returns the number of bytes written from p (0 <= n <= len(p)) and
// any error encountered that caused the write to stop early. Write must
// return a non-nil error if it returns n < len(p). Write must not modify
// the slice data, even temporarily.
//
// So…
// * Always say you wrote all the data.
// * Never return an error?
// * Parse the data as JSON without ever modifying the slice.
func (l *Logger) Write(p []byte) (int, error) {
logMessage := map[string]interface{}{}
err := json.Unmarshal(p, &logMessage)
if err != nil {
return 0, err
}
l.lager.Info("logger-middleware", logMessage)
return len(p), nil
}
var _ io.Writer = &Logger{}
func NewLogger(logger lager.Logger) *Logger {
return &Logger{
lager: logger,
action: "log",
}
}