implemented log levels and structured log with slog
Some checks failed
Tests / Run Go Tests (push) Failing after 1m36s
Some checks failed
Tests / Run Go Tests (push) Failing after 1m36s
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user