Skip to content

Commit e0a150f

Browse files
authored
Merge pull request #261 from deploymenttheory/dev-jl-testing
Improved log formatting in multi-part requests
2 parents 3504d44 + 82a9733 commit e0a150f

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

Diff for: httpclient/multipartrequest.go

+24-23
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]
7878

7979
if c.config.CustomTimeout > 0 {
8080
ctx, cancel = context.WithTimeout(context.Background(), c.config.CustomTimeout)
81-
c.Sugar.Info("Using timeout context for multipart request", zap.Duration("custom_timeout_seconds", c.config.CustomTimeout))
81+
c.Sugar.Infow("Using timeout context for multipart request", zap.Duration("custom_timeout_seconds", c.config.CustomTimeout))
8282
} else {
8383
ctx = context.Background()
8484
cancel = func() {}
@@ -94,40 +94,41 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]
9494
var err error
9595
body, contentType, err = createStreamingMultipartRequestBody(files, formDataFields, fileContentTypes, formDataPartHeaders, c.Sugar)
9696
if err != nil {
97-
c.Sugar.Error("Failed to create streaming multipart request body", zap.Error(err))
97+
c.Sugar.Errorw("Failed to create streaming multipart request body", zap.Error(err))
9898
} else {
99-
c.Sugar.Info("Successfully created streaming multipart request body", zap.String("content_type", contentType))
99+
c.Sugar.Infow("Successfully created streaming multipart request body", zap.String("content_type", contentType))
100100
}
101101
return err
102102
}
103103

104104
if err := createBody(); err != nil {
105-
c.Sugar.Error("Failed to create streaming multipart request body", zap.Error(err))
105+
c.Sugar.Errorw("Failed to create streaming multipart request body", zap.Error(err))
106106
return nil, err
107107
}
108108

109109
req, err := http.NewRequestWithContext(ctx, method, url, body)
110110
if err != nil {
111-
c.Sugar.Error("Failed to create HTTP request", zap.Error(err))
111+
c.Sugar.Errorw("Failed to create HTTP request", zap.Error(err))
112112
return nil, err
113113
}
114114

115-
c.Sugar.Info("Created HTTP Multipart request", zap.String("method", method), zap.String("url", url), zap.String("content_type", contentType))
115+
c.Sugar.Infow("Created HTTP Multipart request", zap.String("method", method), zap.String("url", url), zap.String("content_type", contentType))
116116

117117
(*c.Integration).PrepRequestParamsAndAuth(req)
118118

119119
req.Header.Set("Content-Type", contentType)
120120

121121
startTime := time.Now()
122+
122123
resp, requestErr := c.http.Do(req)
123124
duration := time.Since(startTime)
124125

125126
if requestErr != nil {
126-
c.Sugar.Error("Failed to send request", zap.String("method", method), zap.String("endpoint", endpoint), zap.Error(requestErr))
127+
c.Sugar.Errorw("Failed to send request", zap.String("method", method), zap.String("endpoint", endpoint), zap.Error(requestErr))
127128
return nil, requestErr
128129
}
129130

130-
c.Sugar.Debug("Request sent successfully", zap.String("method", method), zap.String("endpoint", endpoint), zap.Int("status_code", resp.StatusCode), zap.Duration("duration", duration))
131+
c.Sugar.Debugw("Request sent successfully", zap.String("method", method), zap.String("endpoint", endpoint), zap.Int("status_code", resp.StatusCode), zap.Duration("duration", duration))
131132

