3
3
* SPDX-FileCopyrightText: Huawei Inc.
4
4
*/
5
5
6
- package common
6
+ package logger
7
7
8
8
import (
9
9
"bytes"
10
10
"fmt"
11
- "io/ioutil "
11
+ "io"
12
12
"log/slog"
13
13
"net/http"
14
- "xpanse-agent/pkg/logger"
15
14
)
16
15
17
16
type HttpRequestLogger struct {
@@ -20,21 +19,21 @@ type HttpRequestLogger struct {
20
19
21
20
func (httpRequestLogger * HttpRequestLogger ) RoundTrip (req * http.Request ) (* http.Response , error ) {
22
21
// Log the request URL
23
- logger . Logger .Info (fmt .Sprintf ("Request URL: %s" , req .URL .String ()))
22
+ Logger .Info (fmt .Sprintf ("Request URL: %s" , req .URL .String ()))
24
23
25
24
// Read and log the request body if it exists
26
25
var bodyBytes []byte
27
26
if req .Body != nil {
28
27
var err error
29
- bodyBytes , err = ioutil .ReadAll (req .Body )
28
+ bodyBytes , err = io .ReadAll (req .Body )
30
29
if err != nil {
31
30
return nil , err
32
31
}
33
32
// Log the request body
34
- logger . Logger .Info (fmt .Sprintf ("Request Body: %s" , string (bodyBytes )))
33
+ Logger .Info (fmt .Sprintf ("Request Body: %s" , string (bodyBytes )))
35
34
36
35
// Restore the io.ReadCloser to its original state
37
- req .Body = ioutil .NopCloser (bytes .NewBuffer (bodyBytes ))
36
+ req .Body = io .NopCloser (bytes .NewBuffer (bodyBytes ))
38
37
}
39
38
40
39
// Make the actual HTTP request using the embedded RoundTripper
@@ -44,18 +43,18 @@ func (httpRequestLogger *HttpRequestLogger) RoundTrip(req *http.Request) (*http.
44
43
}
45
44
46
45
// Log the response status code
47
- logger . Logger .Info (fmt .Sprintf ("Response Status: %d" , resp .StatusCode ))
46
+ Logger .Info (fmt .Sprintf ("Response Status: %d" , resp .StatusCode ))
48
47
49
48
// Read and log the response body
50
- responseBody , err := ioutil .ReadAll (resp .Body )
49
+ responseBody , err := io .ReadAll (resp .Body )
51
50
if err != nil {
52
51
return nil , err
53
52
}
54
53
// Log the response body
55
- logger . Logger .Info ("Response Body: " , slog .String ("msg" , string (responseBody )))
54
+ Logger .Info ("Response Body: " , slog .String ("msg" , string (responseBody )))
56
55
57
56
// Restore the response body so it can be read later by application code
58
- resp .Body = ioutil .NopCloser (bytes .NewBuffer (responseBody ))
57
+ resp .Body = io .NopCloser (bytes .NewBuffer (responseBody ))
59
58
60
59
return resp , nil
61
60
}
0 commit comments