Skip to content

Commit

Permalink
Rework trace/log/info debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
benpate committed Sep 28, 2024
1 parent e3f6777 commit ec89f3d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 55 deletions.
36 changes: 18 additions & 18 deletions inbox/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ import (
"github.com/rs/zerolog"
)

// canLog is a silly zerolog helper that returns TRUE
// if the provided log level would be allowed
// (based on the global log level).
// This makes it easier to execute expensive code conditionally,
// for instance: marshalling a JSON object for logging.
func canLog(level zerolog.Level) bool {
return zerolog.GlobalLevel() <= level
}

// canTrace returns TRUE if zerolog is configured to allow Trace logs
// This function is here for completeness. It may or may not be used
func canTrace() bool {
return canLog(zerolog.TraceLevel)
// canInfo returns TRUE if zerolog is configured to allow Info logs
// nolint:unused
func canInfo() bool {
return canLog(zerolog.InfoLevel)
}

// canDebug returns TRUE if zerolog is configured to allow Debug logs
// This function is here for completeness. It may or may not be used
// nolint:unused
func canDebug() bool {
return canLog(zerolog.DebugLevel)
}

// canInfo returns TRUE if zerolog is configured to allow Info logs
// This function is here for completeness. It may or may not be used
func canInfo() bool {
return canLog(zerolog.InfoLevel)
// canTrace returns TRUE if zerolog is configured to allow Trace logs
// nolint:unused
func canTrace() bool {
return canLog(zerolog.TraceLevel)
}

// canLog is a silly zerolog helper that returns TRUE
// if the provided log level would be allowed
// (based on the global log level).
// This makes it easier to execute expensive code conditionally,
// for instance: marshalling a JSON object for logging.
func canLog(level zerolog.Level) bool {
return zerolog.GlobalLevel() <= level
}
12 changes: 0 additions & 12 deletions outbox/actor-send.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package outbox

import (
"encoding/json"

"github.com/benpate/hannibal/streams"
"github.com/benpate/hannibal/vocab"
"github.com/benpate/rosetta/mapof"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

Expand All @@ -19,15 +16,6 @@ import (
// https://www.w3.org/TR/activitypub/#delivery
func (actor *Actor) Send(message mapof.Any) {

if canLog(zerolog.DebugLevel) {

messageID := message.GetString(vocab.PropertyID)
log.Debug().Msg("Sending Message: " + messageID)

rawJSON, _ := json.MarshalIndent(message, "", " ")
log.Trace().Msg(string(rawJSON))
}

// Create a streams.Document from the message
document := streams.NewDocument(message, streams.WithClient(actor.getClient()))

Expand Down
19 changes: 2 additions & 17 deletions outbox/sendTask.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package outbox

import (
"encoding/json"

"github.com/benpate/derp"
"github.com/benpate/hannibal/streams"
"github.com/benpate/hannibal/vocab"
"github.com/benpate/remote"
"github.com/benpate/remote/options"
"github.com/benpate/rosetta/mapof"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

Expand All @@ -35,19 +32,9 @@ func (task SendTask) Run() error {

inboxURL := task.recipient.Inbox().ID()

if canLog(zerolog.DebugLevel) {
messageID := task.message.GetString(vocab.PropertyID)
log.Debug().Str("loc", location).Str("id", messageID).Str("to", inboxURL).Msg("Sending:")

if canLog(zerolog.TraceLevel) {
rawJSON, _ := json.MarshalIndent(task.message, "", " ")
log.Trace().Msg(string(rawJSON))
}
}

if inboxURL == "" {
log.Error().Msg("Recipient does not have an inbox")
return nil // returning nil error because we have failed so bacly that we don't even want to retry.
return nil // returning nil error because we have failed so badly that we don't even want to retry.
}

// Send the request to the target Actor's inbox
Expand All @@ -57,16 +44,14 @@ func (task SendTask) Run() error {
With(SignRequest(task.actor)).
JSON(task.message)

if canLog(zerolog.TraceLevel) {
if canDebug() {
transaction.With(options.Debug())
}

if err := transaction.Send(); err != nil {
return derp.ReportAndReturn(derp.Wrap(err, location, "Error sending ActivityPub request", inboxURL))
}

log.Debug().Msg("Outbox: sendTask: Activity sent successfully")

// Done!
return nil
}
18 changes: 18 additions & 0 deletions outbox/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@ package outbox

import "github.com/rs/zerolog"

// canInfo returns TRUE if zerolog is configured to allow Info logs
// nolint:unused
func canInfo() bool {
return canLog(zerolog.InfoLevel)
}

// canDebug returns TRUE if zerolog is configured to allow Debug logs
// nolint:unused
func canDebug() bool {
return canLog(zerolog.DebugLevel)
}

// canTrace returns TRUE if zerolog is configured to allow Trace logs
// nolint:unused
func canTrace() bool {
return canLog(zerolog.TraceLevel)
}

// canLog is a silly zerolog helper that returns TRUE
// if the provided log level would be allowed
// (based on the global log level).
Expand Down
27 changes: 19 additions & 8 deletions sigs/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@ package sigs

import "github.com/rs/zerolog"

func canLog(logLevel zerolog.Level) bool {
return logLevel >= zerolog.GlobalLevel()
}

func canTrace() bool {
return canLog(zerolog.TraceLevel)
// canInfo returns TRUE if zerolog is configured to allow Info logs
// nolint:unused
func canInfo() bool {
return canLog(zerolog.InfoLevel)
}

// canDebug returns TRUE if zerolog is configured to allow Debug logs
// nolint:unused
func canDebug() bool {
return canLog(zerolog.DebugLevel)
}

func canInfo() bool {
return canLog(zerolog.InfoLevel)
// canTrace returns TRUE if zerolog is configured to allow Trace logs
// nolint:unused
func canTrace() bool {
return canLog(zerolog.TraceLevel)
}

// canLog is a silly zerolog helper that returns TRUE
// if the provided log level would be allowed
// (based on the global log level).
// This makes it easier to execute expensive code conditionally,
// for instance: marshalling a JSON object for logging.
func canLog(level zerolog.Level) bool {
return zerolog.GlobalLevel() <= level
}

0 comments on commit ec89f3d

Please sign in to comment.