-
Notifications
You must be signed in to change notification settings - Fork 94
/
logging.go
219 lines (164 loc) · 4.38 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* @license
* Copyright 2020 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package util
import (
"fmt"
"net/http"
"net/http/httputil"
"os"
"path/filepath"
"strings"
"time"
"github.com/jcelliott/lumber"
)
// Log is the shared Lumber Logger logging to console and after calling SetupLogging also to file
var Log lumber.Logger = lumber.NewConsoleLogger(lumber.INFO)
var requestLogFile *os.File
var responseLogFile *os.File
// SetupLogging is used to initialize the shared file Logger once the necessary setup config is available
func SetupLogging(verbose bool) error {
multiLog := lumber.NewMultiLogger()
consoleLog := lumber.NewConsoleLogger(lumber.INFO)
if verbose {
consoleLog.Level(lumber.DEBUG)
}
multiLog.AddLoggers(consoleLog)
if _, err := os.Stat(".logs"); os.IsNotExist(err) {
err = os.Mkdir(".logs", 0777)
if err != nil {
FailOnError(err, "could not create directory")
}
}
logName := ".logs" + string(os.PathSeparator) + time.Now().Format("20060102-150405") + ".log"
fileLog, err := lumber.NewAppendLogger(logName)
if err != nil {
return err
}
fileLog.Level(lumber.DEBUG)
multiLog.AddLoggers(fileLog)
Log = multiLog
err = setupRequestLog()
if err != nil {
return err
}
return setupResponseLog()
}
func setupRequestLog() error {
if logFilePath, found := os.LookupEnv("MONACO_REQUEST_LOG"); found {
logFilePath, err := filepath.Abs(logFilePath)
if err != nil {
return err
}
Log.Debug("request log activated at %s", logFilePath)
handle, err := prepareLogFile(logFilePath)
if err != nil {
return err
}
requestLogFile = handle
} else {
Log.Debug("request log not activated")
}
return nil
}
func setupResponseLog() error {
if logFilePath, found := os.LookupEnv("MONACO_RESPONSE_LOG"); found {
logFilePath, err := filepath.Abs(logFilePath)
if err != nil {
return err
}
Log.Debug("response log activated at %s", logFilePath)
handle, err := prepareLogFile(logFilePath)
if err != nil {
return err
}
responseLogFile = handle
} else {
Log.Debug("response log not activated")
}
return nil
}
func prepareLogFile(file string) (*os.File, error) {
return os.OpenFile(file, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
}
func IsRequestLoggingActive() bool {
return requestLogFile != nil
}
func IsResponseLoggingActive() bool {
return responseLogFile != nil
}
func LogRequest(id string, request *http.Request) error {
if !IsRequestLoggingActive() {
return nil
}
var dumpBody = false
if contentTypes, ok := request.Header["Content-Type"]; ok {
contentType := contentTypes[len(contentTypes)-1]
dumpBody = shouldDumpBody(contentType)
}
dump, err := httputil.DumpRequestOut(request, dumpBody)
if err != nil {
return err
}
stringDump := string(dump)
_, err = requestLogFile.WriteString(fmt.Sprintf(`Request-ID: %s
%s
=========================
`, id, stringDump))
if err != nil {
return err
}
return requestLogFile.Sync()
}
func LogResponse(id string, response *http.Response) error {
if !IsResponseLoggingActive() {
return nil
}
var dumpBody = false
if contentTypes, ok := response.Header["Content-Type"]; ok {
contentType := contentTypes[len(contentTypes)-1]
dumpBody = shouldDumpBody(contentType)
}
dump, err := httputil.DumpResponse(response, dumpBody)
if err != nil {
return err
}
if id != "" {
_, err = responseLogFile.WriteString(fmt.Sprintf("Request-ID: %s\n", id))
if err != nil {
return err
}
}
stringDump := string(dump)
_, err = responseLogFile.WriteString(fmt.Sprintf(`%s
=========================
`, stringDump))
if err != nil {
return err
}
return responseLogFile.Sync()
}
func shouldDumpBody(contentType string) bool {
if strings.HasPrefix("text/", contentType) {
return true
}
if strings.HasPrefix("application/json", contentType) {
return true
}
if strings.HasPrefix("application/xml", contentType) {
return true
}
return false
}