From 073aa84dd4b803ffce45055d0ef09de68a95565d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Fri, 13 Jun 2025 16:53:24 +0200 Subject: [PATCH] v0.0.579 fix StackSkip count on exerr zero-logger for Build() --- exerr/builder.go | 28 ++++++++++++++++++++++++---- exerr/errinit.go | 4 ++-- goextVersion.go | 4 ++-- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/exerr/builder.go b/exerr/builder.go index 82372e2..3224f9e 100644 --- a/exerr/builder.go +++ b/exerr/builder.go @@ -5,12 +5,12 @@ import ( "context" "encoding/json" "fmt" - "github.com/gin-gonic/gin" - "github.com/rs/zerolog" - "go.mongodb.org/mongo-driver/bson/primitive" "git.blackforestbytes.com/BlackForestBytes/goext/dataext" "git.blackforestbytes.com/BlackForestBytes/goext/enums" "git.blackforestbytes.com/BlackForestBytes/goext/langext" + "github.com/gin-gonic/gin" + "github.com/rs/zerolog" + "go.mongodb.org/mongo-driver/bson/primitive" "net/http" "os" "runtime/debug" @@ -445,6 +445,8 @@ func (b *Builder) BuildAsExerr(ctxs ...context.Context) *ExErr { // The error also gets printed to stdout/stderr // If the error is SevErr|SevFatal we also send it to the error-service func (b *Builder) Output(ctx context.Context, g *gin.Context) { + warnOnPkgConfigNotInitialized() + if !b.containsGinData && g.Request != nil { // Auto-Add gin metadata if the caller hasn't already done it b.GinReq(ctx, g, g.Request) @@ -452,6 +454,12 @@ func (b *Builder) Output(ctx context.Context, g *gin.Context) { b.CtxData(MethodOutput, ctx) + // this is only here to add one level to the trace + // so that .Build() and .Output() and .Print() have the same depth and our stack-skip logger can have the same skip-count + b.doOutput(ctx, g) +} + +func (b *Builder) doOutput(ctx context.Context, g *gin.Context) { b.errorData.Output(g) if (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) && (pkgconfig.ZeroLogErrGinOutput || pkgconfig.ZeroLogAllGinOutput) { @@ -472,6 +480,12 @@ func (b *Builder) Print(ctxs ...context.Context) Proxy { b.CtxData(MethodPrint, dctx) } + // this is only here to add one level to the trace + // so that .Build() and .Output() and .Print() have the same depth and our stack-skip logger can have the same skip-count + return b.doPrint() +} + +func (b *Builder) doPrint() Proxy { if b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal { b.errorData.Log(pkgconfig.ZeroLogger.Error()) } else if b.errorData.Severity == SevWarn { @@ -501,13 +515,19 @@ func (b *Builder) Fatal(ctxs ...context.Context) { b.CtxData(MethodFatal, dctx) } - b.errorData.Log(pkgconfig.ZeroLogger.WithLevel(zerolog.FatalLevel)) + // this is only here to add one level to the trace + // so that .Build() and .Output() and .Print() have the same depth and our stack-skip logger can have the same skip-count + b.doLogFatal() b.errorData.CallListener(MethodFatal, ListenerOpt{NoLog: b.noLog}) os.Exit(1) } +func (b *Builder) doLogFatal() { + b.errorData.Log(pkgconfig.ZeroLogger.WithLevel(zerolog.FatalLevel)) +} + // ---------------------------------------------------------------------------- func (b *Builder) addMeta(key string, mdtype metaDataType, val interface{}) *Builder { diff --git a/exerr/errinit.go b/exerr/errinit.go index 018486f..4da7f3e 100644 --- a/exerr/errinit.go +++ b/exerr/errinit.go @@ -3,9 +3,9 @@ package exerr import ( "context" "fmt" + "git.blackforestbytes.com/BlackForestBytes/goext/langext" "github.com/gin-gonic/gin" "github.com/rs/zerolog" - "git.blackforestbytes.com/BlackForestBytes/goext/langext" "net/http" "os" ) @@ -119,7 +119,7 @@ func newDefaultLogger() zerolog.Logger { multi := zerolog.MultiLevelWriter(cw) - return zerolog.New(multi).With().Timestamp().CallerWithSkipFrameCount(4).Logger() + return zerolog.New(multi).With().Timestamp().CallerWithSkipFrameCount(5).Logger() } func Initialized() bool { diff --git a/goextVersion.go b/goextVersion.go index 90e3ac6..124bd1c 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.578" +const GoextVersion = "0.0.579" -const GoextVersionTimestamp = "2025-06-11T14:37:51+0200" +const GoextVersionTimestamp = "2025-06-13T16:53:24+0200"