refactor: migrate server logs to observability
This commit is contained in:
@@ -2,7 +2,6 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
|
||||||
"mal/internal/config"
|
"mal/internal/config"
|
||||||
"mal/internal/observability"
|
"mal/internal/observability"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -41,17 +40,32 @@ func RunServer(cfg config.Config, lifecycle fx.Lifecycle, r *gin.Engine) {
|
|||||||
|
|
||||||
lifecycle.Append(fx.Hook{
|
lifecycle.Append(fx.Hook{
|
||||||
OnStart: func(context.Context) error {
|
OnStart: func(context.Context) error {
|
||||||
log.Printf("Starting server on http://localhost:%s", port)
|
observability.Info(
|
||||||
|
"server_start",
|
||||||
|
"server",
|
||||||
|
"",
|
||||||
|
map[string]any{
|
||||||
|
"port": port,
|
||||||
|
},
|
||||||
|
)
|
||||||
go func() {
|
go func() {
|
||||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
// Avoid exiting the process from a goroutine; let the process supervisor handle restarts.
|
// Avoid exiting the process from a goroutine; let the process supervisor handle restarts.
|
||||||
log.Printf("server listen error: %s", err)
|
observability.Error(
|
||||||
|
"server_listen_error",
|
||||||
|
"server",
|
||||||
|
"",
|
||||||
|
map[string]any{
|
||||||
|
"port": port,
|
||||||
|
},
|
||||||
|
err,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
OnStop: func(ctx context.Context) error {
|
OnStop: func(ctx context.Context) error {
|
||||||
log.Println("Shutting down server...")
|
observability.Info("server_stop", "server", "", nil)
|
||||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
return srv.Shutdown(ctx)
|
return srv.Shutdown(ctx)
|
||||||
|
|||||||
Reference in New Issue
Block a user