Skip to content

Commit

Permalink
last one for now
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe committed Apr 11, 2024
1 parent 1d9f39d commit 0ec55b4
Showing 1 changed file with 2 additions and 21 deletions.
23 changes: 2 additions & 21 deletions impl/pkg/dht/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"strings"

"github.com/anacrolix/log"
"github.com/goccy/go-json"
"github.com/sirupsen/logrus"
)

func init() {
logrus.SetFormatter(&logrus.JSONFormatter{})
log.Default.WithDefaultLevel(log.Debug)
log.Default.Handlers = []log.Handler{logrusHandler{}}
}
Expand All @@ -19,26 +19,7 @@ type logrusHandler struct{}
// It intentionally downgrades the log level to reduce verbosity.
func (logrusHandler) Handle(record log.Record) {
entry := logrus.WithField("names", strings.Join(record.Names, "/"))
msg := strings.Replace(record.Msg.String(), "\n", "", -1)

// Check if the log message is a valid JSON string
var jsonMsg map[string]any
if err := json.Unmarshal([]byte(msg), &jsonMsg); err == nil {
// If the log message is a valid JSON string, escape backslashes and double quotes within the field values
for k, v := range jsonMsg {
if strVal, ok := v.(string); ok {
escaped := strings.Replace(strVal, "\\", "\\\\", -1)
escaped = strings.Replace(escaped, "\"", "\\\"", -1)
jsonMsg[k] = escaped
}
}
// Marshal the modified JSON message back to a string
escapedMsg, _ := json.Marshal(jsonMsg)
msg = string(escapedMsg)
} else {
// If the log message is not a valid JSON string, replace newline characters with empty strings
msg = strings.Replace(msg, "\n", "", -1)
}
msg := strings.Replace(record.Msg.String(), "\n", "\\n", -1)

switch record.Level {
case log.Debug:
Expand Down

0 comments on commit 0ec55b4

Please sign in to comment.