Skip to content

Commit

Permalink
cleanup logging (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
decentralgabe authored Apr 12, 2024
1 parent 6c55cbd commit 74fba5a
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 22 deletions.
4 changes: 2 additions & 2 deletions impl/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ func configureLogger(level string) {
if level != "" {
logLevel, err := logrus.ParseLevel(level)
if err != nil {
logrus.WithError(err).WithField("level", level).Error("could not parse log level, setting to debug")
logrus.SetLevel(logrus.DebugLevel)
logrus.WithError(err).WithField("level", level).Error("could not parse log level, setting to info")
logrus.SetLevel(logrus.InfoLevel)
} else {
logrus.SetLevel(logLevel)
}
Expand Down
4 changes: 2 additions & 2 deletions impl/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
)

const (
Version = "1"

ServiceName = "did-dht"
DefaultConfigPath = "config/config.toml"
DefaultEnvPath = "config/config.env"
Expand All @@ -29,8 +31,6 @@ const (
LogLevel EnvironmentVariable = "LOG_LEVEL"
)

var Version = "devel"

type (
Environment string
EnvironmentVariable string
Expand Down
2 changes: 1 addition & 1 deletion impl/internal/dht/getput.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func startGetTraversal(
Alpha: 15,
Target: target,
DoQuery: func(ctx context.Context, addr krpc.NodeAddr) traversal.QueryResult {
queryCtx, cancel := context.WithTimeout(ctx, 8*time.Second)
queryCtx, cancel := context.WithTimeout(ctx, time.Second*8)
defer cancel()

res := s.Get(queryCtx, dht.NewAddr(addr.UDP()), target, seq, dht.QueryRateLimiting{})
Expand Down
13 changes: 1 addition & 12 deletions impl/pkg/dht/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
)

func init() {
logrus.SetFormatter(&logrus.JSONFormatter{})
log.Default.WithDefaultLevel(log.Debug)
log.Default.Handlers = []log.Handler{logrusHandler{}}
}
Expand All @@ -20,15 +19,5 @@ type logrusHandler struct{}
func (logrusHandler) Handle(record log.Record) {
entry := logrus.WithField("names", strings.Join(record.Names, "/"))
msg := strings.Replace(record.Msg.String(), "\n", "\\n", -1)

switch record.Level {
case log.Debug:
entry.Debug(msg)
case log.Info:
entry.Info(msg)
case log.Warning, log.Error:
entry.Warn(msg)
default:
entry.Debug(msg)
}
entry.Debug(msg)
}
9 changes: 4 additions & 5 deletions impl/pkg/service/pkarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ func (s *PkarrService) PublishPkarr(ctx context.Context, id string, record pkarr
}

// return here and put it in the DHT asynchronously
// TODO(gabe): consider a background process to monitor failures
go func() {
// Create a new context with a timeout so that the parent context does not cancel the put
putCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
Expand Down Expand Up @@ -265,11 +264,11 @@ func (s *PkarrService) republish() {
recordID := zbase32.EncodeToString(record.Key[:])
logrus.WithContext(ctx).Debugf("republishing record: %s", recordID)

putCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
putCtx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()

if _, putErr := s.dht.Put(putCtx, record.BEP44()); putErr != nil {
logrus.WithContext(putCtx).WithError(putErr).Errorf("failed to republish record: %s", recordID)
logrus.WithContext(putCtx).WithError(putErr).Debugf("failed to republish record: %s", recordID)
atomic.AddInt32(&batchErrCnt, 1)
} else {
atomic.AddInt32(&batchSuccessCnt, 1)
Expand All @@ -290,7 +289,7 @@ func (s *PkarrService) republish() {
"batch_number": batchCnt,
"success": successCnt,
"errors": errCnt,
}).Infof("batch [%d] completed with a [%02f] percent success rate", batchCnt, successRate)
}).Infof("batch [%d] completed with a [%.2f] percent success rate", batchCnt, successRate)

if successRate < 0.8 {
logrus.WithContext(ctx).WithFields(logrus.Fields{
Expand All @@ -311,7 +310,7 @@ func (s *PkarrService) republish() {
"success": seenRecords - errCnt,
"errors": errCnt,
"total": seenRecords,
}).Infof("republishing complete with [%d] batches of [%d] total records with an [%02f] percent success rate", batchCnt, seenRecords, successRate*100)
}).Infof("republishing complete with [%d] batches of [%d] total records with an [%.2f] percent success rate", batchCnt, seenRecords, successRate*100)
}

// Close closes the Pkarr service gracefully
Expand Down

0 comments on commit 74fba5a

Please sign in to comment.