From 232c7ed4c1152931fcaf230201d73f75e3ccb2dd Mon Sep 17 00:00:00 2001 From: marbar3778 Date: Tue, 15 Oct 2024 16:15:18 +0200 Subject: [PATCH] revert trace removal --- log/go.mod | 1 + log/go.sum | 1 + log/logger.go | 10 ++++++++++ log/options.go | 9 +++++++++ 4 files changed, 21 insertions(+) diff --git a/log/go.mod b/log/go.mod index c3202e1d72cf..292e5d973e6f 100644 --- a/log/go.mod +++ b/log/go.mod @@ -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 ) diff --git a/log/go.sum b/log/go.sum index 700f4d290c52..b6a141763c97 100644 --- a/log/go.sum +++ b/log/go.sum @@ -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= diff --git a/log/logger.go b/log/logger.go index 1660c1518a51..ef9928029edb 100644 --- a/log/logger.go +++ b/log/logger.go @@ -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() { @@ -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() } diff --git a/log/options.go b/log/options.go index c6b6076a4cf8..28cfefee2e80 100644 --- a/log/options.go +++ b/log/options.go @@ -12,6 +12,7 @@ var defaultConfig = Config{ Filter: nil, OutputJSON: false, Color: true, + StackTrace: false, TimeFormat: time.Kitchen, Hooks: nil, } @@ -22,6 +23,7 @@ type Config struct { Filter FilterFunc OutputJSON bool Color bool + StackTrace bool TimeFormat string Hooks []zerolog.Hook } @@ -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) {