132133
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
133134
return resp, response.HandleAPISuccessResponse(resp, out, c.Sugar)
@@ -164,28 +165,28 @@ func createStreamingMultipartRequestBody(files map[string][]string, formDataFiel
164165
go func() {
165166
defer func() {
166167
if err := writer.Close(); err != nil {
167-
sugar.Error("Failed to close multipart writer", zap.Error(err))
168+
sugar.Errorw("Failed to close multipart writer", zap.Error(err))
168169
}
169170
if err := pw.Close(); err != nil {
170-
sugar.Error("Failed to close pipe writer", zap.Error(err))
171+
sugar.Errorw("Failed to close pipe writer", zap.Error(err))
171172
}
172173
}()
173174

174175
for fieldName, filePaths := range files {
175176
for _, filePath := range filePaths {
176-
sugar.Debug("Adding file part", zap.String("field_name", fieldName), zap.String("file_path", filePath))
177+
sugar.Debugw("Adding file part", zap.String("field_name", fieldName), zap.String("file_path", filePath))
177178
if err := addFilePart(writer, fieldName, filePath, fileContentTypes, formDataPartHeaders, sugar); err != nil {
178-
sugar.Error("Failed to add file part", zap.Error(err))
179+
sugar.Errorw("Failed to add file part", zap.Error(err))
179180
pw.CloseWithError(err)
180181
return
181182
}
182183
}
183184
}
184185

185186
for key, val := range formDataFields {
186-
sugar.Debug("Adding form field", zap.String("field_name", key), zap.String("field_value", val))
187+
sugar.Debugw("Adding form field", zap.String("field_name", key), zap.String("field_value", val))
187188
if err := addFormField(writer, key, val, sugar); err != nil {
188-
sugar.Error("Failed to add form field", zap.Error(err))
189+
sugar.Errorw("Failed to add form field", zap.Error(err))
189190
pw.CloseWithError(err)
190191
return
191192
}
@@ -214,7 +215,7 @@ func createStreamingMultipartRequestBody(files map[string][]string, formDataFiel
214215
func addFilePart(writer *multipart.Writer, fieldName, filePath string, fileContentTypes map[string]string, formDataPartHeaders map[string]http.Header, sugar *zap.SugaredLogger) error {
215216
file, err := os.Open(filePath)
216217
if err != nil {
217-
sugar.Error("Failed to open file", zap.String("filePath", filePath), zap.Error(err))
218+
sugar.Errorw("Failed to open file", zap.String("filePath", filePath), zap.Error(err))
218219
return err
219220
}
220221
defer file.Close()
@@ -229,7 +230,7 @@ func addFilePart(writer *multipart.Writer, fieldName, filePath string, fileConte
229230

230231
part, err := writer.CreatePart(header)
231232
if err != nil {
232-
sugar.Error("Failed to create form file part", zap.String("fieldName", fieldName), zap.Error(err))
233+
sugar.Errorw("Failed to create form file part", zap.String("fieldName", fieldName), zap.Error(err))
233234
return err
234235
}
235236

@@ -238,14 +239,14 @@ func addFilePart(writer *multipart.Writer, fieldName, filePath string, fileConte
238239

239240
fileSize, err := file.Stat()
240241
if err != nil {
241-
sugar.Error("Failed to get file info", zap.String("filePath", filePath), zap.Error(err))
242+
sugar.Errorw("Failed to get file info", zap.String("filePath", filePath), zap.Error(err))
242243
return err
243244
}
244245

245246
progressLogger := logUploadProgress(file, fileSize.Size(), sugar)
246247
uploadState := &UploadState{}
247248
if err := chunkFileUpload(file, encoder, progressLogger, uploadState, sugar); err != nil {
248-
sugar.Error("Failed to copy file content", zap.String("filePath", filePath), zap.Error(err))
249+
sugar.Errorw("Failed to copy file content", zap.String("filePath", filePath), zap.Error(err))
249250
return err
250251
}
251252

@@ -267,11 +268,11 @@ func addFilePart(writer *multipart.Writer, fieldName, filePath string, fileConte
267268
func addFormField(writer *multipart.Writer, key, val string, sugar *zap.SugaredLogger) error {
268269
fieldWriter, err := writer.CreateFormField(key)
269270
if err != nil {
270-
sugar.Error("Failed to create form field", zap.String("key", key), zap.Error(err))
271+
sugar.Errorw("Failed to create form field", zap.String("key", key), zap.Error(err))
271272
return err
272273
}
273274
if _, err := fieldWriter.Write([]byte(val)); err != nil {
274-
sugar.Error("Failed to write form field", zap.String("key", key), zap.Error(err))
275+
sugar.Errorw("Failed to write form field", zap.String("key", key), zap.Error(err))
275276
return err
276277
}
277278
return nil
@@ -362,7 +363,7 @@ func chunkFileUpload(file *os.File, writer io.Writer, updateProgress func(int64)
362363

363364
if chunkWritten >= chunkSize {
364365
currentChunk++
365-
sugar.Debug("File Upload Chunk Sent",
366+
sugar.Debugw("File Upload Chunk Sent",
366367
zap.String("file_name", fileName),
367368
zap.Int64("chunk_number", currentChunk),
368369
zap.Int64("total_chunks", totalChunks),
@@ -375,7 +376,7 @@ func chunkFileUpload(file *os.File, writer io.Writer, updateProgress func(int64)
375376
// sugar any remaining bytes that were written but didn't reach the sugar threshold
376377
if chunkWritten > 0 {
377378
currentChunk++
378-
sugar.Debug("Final Upload Chunk Sent",
379+
sugar.Debugw("Final Upload Chunk Sent",
379380
zap.String("file_name", fileName),
380381
zap.Int64("chunk_number", currentChunk),
381382
zap.Int64("total_chunks", totalChunks),
@@ -411,7 +412,7 @@ func logUploadProgress(file *os.File, fileSize int64, sugar *zap.SugaredLogger)
411412
if percentage >= lastLoggedPercentage+logInterval {
412413
elapsedTime := time.Since(startTime)
413414

414-
sugar.Info("Upload progress",
415+
sugar.Infow("Upload progress",
415416
zap.String("file_name", fileName),
416417
zap.Float64("uploaded_MB's", float64(uploaded)/1048576), // sugar in MB (1024 * 1024)
417418
zap.Float64("total_filesize_in_MB", float64(fileSize)/1048576),

0 commit comments

Comments
 (0)