feat(logging): add new field log_type with access or application as values

This commit is contained in:
Julien Neuhart
2025-01-10 16:58:26 +01:00
parent 1ca27fdba2
commit e3a961623b

View File

@@ -161,9 +161,12 @@ func loggerMiddleware(logger *zap.Logger, disableLoggingForPaths []string) echo.
trace := c.Get("trace").(string) trace := c.Get("trace").(string)
rootPath := c.Get("rootPath").(string) rootPath := c.Get("rootPath").(string)
// Create the request logger and add it to our locals. // Create the application logger and add it to our locals.
reqLogger := logger.With(zap.String("trace", trace)) appLogger := logger.
c.Set("logger", reqLogger.Named(func() string { With(zap.String("log_type", "application")).
With(zap.String("trace", trace))
c.Set("logger", appLogger.Named(func() string {
return strings.ReplaceAll( return strings.ReplaceAll(
strings.ReplaceAll(c.Request().URL.Path, rootPath, ""), strings.ReplaceAll(c.Request().URL.Path, rootPath, ""),
"/", "/",
@@ -177,6 +180,11 @@ func loggerMiddleware(logger *zap.Logger, disableLoggingForPaths []string) echo.
c.Error(err) c.Error(err)
} }
// Create the access logger.
accessLogger := logger.
With(zap.String("log_type", "access")).
With(zap.String("trace", trace))
for _, path := range disableLoggingForPaths { for _, path := range disableLoggingForPaths {
URI := fmt.Sprintf("%s%s", rootPath, path) 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) fields[11] = zap.Int64("bytes_out", c.Response().Size)
if err != nil { if err != nil {
reqLogger.Error(err.Error(), fields...) accessLogger.Error(err.Error(), fields...)
} else { } else {
reqLogger.Info("request handled", fields...) accessLogger.Info("request handled", fields...)
} }
return nil return nil