Skip to content

Commit

Permalink
don't dump raw bytes into log output? its hard to read
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgmiller committed Dec 6, 2024
1 parent 47b243c commit 287f912
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions cns/logger/cnslogger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logger

import (
"encoding/json"
"fmt"
"os"
"sync"
Expand Down Expand Up @@ -140,11 +141,19 @@ func (c *CNSLogger) Request(tag string, request any, err error) {
return
}

//an alternative is to make al request types support stringer/fmt.formatter but thats easy to mess up.

Check failure on line 144 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 144 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 144 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 144 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 144 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 144 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 144 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 144 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 144 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 144 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 144 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 144 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

commentFormatting: put a space between `//` and comment text (gocritic)
var requestString string
if bytes, err := json.Marshal(request); err != nil {

Check failure on line 146 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 137 (govet)

Check failure on line 146 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 137 (govet)

Check failure on line 146 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 137 (govet)

Check failure on line 146 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 137 (govet)

Check failure on line 146 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 137 (govet)

Check failure on line 146 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 137 (govet)

Check failure on line 146 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 137 (govet)

Check failure on line 146 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 137 (govet)

Check failure on line 146 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 137 (govet)

Check failure on line 146 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 137 (govet)

Check failure on line 146 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 137 (govet)

Check failure on line 146 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 137 (govet)
requestString = string(bytes)
} else {
requestString = fmt.Sprintf("Failed to json marshal %s, %+v", err, request)
}

var msg string
if err == nil {
msg = fmt.Sprintf("[%s] Received %T %+v.", tag, request, request)
msg = fmt.Sprintf("[%s] Received %T %s.", tag, request, requestString)
} else {
msg = fmt.Sprintf("[%s] Failed to decode %T %+v %s.", tag, request, request, err.Error())
msg = fmt.Sprintf("[%s] Failed to decode %T %s %s.", tag, request, requestString, err.Error())
}

c.sendTraceInternal(msg)
Expand All @@ -157,14 +166,21 @@ func (c *CNSLogger) Response(tag string, response any, returnCode types.Response
return
}

var responseString string
if bytes, err := json.Marshal(response); err != nil {

Check failure on line 170 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 162 (govet)

Check failure on line 170 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 162 (govet)

Check failure on line 170 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 162 (govet)

Check failure on line 170 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 162 (govet)

Check failure on line 170 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 162 (govet)

Check failure on line 170 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 162 (govet)

Check failure on line 170 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 162 (govet)

Check failure on line 170 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 162 (govet)

Check failure on line 170 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 162 (govet)

Check failure on line 170 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 162 (govet)

Check failure on line 170 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 162 (govet)

Check failure on line 170 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 162 (govet)
responseString = string(bytes)
} else {
responseString = fmt.Sprintf("Failed to json marshal %s, %+v", err, responseString)
}

var msg string
switch {
case err == nil && returnCode == 0:
msg = fmt.Sprintf("[%s] Sent %T %+v.", tag, response, response)
msg = fmt.Sprintf("[%s] Sent %T %s.", tag, response, responseString)
case err != nil:
msg = fmt.Sprintf("[%s] Code:%s, %+v %s.", tag, returnCode.String(), response, err.Error())
msg = fmt.Sprintf("[%s] Code:%s, %s %s.", tag, returnCode.String(), responseString, err.Error())
default:
msg = fmt.Sprintf("[%s] Code:%s, %+v.", tag, returnCode.String(), response)
msg = fmt.Sprintf("[%s] Code:%s, %s.", tag, returnCode.String(), responseString)
}

c.sendTraceInternal(msg)
Expand All @@ -177,14 +193,28 @@ func (c *CNSLogger) ResponseEx(tag string, request, response any, returnCode typ
return
}

var requestString string
if bytes, err := json.Marshal(request); err != nil {

Check failure on line 197 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 197 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 197 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 197 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 197 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 197 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 197 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 197 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 197 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 197 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 197 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 197 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)
requestString = string(bytes)
} else {
requestString = fmt.Sprintf("Failed to json marshal %s, %+v", err, request)
}

var responseString string
if bytes, err := json.Marshal(response); err != nil {

Check failure on line 204 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 204 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 204 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 204 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 204 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 204 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 204 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 204 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 204 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 204 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 204 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)

Check failure on line 204 in cns/logger/cnslogger.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

shadow: declaration of "err" shadows declaration at line 189 (govet)
responseString = string(bytes)
} else {
responseString = fmt.Sprintf("Failed to json marshal %s, %+v", err, responseString)
}

var msg string
switch {
case err == nil && returnCode == 0:
msg = fmt.Sprintf("[%s] Sent %T %+v %T %+v.", tag, request, request, response, response)
msg = fmt.Sprintf("[%s] Sent %T %s %T %s.", tag, request, requestString, response, responseString)
case err != nil:
msg = fmt.Sprintf("[%s] Code:%s, %+v, %+v, %s.", tag, returnCode.String(), request, response, err.Error())
msg = fmt.Sprintf("[%s] Code:%s, %s, %s, %s.", tag, returnCode.String(), requestString, responseString, err.Error())
default:
msg = fmt.Sprintf("[%s] Code:%s, %+v, %+v.", tag, returnCode.String(), request, response)
msg = fmt.Sprintf("[%s] Code:%s, %s, %s.", tag, returnCode.String(), requestString, responseString)
}

c.sendTraceInternal(msg)
Expand Down

0 comments on commit 287f912

Please sign in to comment.