File tree Expand file tree Collapse file tree 2 files changed +51
-1
lines changed Expand file tree Collapse file tree 2 files changed +51
-1
lines changed Original file line number Diff line number Diff line change @@ -48,7 +48,7 @@ func RunApi(cmd *cobra.Command, args []string) {
48
48
defer stop ()
49
49
50
50
r := gin .New ()
51
- r .Use (gin .Logger ())
51
+ r .Use (middleware .Logger ())
52
52
r .Use (gin .Recovery ())
53
53
54
54
// Add Swagger route
Original file line number Diff line number Diff line change
1
+ package middleware
2
+
3
+ import (
4
+ "time"
5
+
6
+ "github.com/gin-gonic/gin"
7
+ "github.com/rs/zerolog/log"
8
+ )
9
+
10
+ // Logger returns a gin.HandlerFunc (middleware) that logs requests using zerolog.
11
+ func Logger () gin.HandlerFunc {
12
+ return func (c * gin.Context ) {
13
+ // Start timer
14
+ start := time .Now ()
15
+ path := c .Request .URL .Path
16
+ raw := c .Request .URL .RawQuery
17
+
18
+ // Process request
19
+ c .Next ()
20
+
21
+ // Stop timer
22
+ end := time .Now ()
23
+ latency := end .Sub (start )
24
+
25
+ // Get status code
26
+ statusCode := c .Writer .Status ()
27
+
28
+ // Get client IP
29
+ clientIP := c .ClientIP ()
30
+
31
+ // Get method
32
+ method := c .Request .Method
33
+
34
+ // Get error message if any
35
+ var errorMessage string
36
+ if len (c .Errors ) > 0 {
37
+ errorMessage = c .Errors .String ()
38
+ }
39
+
40
+ log .Debug ().
41
+ Str ("path" , path ).
42
+ Str ("raw" , raw ).
43
+ Int ("status" , statusCode ).
44
+ Str ("method" , method ).
45
+ Str ("ip" , clientIP ).
46
+ Dur ("latency" , latency ).
47
+ Str ("error" , errorMessage ).
48
+ Msg ("incoming request" )
49
+ }
50
+ }
You can’t perform that action at this time.
0 commit comments