From e3a961623b2baa204bb558a1085f2cc7343a223a Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Fri, 10 Jan 2025 16:58:26 +0100 Subject: [PATCH] feat(logging): add new field log_type with access or application as values --- pkg/modules/api/middlewares.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/pkg/modules/api/middlewares.go b/pkg/modules/api/middlewares.go index 78bcb854..f9354510 100644 --- a/pkg/modules/api/middlewares.go +++ b/pkg/modules/api/middlewares.go @@ -161,9 +161,12 @@ func loggerMiddleware(logger *zap.Logger, disableLoggingForPaths []string) echo. 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.Named(func() string { + // Create the application logger and add it to our locals. + appLogger := logger. + With(zap.String("log_type", "application")). + With(zap.String("trace", trace)) + + c.Set("logger", appLogger.Named(func() string { return strings.ReplaceAll( strings.ReplaceAll(c.Request().URL.Path, rootPath, ""), "/", @@ -177,6 +180,11 @@ func loggerMiddleware(logger *zap.Logger, disableLoggingForPaths []string) echo. c.Error(err) } + // Create the access logger. + accessLogger := logger. + With(zap.String("log_type", "access")). + With(zap.String("trace", trace)) + for _, path := range disableLoggingForPaths { URI := fmt.Sprintf("%s%s", rootPath, path) @@ -212,9 +220,9 @@ func loggerMiddleware(logger *zap.Logger, disableLoggingForPaths []string) echo. fields[11] = zap.Int64("bytes_out", c.Response().Size) if err != nil { - reqLogger.Error(err.Error(), fields...) + accessLogger.Error(err.Error(), fields...) } else { - reqLogger.Info("request handled", fields...) + accessLogger.Info("request handled", fields...) } return nil