From f376cd5ab1445f070f5ea8f24185664082d2be14 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Wed, 8 Dec 2021 15:41:08 +0100 Subject: [PATCH] feat: improve 'logger' field from logger --- pkg/gotenberg/cmd.go | 4 ++-- pkg/modules/api/middlewares.go | 11 +++++++++-- pkg/modules/chromium/chromium.go | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/pkg/gotenberg/cmd.go b/pkg/gotenberg/cmd.go index 9788dbab..fa9d1a25 100644 --- a/pkg/gotenberg/cmd.go +++ b/pkg/gotenberg/cmd.go @@ -31,7 +31,7 @@ func Command(logger *zap.Logger, binPath string, args ...string) Cmd { return Cmd{ ctx: nil, - logger: logger.Named("cmd"), + logger: logger.Named(strings.ReplaceAll(binPath, "/", "")), process: cmd, } } @@ -51,7 +51,7 @@ func CommandContext(ctx context.Context, logger *zap.Logger, binPath string, arg return Cmd{ ctx: ctx, - logger: logger.Named("cmd"), + logger: logger.Named(strings.ReplaceAll(binPath, "/", "")), process: cmd, }, nil } diff --git a/pkg/modules/api/middlewares.go b/pkg/modules/api/middlewares.go index c2c818e1..fa0d0855 100644 --- a/pkg/modules/api/middlewares.go +++ b/pkg/modules/api/middlewares.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "net/http" + "strings" "time" "github.com/google/uuid" @@ -147,10 +148,17 @@ func loggerMiddleware(logger *zap.Logger, disableLoggingForPaths []string) echo. return func(c echo.Context) error { startTime := c.Get("startTime").(time.Time) trace := c.Get("trace").(string) + rootPath := c.Get("rootPath").(string) // Create the request logger and add it to our locals. reqLogger := logger.With(zap.String("trace", trace)) - c.Set("logger", reqLogger) + c.Set("logger", reqLogger.Named(func() string { + return strings.ReplaceAll( + strings.ReplaceAll(c.Request().URL.Path, rootPath, ""), + "/", + "", + ) + }())) // Call the next middleware in the chain. err := next(c) @@ -159,7 +167,6 @@ func loggerMiddleware(logger *zap.Logger, disableLoggingForPaths []string) echo. } for _, path := range disableLoggingForPaths { - rootPath := c.Get("rootPath").(string) URI := fmt.Sprintf("%s%s", rootPath, path) if c.Request().RequestURI == URI { diff --git a/pkg/modules/chromium/chromium.go b/pkg/modules/chromium/chromium.go index 6b10181c..bac36feb 100644 --- a/pkg/modules/chromium/chromium.go +++ b/pkg/modules/chromium/chromium.go @@ -355,7 +355,7 @@ func (mod Chromium) Routes() ([]api.Route, error) { // drastically. In such a scenario, the given context may also be done before // the end of the conversion. func (mod Chromium) PDF(ctx context.Context, logger *zap.Logger, URL, outputPath string, options Options) error { - debug := debugLogger{logger: logger.Named("chromium.debug")} + debug := debugLogger{logger: logger.Named("browser")} userProfileDirPath := gotenberg.NewDirPath() args := append(chromedp.DefaultExecAllocatorOptions[:],