Skip to content

Commit

Permalink
revert trace removal
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed Oct 15, 2024
1 parent dd6d7f1 commit 232c7ed
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions log/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/bytedance/sonic v1.12.3
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.33.0
)

Expand Down
1 change: 1 addition & 0 deletions log/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
10 changes: 10 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"io"

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

func init() {
Expand Down Expand Up @@ -112,6 +114,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
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,
Hooks: nil,
}
Expand All @@ -22,6 +23,7 @@ type Config struct {
Filter FilterFunc
OutputJSON bool
Color bool
StackTrace bool
TimeFormat string
Hooks []zerolog.Hook
}
Expand Down Expand Up @@ -81,6 +83,13 @@ func TimeFormatOption(format string) Option {
}
}

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

// HooksOption append hooks to the Logger hooks
func HooksOption(hooks ...zerolog.Hook) Option {
return func(cfg *Config) {
Expand Down

0 comments on commit 232c7ed

Please sign in to comment.