forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request_logger_file_writer.go
149 lines (128 loc) · 3.56 KB
/
request_logger_file_writer.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
package ui
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"sync"
"time"
)
type RequestLoggerFileWriter struct {
ui *UI
lock *sync.Mutex
filePaths []string
logFiles []*os.File
dumpSanitizer *regexp.Regexp
}
func newRequestLoggerFileWriter(ui *UI, lock *sync.Mutex, filePaths []string) *RequestLoggerFileWriter {
return &RequestLoggerFileWriter{
ui: ui,
lock: lock,
filePaths: filePaths,
logFiles: []*os.File{},
dumpSanitizer: regexp.MustCompile(tokenRegexp),
}
}
func (display *RequestLoggerFileWriter) DisplayBody([]byte) error {
for _, logFile := range display.logFiles {
_, err := logFile.WriteString(RedactedValue)
if err != nil {
return err
}
}
return nil
}
func (display *RequestLoggerFileWriter) DisplayDump(dump string) error {
sanitized := display.dumpSanitizer.ReplaceAllString(dump, RedactedValue)
for _, logFile := range display.logFiles {
_, err := logFile.WriteString(sanitized)
if err != nil {
return err
}
}
return nil
}
func (display *RequestLoggerFileWriter) DisplayHeader(name string, value string) error {
return display.DisplayMessage(fmt.Sprintf("%s: %s", name, value))
}
func (display *RequestLoggerFileWriter) DisplayHost(name string) error {
return display.DisplayMessage(fmt.Sprintf("Host: %s", name))
}
func (display *RequestLoggerFileWriter) DisplayJSONBody(body []byte) error {
if body == nil || len(body) == 0 {
return nil
}
sanitized, err := SanitizeJSON(body)
if err != nil {
return display.DisplayMessage(string(body))
}
buff := new(bytes.Buffer)
encoder := json.NewEncoder(buff)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
err = encoder.Encode(sanitized)
if err != nil {
return err
}
for _, logFile := range display.logFiles {
_, err = logFile.Write(buff.Bytes())
if err != nil {
return err
}
}
return nil
}
func (display *RequestLoggerFileWriter) DisplayMessage(msg string) error {
for _, logFile := range display.logFiles {
_, err := logFile.WriteString(fmt.Sprintf("%s\n", msg))
if err != nil {
return err
}
}
return nil
}
func (display *RequestLoggerFileWriter) DisplayRequestHeader(method string, uri string, httpProtocol string) error {
return display.DisplayMessage(fmt.Sprintf("%s %s %s", method, uri, httpProtocol))
}
func (display *RequestLoggerFileWriter) DisplayResponseHeader(httpProtocol string, status string) error {
return display.DisplayMessage(fmt.Sprintf("%s %s", httpProtocol, status))
}
func (display *RequestLoggerFileWriter) DisplayType(name string, requestDate time.Time) error {
return display.DisplayMessage(fmt.Sprintf("%s: [%s]", name, requestDate.Format(time.RFC3339)))
}
func (display *RequestLoggerFileWriter) HandleInternalError(err error) {
display.ui.DisplayWarning(err.Error())
}
func (display *RequestLoggerFileWriter) Start() error {
display.lock.Lock()
for _, filePath := range display.filePaths {
err := os.MkdirAll(filepath.Dir(filePath), os.ModeDir|os.ModePerm)
if err != nil {
return err
}
logFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600)
if err != nil {
return err
}
display.logFiles = append(display.logFiles, logFile)
}
return nil
}
func (display *RequestLoggerFileWriter) Stop() error {
var err error
for _, logFile := range display.logFiles {
_, lastLineErr := logFile.WriteString("\n")
closeErr := logFile.Close()
switch {
case closeErr != nil:
err = closeErr
case lastLineErr != nil:
err = lastLineErr
}
}
display.logFiles = []*os.File{}
display.lock.Unlock()
return err
}