Skip to content

Commit aa2b910

Browse files
committed
feat(Logs): Refactor logs to show just one log per request/response pair
1 parent add1885 commit aa2b910

File tree

4 files changed

+14
-11
lines changed

4 files changed

+14
-11
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ logs:
5555
- REQUEST_HEADER:x-real-ip
5656

5757
- RESPONSE:status
58-
- RESPONSE:proto
5958

6059
- RESPONSE_HEADER:content-length
6160
```
@@ -90,7 +89,6 @@ The project includes extensive logging capabilities. The logs can be configured
9089
* REQUEST_HEADER:x-forwarded-for: X-Forwarded-For header of the request.
9190
* REQUEST_HEADER:x-real-ip: X-Real-IP header of the request.
9291
* RESPONSE:status: HTTP status of the response.
93-
* RESPONSE:proto: Protocol of the response.
9492
* RESPONSE_HEADER:content-length: Content-Length header of the response.
9593

9694
> Note:

config/samples/config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ logs:
2121
- REQUEST_HEADER:x-real-ip
2222

2323
- RESPONSE:status
24-
- RESPONSE:proto
2524

2625
- RESPONSE_HEADER:content-length

internal/api/api.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ var (
2121
func PurgeHandler(ctx v1alpha1.Context) func(c *fiber.Ctx) error {
2222
return func(c *fiber.Ctx) error {
2323

24-
if ctx.Config.Logs.ShowAccessLogs {
25-
logFields := GetRequestLogFields(c.Request(), ctx.Config.Logs.AccessLogsFields)
26-
ctx.Logger.Infow("request", logFields...)
27-
}
24+
// Log the request when the function ends
25+
defer LogRequest(c, ctx)
2826

2927
// Parse the JSON body from the request and validate the body
3028
if err := c.BodyParser(&req); err != nil {
@@ -94,10 +92,6 @@ func PurgeHandler(ctx v1alpha1.Context) func(c *fiber.Ctx) error {
9492
})
9593
}
9694

97-
if ctx.Config.Logs.ShowAccessLogs {
98-
logFields := GetResponseLogFields(c.Response(), ctx.Config.Logs.AccessLogsFields)
99-
ctx.Logger.Infow("response", logFields...)
100-
}
10195
defer resp.Body.Close()
10296

10397
// Decode the Akamai response

internal/api/commons.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package api
22

33
import (
4+
"akapurgo/api/v1alpha1"
5+
"github.com/gofiber/fiber/v2"
46
"github.com/valyala/fasthttp"
57
"regexp"
68
"strconv"
@@ -158,3 +160,13 @@ func GetResponseLogFields(resp *fasthttp.Response, configurationFields []string)
158160

159161
return logFields
160162
}
163+
164+
// LogRequest
165+
func LogRequest(c *fiber.Ctx, ctx v1alpha1.Context) {
166+
if ctx.Config.Logs.ShowAccessLogs {
167+
logFieldsReq := GetResponseLogFields(c.Response(), ctx.Config.Logs.AccessLogsFields)
168+
logFieldsResp := GetRequestLogFields(c.Request(), ctx.Config.Logs.AccessLogsFields)
169+
logFields := append(logFieldsReq, logFieldsResp...)
170+
ctx.Logger.Infow("request", logFields...)
171+
}
172+
}

0 commit comments

Comments
 (0)