Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #179

Merged
merged 4 commits into from
Apr 11, 2024
Merged

fixes #179

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading