rpc: truncate errdata at all log levels

This commit is contained in:
Ceyhun Onur 2024-06-20 18:40:38 +03:00
parent 7f6283e10a
commit a15b9c88cb
No known key found for this signature in database
GPG key ID: 60D53BEEE37F6422

View file

@ -477,12 +477,8 @@ func (h *handler) handleCallMsg(ctx *callProc, msg *jsonrpcMessage) *jsonrpcMess
if resp.Error != nil { if resp.Error != nil {
logCtx = append(logCtx, "err", resp.Error.Message) logCtx = append(logCtx, "err", resp.Error.Message)
if resp.Error.Data != nil { if resp.Error.Data != nil {
// If the log level is debug, log the full error data. Otherwise, try to truncate it.
if h.log.Enabled(context.Background(), log.LvlDebug) {
logCtx = append(logCtx, "errdata", resp.Error.Data)
} else {
errDataStr := fmt.Sprintf("%v", resp.Error.Data) errDataStr := fmt.Sprintf("%v", resp.Error.Data)
// Truncate the error data if it is too long. Otherwise, preserve the original data. // Truncate the error data as string if it is too long. Otherwise, preserve the original data.
if len(errDataStr) > callErrorDataLogTruncateLimit { if len(errDataStr) > callErrorDataLogTruncateLimit {
remaining := len(errDataStr) - callErrorDataLogTruncateLimit remaining := len(errDataStr) - callErrorDataLogTruncateLimit
errDataStr = fmt.Sprintf("%s... (truncated remaining %d chars)", errDataStr[:callErrorDataLogTruncateLimit], remaining) errDataStr = fmt.Sprintf("%s... (truncated remaining %d chars)", errDataStr[:callErrorDataLogTruncateLimit], remaining)
@ -491,7 +487,6 @@ func (h *handler) handleCallMsg(ctx *callProc, msg *jsonrpcMessage) *jsonrpcMess
logCtx = append(logCtx, "errdata", resp.Error.Data) logCtx = append(logCtx, "errdata", resp.Error.Data)
} }
} }
}
h.log.Warn("Served "+msg.Method, logCtx...) h.log.Warn("Served "+msg.Method, logCtx...)
} else { } else {
h.log.Debug("Served "+msg.Method, logCtx...) h.log.Debug("Served "+msg.Method, logCtx...)