11package logger
22
33import (
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+
134151func (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}
0 commit comments