From 5001f24d9b62aae86b752e35a7a1d5e0f5ecb8cb Mon Sep 17 00:00:00 2001 From: tom Date: Mon, 13 Oct 2025 22:33:48 +0200 Subject: [PATCH] implemented log levels and structured log with slog --- Backend/endpoints/pdf.go | 2 +- Backend/main.go | 24 ++++++++++++---------- Backend/templates/headerComponent_templ.go | 2 +- Backend/templates/pages_templ.go | 2 +- Backend/templates/pdf_templ.go | 2 +- Backend/templates/teamComponents_templ.go | 2 +- Backend/templates/timeComponents_templ.go | 2 +- Backend/templates/timePage_templ.go | 2 +- Makefile | 2 +- 9 files changed, 21 insertions(+), 19 deletions(-) diff --git a/Backend/endpoints/pdf.go b/Backend/endpoints/pdf.go index 87beb06..f3ebc12 100644 --- a/Backend/endpoints/pdf.go +++ b/Backend/endpoints/pdf.go @@ -11,7 +11,7 @@ import ( func PDFHandler(w http.ResponseWriter, r *http.Request) { helper.RequiresLogin(Session, w, r) - startDate, err := parseTimestamp(r, "start", time.Now().Format("2006-01-02")) + startDate, err := parseTimestamp(r, "start_date", time.Now().Format("2006-01-02")) if err != nil { log.Println("Error parsing 'start_date' time", err) http.Error(w, "Timestamp 'start_date' cannot be parsed!", http.StatusBadRequest) diff --git a/Backend/main.go b/Backend/main.go index 35395e2..15d4a96 100644 --- a/Backend/main.go +++ b/Backend/main.go @@ -5,8 +5,7 @@ import ( "arbeitszeitmessung/helper" "arbeitszeitmessung/models" "context" - "fmt" - "log" + "log/slog" "net/http" "os" "time" @@ -17,23 +16,25 @@ import ( func main() { var err error + var logLevel slog.LevelVar + logLevel.Set(slog.LevelWarn) + + logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: &logLevel})) + slog.SetDefault(logger) err = godotenv.Load(".env") if err != nil { - log.Println("No .env file found in directory!") + slog.Info("No .env file found in directory!") } if helper.GetEnv("GO_ENV", "production") == "debug" { - log.Println("Debug mode enabled") - log.Println("Environment Variables") + logLevel.Set(slog.LevelDebug) envs := os.Environ() - for _, e := range envs { - fmt.Println(e) - } + slog.Debug("Debug mode enabled", "Environment Variables", envs) } models.DB, err = OpenDatabase() if err != nil { - log.Fatal(err) + slog.Error("Error while opening the database", "Error", err) } fs := http.FileServer(http.Dir("./static")) @@ -58,14 +59,15 @@ func main() { serverSessionMiddleware := endpoints.Session.LoadAndSave(server) // starting the http server - fmt.Printf("Server is running at http://localhost:%s\n", helper.GetEnv("EXPOSED_PORT", "8080")) - log.Fatal(http.ListenAndServe(":8080", serverSessionMiddleware)) + slog.Info("Server is running at http://localhost:8080") + slog.Error("Error starting Server", "Error", http.ListenAndServe(":8080", serverSessionMiddleware)) } func ParamsMiddleware(next http.HandlerFunc) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { queryParams := r.URL.Query() ctx := context.WithValue(r.Context(), "urlParams", queryParams) + slog.Debug("ParamsMiddleware added urlParams", slog.Any("urlParams", queryParams)) next.ServeHTTP(w, r.WithContext(ctx)) }) } diff --git a/Backend/templates/headerComponent_templ.go b/Backend/templates/headerComponent_templ.go index aeef6ec..8e59160 100644 --- a/Backend/templates/headerComponent_templ.go +++ b/Backend/templates/headerComponent_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.3.924 +// templ: version: v0.3.943 package templates //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/Backend/templates/pages_templ.go b/Backend/templates/pages_templ.go index 491784c..ad17982 100644 --- a/Backend/templates/pages_templ.go +++ b/Backend/templates/pages_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.3.924 +// templ: version: v0.3.943 package templates //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/Backend/templates/pdf_templ.go b/Backend/templates/pdf_templ.go index 3c73ded..321301b 100644 --- a/Backend/templates/pdf_templ.go +++ b/Backend/templates/pdf_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.3.924 +// templ: version: v0.3.943 package templates //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/Backend/templates/teamComponents_templ.go b/Backend/templates/teamComponents_templ.go index ec539f2..c1d475e 100644 --- a/Backend/templates/teamComponents_templ.go +++ b/Backend/templates/teamComponents_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.3.924 +// templ: version: v0.3.943 package templates //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/Backend/templates/timeComponents_templ.go b/Backend/templates/timeComponents_templ.go index 835b46a..c7ad17e 100644 --- a/Backend/templates/timeComponents_templ.go +++ b/Backend/templates/timeComponents_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.3.924 +// templ: version: v0.3.943 package templates //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/Backend/templates/timePage_templ.go b/Backend/templates/timePage_templ.go index d668290..dd29246 100644 --- a/Backend/templates/timePage_templ.go +++ b/Backend/templates/timePage_templ.go @@ -1,6 +1,6 @@ // Code generated by templ - DO NOT EDIT. -// templ: version: v0.3.924 +// templ: version: v0.3.943 package templates //lint:file-ignore SA4006 This context is only used if a nested component is present. diff --git a/Makefile b/Makefile index 283ab33..58d90fb 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,7 @@ generateFrontend: backend: generateFrontend login_registry - docker buildx build --platform linux/amd64,linux/arm64 -t ${IMAGE_REGISTRY}/${PACKAGE_OWNER}/arbeitszeitmessung:latest Backend --load #--push + docker buildx build --platform linux/amd64,linux/arm64 -t ${IMAGE_REGISTRY}/${PACKAGE_OWNER}/arbeitszeitmessung:latest Backend --push # docker buildx build --platform linux/amd64,linux/arm64 -t ${IMAGE_REGISTRY}/${PACKAGE_OWNER}/arbeitszeitmessung:${GIT_COMMIT} Backend //--push test: