diff --git a/impl/pkg/dht/logging.go b/impl/pkg/dht/logging.go index df7b8b39..30093aab 100644 --- a/impl/pkg/dht/logging.go +++ b/impl/pkg/dht/logging.go @@ -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{}} } @@ -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: