v0.0.474 Add ZeroLogger config field to exerr.Init to override used zerolog instance
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m27s

This commit is contained in:
Mike Schwörer 2024-06-14 23:18:58 +02:00
parent fc4bed4b9f
commit d2f2a0558a
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
3 changed files with 32 additions and 21 deletions

View File

@ -55,18 +55,6 @@ import (
// => Wrap/New + Fatal // => Wrap/New + Fatal
// //
var stackSkipLogger zerolog.Logger
func init() {
cw := zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: "2006-01-02 15:04:05 Z07:00",
}
multi := zerolog.MultiLevelWriter(cw)
stackSkipLogger = zerolog.New(multi).With().Timestamp().CallerWithSkipFrameCount(4).Logger()
}
type Builder struct { type Builder struct {
wrappedErr error wrappedErr error
errorData *ExErr errorData *ExErr
@ -435,9 +423,9 @@ func (b *Builder) Build(ctxs ...context.Context) error {
} }
if pkgconfig.ZeroLogErrTraces && !b.noLog && (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) { if pkgconfig.ZeroLogErrTraces && !b.noLog && (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) {
b.errorData.ShortLog(stackSkipLogger.Error()) b.errorData.ShortLog(pkgconfig.ZeroLogger.Error())
} else if pkgconfig.ZeroLogAllTraces && !b.noLog { } else if pkgconfig.ZeroLogAllTraces && !b.noLog {
b.errorData.ShortLog(stackSkipLogger.Error()) b.errorData.ShortLog(pkgconfig.ZeroLogger.Error())
} }
b.errorData.CallListener(MethodBuild) b.errorData.CallListener(MethodBuild)
@ -459,9 +447,9 @@ func (b *Builder) Output(ctx context.Context, g *gin.Context) {
b.errorData.Output(g) b.errorData.Output(g)
if (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) && (pkgconfig.ZeroLogErrGinOutput || pkgconfig.ZeroLogAllGinOutput) { if (b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal) && (pkgconfig.ZeroLogErrGinOutput || pkgconfig.ZeroLogAllGinOutput) {
b.errorData.Log(stackSkipLogger.Error()) b.errorData.Log(pkgconfig.ZeroLogger.Error())
} else if (b.errorData.Severity == SevWarn) && (pkgconfig.ZeroLogAllGinOutput) { } else if (b.errorData.Severity == SevWarn) && (pkgconfig.ZeroLogAllGinOutput) {
b.errorData.Log(stackSkipLogger.Warn()) b.errorData.Log(pkgconfig.ZeroLogger.Warn())
} }
b.errorData.CallListener(MethodOutput) b.errorData.CallListener(MethodOutput)
@ -477,9 +465,9 @@ func (b *Builder) Print(ctxs ...context.Context) {
} }
if b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal { if b.errorData.Severity == SevErr || b.errorData.Severity == SevFatal {
b.errorData.Log(stackSkipLogger.Error()) b.errorData.Log(pkgconfig.ZeroLogger.Error())
} else if b.errorData.Severity == SevWarn { } else if b.errorData.Severity == SevWarn {
b.errorData.ShortLog(stackSkipLogger.Warn()) b.errorData.ShortLog(pkgconfig.ZeroLogger.Warn())
} }
b.errorData.CallListener(MethodPrint) b.errorData.CallListener(MethodPrint)
@ -499,7 +487,7 @@ func (b *Builder) Fatal(ctxs ...context.Context) {
b.CtxData(MethodFatal, dctx) b.CtxData(MethodFatal, dctx)
} }
b.errorData.Log(stackSkipLogger.WithLevel(zerolog.FatalLevel)) b.errorData.Log(pkgconfig.ZeroLogger.WithLevel(zerolog.FatalLevel))
b.errorData.CallListener(MethodFatal) b.errorData.CallListener(MethodFatal)

View File

