Skip to content

Commit e902852

Browse files
Copilotrbtr
andcommitted
Implement proper JSON byte array formatting in logs
Co-authored-by: rbtr <2940321+rbtr@users.noreply.github.com>
1 parent 107d383 commit e902852

File tree

2 files changed

+60
-16
lines changed

2 files changed

+60
-16
lines changed

cns/logger/cnslogger.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package logger
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"maps"
67
"os"
@@ -131,17 +132,36 @@ func (c *CNSLogger) Errorf(format string, args ...any) {
131132
c.sendTraceInternal(msg, ai.ErrorLevel)
132133
}
133134

135+
// toJSONString converts any object to a JSON string for logging purposes.
136+
// When the object contains json.RawMessage fields, they will be properly formatted
137+
// instead of being shown as byte arrays. Falls back to %+v if JSON marshaling fails.
138+
func toJSONString(obj any) string {
139+
if obj == nil {
140+
return "null"
141+
}
142+
143+
bytes, err := json.Marshal(obj)
144+
if err != nil {
145+
// Fall back to standard formatting if JSON marshaling fails
146+
return fmt.Sprintf("%+v", obj)
147+
}
148+
return string(bytes)
149+
}
150+
134151
func (c *CNSLogger) Request(tag string, request any, err error) {
135152
c.logger.Request(tag, request, err)
136153
if c.th == nil || c.disableTraceLogging {
137154
return
138155
}
156+
157+
requestString := toJSONString(request)
158+
139159
var msg string
140160
lvl := ai.InfoLevel
141161
if err == nil {
142-
msg = fmt.Sprintf("[%s] Received %T %+v.", tag, request, request)
162+
msg = fmt.Sprintf("[%s] Received %T %s.", tag, request, requestString)
143163
} else {
144-
msg = fmt.Sprintf("[%s] Failed to decode %T %+v %s.", tag, request, request, err.Error())
164+
msg = fmt.Sprintf("[%s] Failed to decode %T %s %s.", tag, request, requestString, err.Error())
145165
lvl = ai.ErrorLevel
146166
}
147167
c.sendTraceInternal(msg, lvl)
@@ -152,16 +172,19 @@ func (c *CNSLogger) Response(tag string, response any, returnCode types.Response
152172
if c.th == nil || c.disableTraceLogging {
153173
return
154174
}
175+
176+
responseString := toJSONString(response)
177+
155178
var msg string
156179
lvl := ai.InfoLevel
157180
switch {
158181
case err == nil && returnCode == 0:
159-
msg = fmt.Sprintf("[%s] Sent %T %+v.", tag, response, response)
182+
msg = fmt.Sprintf("[%s] Sent %T %s.", tag, response, responseString)
160183
case err != nil:
161-
msg = fmt.Sprintf("[%s] Code:%s, %+v %s.", tag, returnCode.String(), response, err.Error())
184+
msg = fmt.Sprintf("[%s] Code:%s, %s %s.", tag, returnCode.String(), responseString, err.Error())
162185
lvl = ai.ErrorLevel
163186
default:
164-
msg = fmt.Sprintf("[%s] Code:%s, %+v.", tag, returnCode.String(), response)
187+
msg = fmt.Sprintf("[%s] Code:%s, %s.", tag, returnCode.String(), responseString)
165188
}
166189
c.sendTraceInternal(msg, lvl)
167190
}
@@ -171,16 +194,20 @@ func (c *CNSLogger) ResponseEx(tag string, request, response any, returnCode typ
171194
if c.th == nil || c.disableTraceLogging {
172195
return
173196
}
197+
198+
requestString := toJSONString(request)
199+
responseString := toJSONString(response)
200+
174201
var msg string
175202
lvl := ai.InfoLevel
176203
switch {
177204
case err == nil && returnCode == 0:
178-
msg = fmt.Sprintf("[%s] Sent %T %+v %T %+v.", tag, request, request, response, response)
205+
msg = fmt.Sprintf("[%s] Sent %T %s %T %s.", tag, request, requestString, response, responseString)
179206
case err != nil:
180-
msg = fmt.Sprintf("[%s] Code:%s, %+v, %+v, %s.", tag, returnCode.String(), request, response, err.Error())
207+
msg = fmt.Sprintf("[%s] Code:%s, %s, %s, %s.", tag, returnCode.String(), requestString, responseString, err.Error())
181208
lvl = ai.ErrorLevel
182209
default:
183-
msg = fmt.Sprintf("[%s] Code:%s, %+v, %+v.", tag, returnCode.String(), request, response)
210+
msg = fmt.Sprintf("[%s] Code:%s, %s, %s.", tag, returnCode.String(), requestString, responseString)
184211
}
185212
c.sendTraceInternal(msg, lvl)
186213
}

log/logger.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package log
55

66
import (
7+
"encoding/json"
78
"fmt"
89
"io"
910
"log"
@@ -199,34 +200,50 @@ func (logger *Logger) rotate() error {
199200
return nil
200201
}
201202

203+
// toJSONString converts any object to a JSON string for logging purposes.
204+
// When the object contains json.RawMessage fields, they will be properly formatted
205+
// instead of being shown as byte arrays. Falls back to %+v if JSON marshaling fails.
206+
func toJSONString(obj interface{}) string {
207+
if obj == nil {
208+
return "null"
209+
}
210+
211+
bytes, err := json.Marshal(obj)
212+
if err != nil {
213+
// Fall back to standard formatting if JSON marshaling fails
214+
return fmt.Sprintf("%+v", obj)
215+
}
216+
return string(bytes)
217+
}
218+
202219
// Request logs a structured request.
203220
func (logger *Logger) Request(tag string, request interface{}, err error) {
204221
if err == nil {
205-
logger.Printf("[%s] Received %T %+v.", tag, request, request)
222+
logger.Printf("[%s] Received %T %s.", tag, request, toJSONString(request))
206223
} else {
207-
logger.Errorf("[%s] Failed to decode %T %+v %s.", tag, request, request, err.Error())
224+
logger.Errorf("[%s] Failed to decode %T %s %s.", tag, request, toJSONString(request), err.Error())
208225
}
209226
}
210227

211228
// Response logs a structured response.
212229
func (logger *Logger) Response(tag string, response interface{}, returnCode int, returnStr string, err error) {
213230
if err == nil && returnCode == 0 {
214-
logger.Printf("[%s] Sent %T %+v.", tag, response, response)
231+
logger.Printf("[%s] Sent %T %s.", tag, response, toJSONString(response))
215232
} else if err != nil {
216-
logger.Errorf("[%s] Code:%s, %+v %s.", tag, returnStr, response, err.Error())
233+
logger.Errorf("[%s] Code:%s, %s %s.", tag, returnStr, toJSONString(response), err.Error())
217234
} else {
218-
logger.Errorf("[%s] Code:%s, %+v.", tag, returnStr, response)
235+
logger.Errorf("[%s] Code:%s, %s.", tag, returnStr, toJSONString(response))
219236
}
220237
}
221238

222239
// ResponseEx logs a structured response and the request associate with it.
223240
func (logger *Logger) ResponseEx(tag string, request interface{}, response interface{}, returnCode int, returnStr string, err error) {
224241
if err == nil && returnCode == 0 {
225-
logger.Printf("[%s] Sent %T %+v %T %+v.", tag, request, request, response, response)
242+
logger.Printf("[%s] Sent %T %s %T %s.", tag, request, toJSONString(request), response, toJSONString(response))
226243
} else if err != nil {
227-
logger.Errorf("[%s] Code:%s, %+v, %+v %s.", tag, returnStr, request, response, err.Error())
244+
logger.Errorf("[%s] Code:%s, %s, %s %s.", tag, returnStr, toJSONString(request), toJSONString(response), err.Error())
228245
} else {
229-
logger.Errorf("[%s] Code:%s, %+v, %+v.", tag, returnStr, request, response)
246+
logger.Errorf("[%s] Code:%s, %s, %s.", tag, returnStr, toJSONString(request), toJSONString(response))
230247
}
231248
}
232249

0 commit comments

Comments
 (0)