Skip to content

Commit

Permalink
feat(log): add stacktrace display option (#16825)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Jul 4, 2023
1 parent 8344a78 commit 88f2c83
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 2 deletions.
6 changes: 4 additions & 2 deletions log/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Features
* [#15956](https://github.com/cosmos/cosmos-sdk/pull/15956) Introduce an option for enabling error stack trace.

* [#15956](https://github.com/cosmos/cosmos-sdk/pull/15956) Introduce extra options to configure logger.
## [v1.1.0](https://github.com/cosmos/cosmos-sdk/releases/tag/log/v1.1.0) - 2023-04-27

* [#15956](https://github.com/cosmos/cosmos-sdk/pull/15956) Introduce options to configure logger (enable/disable colored output, customize log timestamps).

## [v1.0.0](https://github.com/cosmos/cosmos-sdk/releases/tag/log/v1.0.0) - 2023-03-30

Expand Down
1 change: 1 addition & 0 deletions log/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module cosmossdk.io/log
go 1.20

require (
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.29.1
gotest.tools/v3 v3.5.0
)
Expand Down
1 change: 1 addition & 0 deletions log/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
Expand Down
10 changes: 10 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package log
import (
"io"

"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/pkgerrors"
)

// ModuleKey defines a module logging key.
Expand Down Expand Up @@ -69,6 +71,14 @@ func NewLogger(dst io.Writer, options ...Option) Logger {
}

logger := zerolog.New(output)
if logCfg.StackTrace {
zerolog.ErrorStackMarshaler = func(err error) interface{} {
return pkgerrors.MarshalStack(errors.WithStack(err))
}

logger = logger.With().Stack().Logger()
}

if logCfg.TimeFormat != "" {
logger = logger.With().Timestamp().Logger()
}
Expand Down
32 changes: 32 additions & 0 deletions log/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package log_test

import (
"bytes"
"errors"
"strings"
"testing"

"cosmossdk.io/log"
)

func TestLoggerOptionStackTrace(t *testing.T) {
t.Skip() // todo(@julienrbrt) unskip when https://github.com/rs/zerolog/pull/560 merged

buf := new(bytes.Buffer)
logger := log.NewLogger(buf, log.TraceOption(true), log.ColorOption(false))
logger.Error("this log should be displayed", "error", inner())
if strings.Count(buf.String(), "logger_test.go") != 1 {
t.Fatalf("stack trace not found, got: %s", buf.String())
}
buf.Reset()

logger = log.NewLogger(buf, log.TraceOption(false), log.ColorOption(false))
logger.Error("this log should be displayed", "error", inner())
if strings.Count(buf.String(), "logger_test.go") > 0 {
t.Fatalf("stack trace found, got: %s", buf.String())
}
}

func inner() error {
return errors.New("seems we have an error here")
}
9 changes: 9 additions & 0 deletions log/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var defaultConfig = Config{
Filter: nil,
OutputJSON: false,
Color: true,
StackTrace: false,
TimeFormat: time.Kitchen,
}

Expand All @@ -21,6 +22,7 @@ type Config struct {
Filter FilterFunc
OutputJSON bool
Color bool
StackTrace bool
TimeFormat string
}

Expand Down Expand Up @@ -78,3 +80,10 @@ func TimeFormatOption(format string) Option {
cfg.TimeFormat = format
}
}

// TraceOption add option to enable/disable print of stacktrace on error log
func TraceOption(val bool) Option {
return func(cfg *Config) {
cfg.StackTrace = val
}
}

0 comments on commit 88f2c83

Please sign in to comment.