Use human representation of byte counts in content-length logs

This commit is contained in:
2023-12-30 13:52:43 -06:00
parent 62eaebafd7
commit e6a3c9ca97
3 changed files with 17 additions and 4 deletions

View File

@@ -5,9 +5,11 @@ import (
"io"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/dustin/go-humanize"
"github.com/rs/zerolog/log"
"golang.org/x/time/rate"
)
@@ -81,7 +83,11 @@ func DoRequestNoRead(req *http.Request) (*http.Response, error) {
return nil, err
}
log.Debug().Int("code", resp.StatusCode).Str("content-type", resp.Header.Get("Content-Type")).Str("content-length", resp.Header.Get("Content-Length")).
contentLength, err := strconv.ParseUint(resp.Header.Get("Content-Length"), 10, 64)
if err != nil {
contentLength = 0
}
log.Debug().Int("code", resp.StatusCode).Str("content-type", resp.Header.Get("Content-Type")).Str("content-length", Bytes(contentLength)).
Str("duration", duration.String()).Msg("Response")
return resp, nil
@@ -112,12 +118,12 @@ func DoRequest(req *http.Request) (*http.Response, []byte, error) {
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Err(err).Int("code", resp.StatusCode).Str("content-type", resp.Header.Get("Content-Type")).Int("content-length", len(body)).
log.Err(err).Int("code", resp.StatusCode).Str("content-type", resp.Header.Get("Content-Type")).Str("content-length", Bytes(uint64(len(body)))).
Str("duration", duration.String()).Msg("Response (Unable to Read Body)")
return nil, nil, err
}
log.Debug().Int("code", resp.StatusCode).Str("content-type", resp.Header.Get("Content-Type")).Int("content-length", len(body)).
log.Debug().Int("code", resp.StatusCode).Str("content-type", resp.Header.Get("Content-Type")).Str("content-length", Bytes(uint64(len(body)))).
Str("duration", duration.String()).Msg("Response")
return resp, body, nil
}
@@ -169,3 +175,8 @@ func NormalizeTitle(title string) string {
"-",
)
}
// Bytes calls humanize.Bytes and removes space characters
func Bytes(bytes uint64) string {
return strings.Replace(humanize.Bytes(bytes), " ", "", -1)
}