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
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m27s
This commit is contained in:
parent
fc4bed4b9f
commit
d2f2a0558a
@ -55,18 +55,6 @@ import (
|
||||
// => 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 {
|
||||
wrappedErr error
|
||||
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) {
|
||||
b.errorData.ShortLog(stackSkipLogger.Error())
|
||||
b.errorData.ShortLog(pkgconfig.ZeroLogger.Error())
|
||||
} else if pkgconfig.ZeroLogAllTraces && !b.noLog {
|
||||
b.errorData.ShortLog(stackSkipLogger.Error())
|
||||
b.errorData.ShortLog(pkgconfig.ZeroLogger.Error())
|
||||
}
|
||||
|
||||
b.errorData.CallListener(MethodBuild)
|
||||
@ -459,9 +447,9 @@ func (b *Builder) Output(ctx context.Context, g *gin.Context) {
|
||||
b.errorData.Output(g)
|
||||
|
||||
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) {
|
||||
b.errorData.Log(stackSkipLogger.Warn())
|
||||
b.errorData.Log(pkgconfig.ZeroLogger.Warn())
|
||||
}
|
||||
|
||||
b.errorData.CallListener(MethodOutput)
|
||||
@ -477,9 +465,9 @@ func (b *Builder) Print(ctxs ...context.Context) {
|
||||
}
|
||||
|
||||
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 {
|
||||
b.errorData.ShortLog(stackSkipLogger.Warn())
|
||||
b.errorData.ShortLog(pkgconfig.ZeroLogger.Warn())
|
||||
}
|
||||
|
||||
b.errorData.CallListener(MethodPrint)
|
||||
@ -499,7 +487,7 @@ func (b *Builder) Fatal(ctxs ...context.Context) {
|
||||
b.CtxData(MethodFatal, dctx)
|
||||
}
|
||||
|
||||
b.errorData.Log(stackSkipLogger.WithLevel(zerolog.FatalLevel))
|
||||
b.errorData.Log(pkgconfig.ZeroLogger.WithLevel(zerolog.FatalLevel))
|
||||
|
||||
b.errorData.CallListener(MethodFatal)
|
||||
|
||||
|
@ -4,8 +4,10 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type ErrorPackageConfig struct {
|
||||
@ -21,6 +23,7 @@ type ErrorPackageConfig struct {
|
||||
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)
|
||||
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 {
|
||||
@ -36,6 +39,7 @@ type ErrorPackageConfigInit struct {
|
||||
ZeroLogAllGinOutput *bool
|
||||
ExtendGinMeta func(ctx context.Context, b *Builder, g *gin.Context, req *http.Request)
|
||||
ExtendContextMeta func(b *Builder, method Method, dctx context.Context)
|
||||
ZeroLogger *zerolog.Logger
|
||||
}
|
||||
|
||||
var initialized = false
|
||||
@ -81,6 +85,13 @@ func Init(cfg ErrorPackageConfigInit) {
|
||||
egcm = cfg.ExtendContextMeta
|
||||
}
|
||||
|
||||
var logger zerolog.Logger
|
||||
if cfg.ZeroLogger != nil {
|
||||
logger = *cfg.ZeroLogger
|
||||
} else {
|
||||
logger = newDefaultLogger()
|
||||
}
|
||||
|
||||
pkgconfig = ErrorPackageConfig{
|
||||
ZeroLogErrTraces: langext.Coalesce(cfg.ZeroLogErrTraces, pkgconfig.ZeroLogErrTraces),
|
||||
ZeroLogAllTraces: langext.Coalesce(cfg.ZeroLogAllTraces, pkgconfig.ZeroLogAllTraces),
|
||||
@ -94,11 +105,23 @@ func Init(cfg ErrorPackageConfigInit) {
|
||||
ZeroLogErrGinOutput: langext.Coalesce(cfg.ZeroLogErrGinOutput, pkgconfig.ZeroLogErrGinOutput),
|
||||
ExtendGinMeta: egm,
|
||||
ExtendContextMeta: egcm,
|
||||
ZeroLogger: logger,
|
||||
}
|
||||
|
||||
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 {
|
||||
return initialized
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
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"
|
||||
|
Loading…
Reference in New Issue
Block a user