@ -4,8 +4,10 @@ import (
"context" "context"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
"net/http" "net/http"
"os"
) )
type ErrorPackageConfig struct { type ErrorPackageConfig struct {
@ -21,6 +23,7 @@ type ErrorPackageConfig struct {
ZeroLogAllGinOutput bool // autom print zerolog logs on ginext.Error() / .Output(gin) (for all Severities) ZeroLogAllGinOutput bool // autom print zerolog logs on ginext.Error() / .Output(gin) (for all Severities)
ExtendGinMeta func(ctx context.Context, b *Builder, g *gin.Context, req *http.Request) // (Optionally) extend the final error meta values with additional data from the gin context (a few are automatically added, here more can be included) ExtendGinMeta func(ctx context.Context, b *Builder, g *gin.Context, req *http.Request) // (Optionally) extend the final error meta values with additional data from the gin context (a few are automatically added, here more can be included)
ExtendContextMeta func(b *Builder, method Method, dctx context.Context) // (Optionally) extend the final error meta values with additional data from the context (a few are automatically added, here more can be included) ExtendContextMeta func(b *Builder, method Method, dctx context.Context) // (Optionally) extend the final error meta values with additional data from the context (a few are automatically added, here more can be included)
ZeroLogger zerolog.Logger // The logger used to print exerr log messages
} }
type ErrorPackageConfigInit struct { type ErrorPackageConfigInit struct {
@ -36,6 +39,7 @@ type ErrorPackageConfigInit struct {
ZeroLogAllGinOutput *bool ZeroLogAllGinOutput *bool
ExtendGinMeta func(ctx context.Context, b *Builder, g *gin.Context, req *http.Request) ExtendGinMeta func(ctx context.Context, b *Builder, g *gin.Context, req *http.Request)
ExtendContextMeta func(b *Builder, method Method, dctx context.Context) ExtendContextMeta func(b *Builder, method Method, dctx context.Context)
ZeroLogger *zerolog.Logger
} }
var initialized = false var initialized = false
@ -81,6 +85,13 @@ func Init(cfg ErrorPackageConfigInit) {
egcm = cfg.ExtendContextMeta egcm = cfg.ExtendContextMeta
} }
var logger zerolog.Logger
if cfg.ZeroLogger != nil {
logger = *cfg.ZeroLogger
} else {
logger = newDefaultLogger()
}
pkgconfig = ErrorPackageConfig{ pkgconfig = ErrorPackageConfig{
ZeroLogErrTraces: langext.Coalesce(cfg.ZeroLogErrTraces, pkgconfig.ZeroLogErrTraces), ZeroLogErrTraces: langext.Coalesce(cfg.ZeroLogErrTraces, pkgconfig.ZeroLogErrTraces),
ZeroLogAllTraces: langext.Coalesce(cfg.ZeroLogAllTraces, pkgconfig.ZeroLogAllTraces), ZeroLogAllTraces: langext.Coalesce(cfg.ZeroLogAllTraces, pkgconfig.ZeroLogAllTraces),
@ -94,11 +105,23 @@ func Init(cfg ErrorPackageConfigInit) {
ZeroLogErrGinOutput: langext.Coalesce(cfg.ZeroLogErrGinOutput, pkgconfig.ZeroLogErrGinOutput), ZeroLogErrGinOutput: langext.Coalesce(cfg.ZeroLogErrGinOutput, pkgconfig.ZeroLogErrGinOutput),
ExtendGinMeta: egm, ExtendGinMeta: egm,
ExtendContextMeta: egcm, ExtendContextMeta: egcm,
ZeroLogger: logger,
} }
initialized = true initialized = true
} }
func newDefaultLogger() zerolog.Logger {
cw := zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: "2006-01-02 15:04:05 Z07:00",
}
multi := zerolog.MultiLevelWriter(cw)
return zerolog.New(multi).With().Timestamp().CallerWithSkipFrameCount(4).Logger()
}
func Initialized() bool { func Initialized() bool {
return initialized return initialized
} }

View File

@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.473" const GoextVersion = "0.0.474"
const GoextVersionTimestamp = "2024-06-14T17:24:59+0200" const GoextVersionTimestamp = "2024-06-14T23:18:58+0200"