-
Notifications
You must be signed in to change notification settings - Fork 13
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
[CLOB-1001] Add custom error value formatter to zerolog logging library #31
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import ( | |
cmtcfg "github.com/cometbft/cometbft/config" | ||
dbm "github.com/cosmos/cosmos-db" | ||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/pkgerrors" | ||
"github.com/spf13/cast" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
|
@@ -29,6 +30,7 @@ import ( | |
"cosmossdk.io/store/snapshots" | ||
snapshottypes "cosmossdk.io/store/snapshots/types" | ||
storetypes "cosmossdk.io/store/types" | ||
errorspkg "github.com/pkg/errors" | ||
|
||
"github.com/cosmos/cosmos-sdk/baseapp" | ||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
|
@@ -110,7 +112,7 @@ func InterceptConfigsPreRunHandler(cmd *cobra.Command, customAppConfigTemplate s | |
if err != nil { | ||
return err | ||
} | ||
serverCtx.Logger = logger.With(log.ModuleKey, "server") | ||
serverCtx.Logger = logger.With(log.ModuleKey, "server").With("source", "go") | ||
|
||
// set server context | ||
return SetCmdServerContext(cmd, serverCtx) | ||
|
@@ -197,9 +199,39 @@ func CreateSDKLogger(ctx *Context, out io.Writer) (log.Logger, error) { | |
// Check if the CometBFT flag for trace logging is set and enable stack traces if so. | ||
opts = append(opts, log.TraceOption(ctx.Viper.GetBool("trace"))) // cmtcli.TraceFlag | ||
|
||
// Error fields should be set under error object | ||
zerolog.ErrorFieldName = "error" | ||
|
||
// Add the kind and message field | ||
zerolog.ErrorMarshalFunc = func(err error) interface{} { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should guard this behind a flag to allow people to opt out of the custom marshalling function in case they aren't using datadog. |
||
stackArr, ok := pkgerrors.MarshalStack(errorspkg.WithStack(err)).([]map[string]string) | ||
if !ok { | ||
return struct{}{} | ||
} | ||
objectToReturn := DatadogErrorTrackingObject{ | ||
// Discard common prefix stack traces from zerolog call sites | ||
Stack: stackArr[5:], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have a test for this to ensure that we are discarding exactly how much we want in case the stack depth changes due to a change in the implementation of this code or the underlying library. |
||
Kind: "Exception", | ||
Message: err.Error(), | ||
} | ||
return objectToReturn | ||
} | ||
|
||
return log.NewLogger(out, opts...), nil | ||
} | ||
|
||
type DatadogErrorTrackingObject struct { | ||
Stack []map[string]string | ||
Message string | ||
Kind string | ||
} | ||
|
||
func (obj DatadogErrorTrackingObject) MarshalZerologObject(e *zerolog.Event) { | ||
e.Interface("stack", obj.Stack). | ||
Str("message", obj.Message). | ||
Str("kind", obj.Kind) | ||
} | ||
|
||
// GetServerContextFromCmd returns a Context from a command or an empty Context | ||
// if it has not been set. | ||
func GetServerContextFromCmd(cmd *cobra.Command) *Context { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the default in the library so we don't need to do it.
https://pkg.go.dev/github.com/rs/zerolog#section-readme