feat: add logging for endpoint requests and responses in client and utils

This commit is contained in:
zuoban
2025-08-12 14:33:51 +08:00
parent f849a2f5ea
commit 1f7fab5c0b
2 changed files with 6 additions and 5 deletions

View File

@@ -144,6 +144,7 @@ func (c *Client) ListVoices(ctx context.Context, locale string) ([]models.Voice,
} }
url := fmt.Sprintf(voicesEndpoint, endpoint["r"]) url := fmt.Sprintf(voicesEndpoint, endpoint["r"])
log.Println("ListVoices, endpoint:", endpoint)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -8,6 +8,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"log"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@@ -15,11 +16,9 @@ import (
"unicode/utf8" "unicode/utf8"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/sirupsen/logrus"
) )
var ( var (
log = logrus.New()
client = &http.Client{} client = &http.Client{}
) )
@@ -71,18 +70,18 @@ func GetEndpoint() (map[string]interface{}, error) {
} }
headerJson, err := json.Marshal(&headers) headerJson, err := json.Marshal(&headers)
fmt.Printf("GetEndpoint -> url: %s, headers: %v\n", endpointURL, string(headerJson)) log.Printf("GetEndpoint -> url: %s, headers: %v\n", endpointURL, string(headerJson))
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
log.Error("failed to do request: ", err) log.Println("failed to do request: ", err)
return nil, err return nil, err
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
log.Error("failed to get endpoint, status code: ", resp.StatusCode) log.Println("failed to get endpoint, status code: ", resp.StatusCode)
return nil, fmt.Errorf("failed to get endpoint, status code: %d", resp.StatusCode) return nil, fmt.Errorf("failed to get endpoint, status code: %d", resp.StatusCode)
} }
@@ -92,6 +91,7 @@ func GetEndpoint() (map[string]interface{}, error) {
return nil, err return nil, err
} }
log.Println("GetEndpoint <- success get endpoint result: ", result)
return result, nil return result, nil
} }