Add structured logging and centralized error handling
- Initialize slog in the serve command with terminal/file support - Introduce `AppError` with HTTP status for unified service-layer errors - Replace ad-hoc `api.Error` calls with `api.RespondError` - Wrap internal errors with `model.NewInternalError` and add reference IDs - Add `RequestID` middleware and switch router from `gin.Default` to `gin.New`
This commit is contained in:
@@ -3,6 +3,7 @@ package cmd
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
|
||||
"github.com/dhao2001/mygo/internal/app"
|
||||
"github.com/dhao2001/mygo/internal/config"
|
||||
mygolog "github.com/dhao2001/mygo/internal/log"
|
||||
"github.com/dhao2001/mygo/internal/server"
|
||||
)
|
||||
|
||||
@@ -26,6 +28,11 @@ var serveCmd = &cobra.Command{
|
||||
return fmt.Errorf("load config: %w", err)
|
||||
}
|
||||
|
||||
// Set up structured logging before anything else.
|
||||
appLogger := mygolog.NewLogger(cfg.Log)
|
||||
slog.SetDefault(appLogger)
|
||||
slog.Info("mygo server starting")
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user