Skip to content

Commit a39c9da

Browse files
hobostayclaude
authored andcommitted
fix: replace standard log with structured logger in main and recovery middleware
This PR continues the migration to structured logging by replacing the standard Go log package usage with the project's structured logger. Changes: - cmd/server/main.go: Replace all log.Printf, log.Println, log.Fatalf with logger.Infof, logger.Info, logger.Fatalf - internal/middleware/recovery.go: Replace log.Printf with logger.ErrorWithFields for better panic logging with structured data - Remove log.SetFlags and log.SetOutput from main.go as they're no longer needed with the structured logger Benefits: - Consistent logging format across the entire application - Better log parsing and analysis with structured fields - Request ID tracking in recovery middleware for easier debugging - Proper integration with the project's logging configuration Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 84086f5 commit a39c9da

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

cmd/server/main.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ package main
2525
import (
2626
"context"
2727
"fmt"
28-
"log"
2928
"net/http"
3029
"os"
3130
"os/signal"
@@ -36,16 +35,13 @@ import (
3635

3736
"github.com/Tencent/WeKnora/internal/config"
3837
"github.com/Tencent/WeKnora/internal/container"
38+
"github.com/Tencent/WeKnora/internal/logger"
3939
"github.com/Tencent/WeKnora/internal/runtime"
4040
"github.com/Tencent/WeKnora/internal/tracing"
4141
"github.com/Tencent/WeKnora/internal/types/interfaces"
4242
)
4343

4444
func main() {
45-
// Set log format with request ID
46-
log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)
47-
log.SetOutput(os.Stdout)
48-
4945
// Set Gin mode
5046
if os.Getenv("GIN_MODE") == "release" {
5147
gin.SetMode(gin.ReleaseMode)
@@ -87,29 +83,29 @@ func main() {
8783
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
8884
go func() {
8985
sig := <-signals
90-
log.Printf("Received signal: %v, starting server shutdown...", sig)
86+
logger.Infof(context.Background(), "Received signal: %v, starting server shutdown...", sig)
9187

9288
// Create a context with timeout for server shutdown
9389
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 30*time.Second)
9490
defer shutdownCancel()
9591

9692
if err := server.Shutdown(shutdownCtx); err != nil {
97-
log.Fatalf("Server forced to shutdown: %v", err)
93+
logger.Fatalf(context.Background(), "Server forced to shutdown: %v", err)
9894
}
9995

10096
// Clean up all registered resources
101-
log.Println("Cleaning up resources...")
97+
logger.Info(context.Background(), "Cleaning up resources...")
10298
errs := resourceCleaner.Cleanup(cleanupCtx)
10399
if len(errs) > 0 {
104-
log.Printf("Errors occurred during resource cleanup: %v", errs)
100+
logger.Errorf(context.Background(), "Errors occurred during resource cleanup: %v", errs)
105101
}
106102

107-
log.Println("Server has exited")
103+
logger.Info(context.Background(), "Server has exited")
108104
done()
109105
}()
110106

111107
// Start server
112-
log.Printf("Server is running at %s:%d", cfg.Server.Host, cfg.Server.Port)
108+
logger.Infof(context.Background(), "Server is running at %s:%d", cfg.Server.Host, cfg.Server.Port)
113109
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
114110
return fmt.Errorf("failed to start server: %v", err)
115111
}
@@ -119,6 +115,6 @@ func main() {
119115
return nil
120116
})
121117
if err != nil {
122-
log.Fatalf("Failed to run application: %v", err)
118+
logger.Fatalf(context.Background(), "Failed to run application: %v", err)
123119
}
124120
}

internal/middleware/recovery.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package middleware
22

33
import (
44
"fmt"
5-
"log"
65
"runtime/debug"
76

7+
"github.com/sirupsen/logrus"
8+
"github.com/Tencent/WeKnora/internal/logger"
89
"github.com/gin-gonic/gin"
910
)
1011

@@ -13,13 +14,17 @@ func Recovery() gin.HandlerFunc {
1314
return func(c *gin.Context) {
1415
defer func() {
1516
if err := recover(); err != nil {
16-
// Get request ID
17+
// Get request ID from context
18+
ctx := c.Request.Context()
1719
requestID, _ := c.Get("RequestID")
1820

1921
// Print stacktrace
2022
stacktrace := debug.Stack()
21-
// Log error
22-
log.Printf("[PANIC] %s | %v | %s", requestID, err, stacktrace)
23+
// Log error with structured logger
24+
logger.ErrorWithFields(ctx, fmt.Errorf("panic: %v", err), logrus.Fields{
25+
"request_id": requestID,
26+
"stacktrace": string(stacktrace),
27+
})
2328

2429
// 返回500错误
2530
c.AbortWithStatusJSON(500, gin.H{

0 commit comments

Comments
 (0)