From 2c811d9b135e6e8294d2a4637dbe65f123f3fe3e Mon Sep 17 00:00:00 2001 From: Joseph Little Date: Tue, 13 Aug 2024 11:29:13 +0100 Subject: [PATCH 1/8] multipart request logging calls converted to Zap.levelW calls. --- httpclient/multipartrequest.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/httpclient/multipartrequest.go b/httpclient/multipartrequest.go index ecc1331..d4fa462 100644 --- a/httpclient/multipartrequest.go +++ b/httpclient/multipartrequest.go @@ -164,18 +164,18 @@ func createStreamingMultipartRequestBody(files map[string][]string, formDataFiel go func() { defer func() { if err := writer.Close(); err != nil { - sugar.Error("Failed to close multipart writer", zap.Error(err)) + sugar.Errorw("Failed to close multipart writer", zap.Error(err)) } if err := pw.Close(); err != nil { - sugar.Error("Failed to close pipe writer", zap.Error(err)) + sugar.Errorw("Failed to close pipe writer", zap.Error(err)) } }() for fieldName, filePaths := range files { for _, filePath := range filePaths { - sugar.Debug("Adding file part", zap.String("field_name", fieldName), zap.String("file_path", filePath)) + sugar.Debugw("Adding file part", zap.String("field_name", fieldName), zap.String("file_path", filePath)) if err := addFilePart(writer, fieldName, filePath, fileContentTypes, formDataPartHeaders, sugar); err != nil { - sugar.Error("Failed to add file part", zap.Error(err)) + sugar.Errorw("Failed to add file part", zap.Error(err)) pw.CloseWithError(err) return } @@ -183,9 +183,9 @@ func createStreamingMultipartRequestBody(files map[string][]string, formDataFiel } for key, val := range formDataFields { - sugar.Debug("Adding form field", zap.String("field_name", key), zap.String("field_value", val)) + sugar.Debugw("Adding form field", zap.String("field_name", key), zap.String("field_value", val)) if err := addFormField(writer, key, val, sugar); err != nil { - sugar.Error("Failed to add form field", zap.Error(err)) + sugar.Errorw("Failed to add form field", zap.Error(err)) pw.CloseWithError(err) return } @@ -214,7 +214,7 @@ func createStreamingMultipartRequestBody(files map[string][]string, formDataFiel func addFilePart(writer *multipart.Writer, fieldName, filePath string, fileContentTypes map[string]string, formDataPartHeaders map[string]http.Header, sugar *zap.SugaredLogger) error { file, err := os.Open(filePath) if err != nil { - sugar.Error("Failed to open file", zap.String("filePath", filePath), zap.Error(err)) + sugar.Errorw("Failed to open file", zap.String("filePath", filePath), zap.Error(err)) return err } defer file.Close() @@ -229,7 +229,7 @@ func addFilePart(writer *multipart.Writer, fieldName, filePath string, fileConte part, err := writer.CreatePart(header) if err != nil { - sugar.Error("Failed to create form file part", zap.String("fieldName", fieldName), zap.Error(err)) + sugar.Errorw("Failed to create form file part", zap.String("fieldName", fieldName), zap.Error(err)) return err } @@ -238,14 +238,14 @@ func addFilePart(writer *multipart.Writer, fieldName, filePath string, fileConte fileSize, err := file.Stat() if err != nil { - sugar.Error("Failed to get file info", zap.String("filePath", filePath), zap.Error(err)) + sugar.Errorw("Failed to get file info", zap.String("filePath", filePath), zap.Error(err)) return err } progressLogger := logUploadProgress(file, fileSize.Size(), sugar) uploadState := &UploadState{} if err := chunkFileUpload(file, encoder, progressLogger, uploadState, sugar); err != nil { - sugar.Error("Failed to copy file content", zap.String("filePath", filePath), zap.Error(err)) + sugar.Errorw("Failed to copy file content", zap.String("filePath", filePath), zap.Error(err)) return err } From 3b0b9b4cee9ef0efcabbed640066d9534b3899ff Mon Sep 17 00:00:00 2001 From: Joseph Little Date: Tue, 13 Aug 2024 11:42:04 +0100 Subject: [PATCH 2/8] added req printout for multipart --- httpclient/multipartrequest.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/httpclient/multipartrequest.go b/httpclient/multipartrequest.go index d4fa462..d2edb3c 100644 --- a/httpclient/multipartrequest.go +++ b/httpclient/multipartrequest.go @@ -3,6 +3,7 @@ package httpclient import ( "context" "encoding/base64" + "encoding/json" "fmt" "io" "mime/multipart" @@ -119,6 +120,12 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][] req.Header.Set("Content-Type", contentType) startTime := time.Now() + + // Debugging + jsonData, _ := json.MarshalIndent(req, "", " ") + c.Sugar.Debug("LOGHERE") + c.Sugar.Debug(string(jsonData)) + resp, requestErr := c.http.Do(req) duration := time.Since(startTime) From ee8f2a836c040bd74e2b7b525087c631e41b0673 Mon Sep 17 00:00:00 2001 From: Joseph Little Date: Tue, 13 Aug 2024 11:44:34 +0100 Subject: [PATCH 3/8] logging print outs adjusted --- httpclient/multipartrequest.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/httpclient/multipartrequest.go b/httpclient/multipartrequest.go index d2edb3c..74a6eca 100644 --- a/httpclient/multipartrequest.go +++ b/httpclient/multipartrequest.go @@ -122,9 +122,13 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][] startTime := time.Now() // Debugging - jsonData, _ := json.MarshalIndent(req, "", " ") c.Sugar.Debug("LOGHERE") - c.Sugar.Debug(string(jsonData)) + jsonData, err := json.MarshalIndent(req, "", " ") + if err != nil { + c.Sugar.Debugf("error marshalling: %v", err) + } else { + c.Sugar.Debug(string(jsonData)) + } resp, requestErr := c.http.Do(req) duration := time.Since(startTime) From bc29e7589f2bc694f67c856d4af59e2986bcd998 Mon Sep 17 00:00:00 2001 From: Joseph Little Date: Tue, 13 Aug 2024 13:18:59 +0100 Subject: [PATCH 4/8] debug call change --- httpclient/multipartrequest.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/httpclient/multipartrequest.go b/httpclient/multipartrequest.go index 74a6eca..889a7f9 100644 --- a/httpclient/multipartrequest.go +++ b/httpclient/multipartrequest.go @@ -3,7 +3,6 @@ package httpclient import ( "context" "encoding/base64" - "encoding/json" "fmt" "io" "mime/multipart" @@ -123,12 +122,7 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][] // Debugging c.Sugar.Debug("LOGHERE") - jsonData, err := json.MarshalIndent(req, "", " ") - if err != nil { - c.Sugar.Debugf("error marshalling: %v", err) - } else { - c.Sugar.Debug(string(jsonData)) - } + c.Sugar.Debugf("%+v", req) resp, requestErr := c.http.Do(req) duration := time.Since(startTime) From f2e9b213636a37d10148430f640b280acb8e8e5d Mon Sep 17 00:00:00 2001 From: Joseph Little Date: Tue, 13 Aug 2024 13:23:02 +0100 Subject: [PATCH 5/8] more debug --- httpclient/multipartrequest.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/httpclient/multipartrequest.go b/httpclient/multipartrequest.go index 889a7f9..a1e2f90 100644 --- a/httpclient/multipartrequest.go +++ b/httpclient/multipartrequest.go @@ -122,7 +122,7 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][] // Debugging c.Sugar.Debug("LOGHERE") - c.Sugar.Debugf("%+v", req) + c.Sugar.Debugf("%+v", req.Header.Get("Content-Type")) resp, requestErr := c.http.Do(req) duration := time.Since(startTime) From e477efa003f54fcbac6919b9965e5819a2cab1d4 Mon Sep 17 00:00:00 2001 From: Joseph Little Date: Tue, 13 Aug 2024 13:30:30 +0100 Subject: [PATCH 6/8] moving debug stuff around --- httpclient/multipartrequest.go | 1 + 1 file changed, 1 insertion(+) diff --git a/httpclient/multipartrequest.go b/httpclient/multipartrequest.go index a1e2f90..2165cab 100644 --- a/httpclient/multipartrequest.go +++ b/httpclient/multipartrequest.go @@ -120,6 +120,7 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][] startTime := time.Now() + req.Header.Add("Content-Type", "image/png") // Debugging c.Sugar.Debug("LOGHERE") c.Sugar.Debugf("%+v", req.Header.Get("Content-Type")) From 53e867649f5e027cde9da8b4934f9430fc71eadc Mon Sep 17 00:00:00 2001 From: Joseph Little Date: Tue, 13 Aug 2024 13:31:30 +0100 Subject: [PATCH 7/8] removed debugging --- httpclient/multipartrequest.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/httpclient/multipartrequest.go b/httpclient/multipartrequest.go index 2165cab..c270b86 100644 --- a/httpclient/multipartrequest.go +++ b/httpclient/multipartrequest.go @@ -120,11 +120,6 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][] startTime := time.Now() - req.Header.Add("Content-Type", "image/png") - // Debugging - c.Sugar.Debug("LOGHERE") - c.Sugar.Debugf("%+v", req.Header.Get("Content-Type")) - resp, requestErr := c.http.Do(req) duration := time.Since(startTime) From 82a9733c1e303dff2ed5c6623171507ff7b7bc7c Mon Sep 17 00:00:00 2001 From: Joseph Little Date: Tue, 13 Aug 2024 14:52:07 +0100 Subject: [PATCH 8/8] improved logging formatting. --- httpclient/multipartrequest.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/httpclient/multipartrequest.go b/httpclient/multipartrequest.go index c270b86..3d3a7a5 100644 --- a/httpclient/multipartrequest.go +++ b/httpclient/multipartrequest.go @@ -78,7 +78,7 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][] if c.config.CustomTimeout > 0 { ctx, cancel = context.WithTimeout(context.Background(), c.config.CustomTimeout) - c.Sugar.Info("Using timeout context for multipart request", zap.Duration("custom_timeout_seconds", c.config.CustomTimeout)) + c.Sugar.Infow("Using timeout context for multipart request", zap.Duration("custom_timeout_seconds", c.config.CustomTimeout)) } else { ctx = context.Background() cancel = func() {} @@ -94,25 +94,25 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][] var err error body, contentType, err = createStreamingMultipartRequestBody(files, formDataFields, fileContentTypes, formDataPartHeaders, c.Sugar) if err != nil { - c.Sugar.Error("Failed to create streaming multipart request body", zap.Error(err)) + c.Sugar.Errorw("Failed to create streaming multipart request body", zap.Error(err)) } else { - c.Sugar.Info("Successfully created streaming multipart request body", zap.String("content_type", contentType)) + c.Sugar.Infow("Successfully created streaming multipart request body", zap.String("content_type", contentType)) } return err } if err := createBody(); err != nil { - c.Sugar.Error("Failed to create streaming multipart request body", zap.Error(err)) + c.Sugar.Errorw("Failed to create streaming multipart request body", zap.Error(err)) return nil, err } req, err := http.NewRequestWithContext(ctx, method, url, body) if err != nil { - c.Sugar.Error("Failed to create HTTP request", zap.Error(err)) + c.Sugar.Errorw("Failed to create HTTP request", zap.Error(err)) return nil, err } - c.Sugar.Info("Created HTTP Multipart request", zap.String("method", method), zap.String("url", url), zap.String("content_type", contentType)) + c.Sugar.Infow("Created HTTP Multipart request", zap.String("method", method), zap.String("url", url), zap.String("content_type", contentType)) (*c.Integration).PrepRequestParamsAndAuth(req) @@ -124,11 +124,11 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][] duration := time.Since(startTime) if requestErr != nil { - c.Sugar.Error("Failed to send request", zap.String("method", method), zap.String("endpoint", endpoint), zap.Error(requestErr)) + c.Sugar.Errorw("Failed to send request", zap.String("method", method), zap.String("endpoint", endpoint), zap.Error(requestErr)) return nil, requestErr } - c.Sugar.Debug("Request sent successfully", zap.String("method", method), zap.String("endpoint", endpoint), zap.Int("status_code", resp.StatusCode), zap.Duration("duration", duration)) + c.Sugar.Debugw("Request sent successfully", zap.String("method", method), zap.String("endpoint", endpoint), zap.Int("status_code", resp.StatusCode), zap.Duration("duration", duration)) if resp.StatusCode >= 200 && resp.StatusCode < 300 { return resp, response.HandleAPISuccessResponse(resp, out, c.Sugar) @@ -268,11 +268,11 @@ func addFilePart(writer *multipart.Writer, fieldName, filePath string, fileConte func addFormField(writer *multipart.Writer, key, val string, sugar *zap.SugaredLogger) error { fieldWriter, err := writer.CreateFormField(key) if err != nil { - sugar.Error("Failed to create form field", zap.String("key", key), zap.Error(err)) + sugar.Errorw("Failed to create form field", zap.String("key", key), zap.Error(err)) return err } if _, err := fieldWriter.Write([]byte(val)); err != nil { - sugar.Error("Failed to write form field", zap.String("key", key), zap.Error(err)) + sugar.Errorw("Failed to write form field", zap.String("key", key), zap.Error(err)) return err } return nil @@ -363,7 +363,7 @@ func chunkFileUpload(file *os.File, writer io.Writer, updateProgress func(int64) if chunkWritten >= chunkSize { currentChunk++ - sugar.Debug("File Upload Chunk Sent", + sugar.Debugw("File Upload Chunk Sent", zap.String("file_name", fileName), zap.Int64("chunk_number", currentChunk), zap.Int64("total_chunks", totalChunks), @@ -376,7 +376,7 @@ func chunkFileUpload(file *os.File, writer io.Writer, updateProgress func(int64) // sugar any remaining bytes that were written but didn't reach the sugar threshold if chunkWritten > 0 { currentChunk++ - sugar.Debug("Final Upload Chunk Sent", + sugar.Debugw("Final Upload Chunk Sent", zap.String("file_name", fileName), zap.Int64("chunk_number", currentChunk), zap.Int64("total_chunks", totalChunks), @@ -412,7 +412,7 @@ func logUploadProgress(file *os.File, fileSize int64, sugar *zap.SugaredLogger) if percentage >= lastLoggedPercentage+logInterval { elapsedTime := time.Since(startTime) - sugar.Info("Upload progress", + sugar.Infow("Upload progress", zap.String("file_name", fileName), zap.Float64("uploaded_MB's", float64(uploaded)/1048576), // sugar in MB (1024 * 1024) zap.Float64("total_filesize_in_MB", float64(fileSize)/1048576),