chore: share Truncate helper between mqtt and http trace logs (#31947)
This commit is contained in:
parent
8a8abdaede
commit
0b5017e7c1
2 changed files with 13 additions and 13 deletions
|
|
@ -242,7 +242,7 @@ func (m *Client) Publish(topic string, retained bool, payload string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
m.log.TRACE.Printf("send %s: '%v'", topic, truncate(payload))
|
m.log.TRACE.Printf("send %s: '%v'", topic, request.Truncate(payload))
|
||||||
token := m.client.Publish(topic, m.Qos, retained, payload)
|
token := m.client.Publish(topic, m.Qos, retained, payload)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
|
|
@ -290,7 +290,7 @@ func (m *Client) ListenSetter(topic string, callback func(string) error) error {
|
||||||
func (m *Client) listen(topic string) paho.Token {
|
func (m *Client) listen(topic string) paho.Token {
|
||||||
token := m.client.Subscribe(topic, m.Qos, func(c paho.Client, msg paho.Message) {
|
token := m.client.Subscribe(topic, m.Qos, func(c paho.Client, msg paho.Message) {
|
||||||
payload := string(msg.Payload())
|
payload := string(msg.Payload())
|
||||||
m.log.TRACE.Printf("recv %s: '%v'", topic, truncate(payload))
|
m.log.TRACE.Printf("recv %s: '%v'", topic, request.Truncate(payload))
|
||||||
if len(payload) > 0 {
|
if len(payload) > 0 {
|
||||||
m.mux.Lock()
|
m.mux.Lock()
|
||||||
callbacks := m.listener[topic]
|
callbacks := m.listener[topic]
|
||||||
|
|
@ -303,11 +303,3 @@ func (m *Client) listen(topic string) paho.Token {
|
||||||
})
|
})
|
||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
|
|
||||||
// truncate limits payload length in trace logs, matching HTTP logging
|
|
||||||
func truncate(s string) string {
|
|
||||||
if len(s) > request.LogMaxLen {
|
|
||||||
s = s[:request.LogMaxLen] + "..."
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -124,6 +124,14 @@ func drainBody(b io.ReadCloser) (r1, r2 io.ReadCloser, err error) {
|
||||||
return io.NopCloser(&buf), io.NopCloser(bytes.NewReader(buf.Bytes())), nil
|
return io.NopCloser(&buf), io.NopCloser(bytes.NewReader(buf.Bytes())), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Truncate limits a string to LogMaxLen for trace logs, appending an ellipsis when cut
|
||||||
|
func Truncate(s string) string {
|
||||||
|
if len(s) > LogMaxLen {
|
||||||
|
return s[:LogMaxLen] + "..."
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
// dump http request/response body
|
// dump http request/response body
|
||||||
func dump(r io.ReadCloser, w *strings.Builder) error {
|
func dump(r io.ReadCloser, w *strings.Builder) error {
|
||||||
body, err := io.ReadAll(r)
|
body, err := io.ReadAll(r)
|
||||||
|
|
@ -133,7 +141,7 @@ func dump(r io.ReadCloser, w *strings.Builder) error {
|
||||||
if w.Len() > 0 && len(body) > 0 {
|
if w.Len() > 0 && len(body) > 0 {
|
||||||
w.WriteString("\n--\n")
|
w.WriteString("\n--\n")
|
||||||
}
|
}
|
||||||
_, err = w.Write(bytes.TrimSpace(body[:min(LogMaxLen, len(body))]))
|
_, err = w.WriteString(Truncate(strings.TrimSpace(string(body))))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -155,7 +163,7 @@ func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
if LogHeaders {
|
if LogHeaders {
|
||||||
if body, err := httputil.DumpRequestOut(req, true); err == nil {
|
if body, err := httputil.DumpRequestOut(req, true); err == nil {
|
||||||
bld.WriteString("\n")
|
bld.WriteString("\n")
|
||||||
bld.Write(bytes.TrimSpace(body[:min(LogMaxLen, len(body))]))
|
bld.WriteString(Truncate(strings.TrimSpace(string(body))))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if save, req.Body, err = drainBody(req.Body); err == nil {
|
if save, req.Body, err = drainBody(req.Body); err == nil {
|
||||||
|
|
@ -187,7 +195,7 @@ func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
if LogHeaders {
|
if LogHeaders {
|
||||||
if body, err := httputil.DumpResponse(resp, logBody); err == nil {
|
if body, err := httputil.DumpResponse(resp, logBody); err == nil {
|
||||||
bld.WriteString("\n\n")
|
bld.WriteString("\n\n")
|
||||||
bld.Write(bytes.TrimSpace(body[:min(LogMaxLen, len(body))]))
|
bld.WriteString(Truncate(strings.TrimSpace(string(body))))
|
||||||
}
|
}
|
||||||
} else if logBody {
|
} else if logBody {
|
||||||
if save, resp.Body, err = drainBody(resp.Body); err == nil {
|
if save, resp.Body, err = drainBody(resp.Body); err == nil {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue