feat: control over logging in debug mode and control logging for specific actions

This commit is contained in:
jonaustin09
2023-06-20 19:39:58 +04:00
parent 0209ca4bc0
commit c01d3ed542
5 changed files with 69 additions and 44 deletions

View File

@@ -113,6 +113,7 @@ func (c S3ApiController) GetActions(ctx *fiber.Ctx) error {
return SendResponse(ctx, err)
}
ctx.Locals("logResBody", false)
res, err := c.be.GetObject(bucket, key, acceptRange, ctx.Response().BodyWriter())
if err != nil {
return SendResponse(ctx, err)
@@ -333,6 +334,7 @@ func (c S3ApiController) PutActions(ctx *fiber.Ctx) error {
}
body := io.ReadSeeker(bytes.NewReader([]byte(ctx.Body())))
ctx.Locals("logReqBody", false)
etag, err := c.be.PutObjectPart(bucket, keyStart, uploadId,
partNumber, contentLength, body)
ctx.Response().Header.Set("Etag", etag)
@@ -381,6 +383,7 @@ func (c S3ApiController) PutActions(ctx *fiber.Ctx) error {
return SendResponse(ctx, err)
}
ctx.Locals("logReqBody", false)
etag, err := c.be.PutObject(&s3.PutObjectInput{
Bucket: &bucket,
Key: &keyStart,
@@ -636,12 +639,10 @@ func (c S3ApiController) CreateActions(ctx *fiber.Ctx) error {
}
func SendResponse(ctx *fiber.Ctx, err error) error {
utils.LogPathParams(ctx)
if err != nil {
serr, ok := err.(s3err.APIError)
if ok {
ctx.Status(serr.HTTPStatusCode)
log.Printf("%s: %v", serr.Code, err)
return ctx.Send(s3err.GetAPIErrorResponse(serr, "", "", ""))
}
@@ -651,7 +652,7 @@ func SendResponse(ctx *fiber.Ctx, err error) error {
s3err.GetAPIError(s3err.ErrInternalError), "", "", ""))
}
utils.LogResponseHeaders(&ctx.Response().Header)
utils.LogCtxDetails(ctx, []byte{})
// https://github.com/gofiber/fiber/issues/2080
// ctx.SendStatus() sets incorrect content length on HEAD request
@@ -660,12 +661,10 @@ func SendResponse(ctx *fiber.Ctx, err error) error {
}
func SendXMLResponse(ctx *fiber.Ctx, resp any, err error) error {
utils.LogPathParams(ctx)
if err != nil {
serr, ok := err.(s3err.APIError)
if ok {
ctx.Status(serr.HTTPStatusCode)
log.Printf("%s: %v", serr.Code, err)
return ctx.Send(s3err.GetAPIErrorResponse(serr, "", "", ""))
}
@@ -688,9 +687,7 @@ func SendXMLResponse(ctx *fiber.Ctx, resp any, err error) error {
}
}
utils.LogResponseHeaders(&ctx.Response().Header)
fmt.Println()
log.Printf("Response Body: %s", b)
utils.LogCtxDetails(ctx, b)
return ctx.Send(b)
}

View File

@@ -97,6 +97,7 @@ func TestS3ApiController_ListBuckets(t *testing.T) {
app.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
app.Get("/", s3ApiController.ListBuckets)
@@ -117,6 +118,7 @@ func TestS3ApiController_ListBuckets(t *testing.T) {
appErr.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
appErr.Get("/", s3ApiControllerErr.ListBuckets)
@@ -190,6 +192,7 @@ func TestS3ApiController_GetActions(t *testing.T) {
app.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
app.Get("/:bucket/:key/*", s3ApiController.GetActions)
@@ -300,6 +303,7 @@ func TestS3ApiController_ListActions(t *testing.T) {
app.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
@@ -320,6 +324,7 @@ func TestS3ApiController_ListActions(t *testing.T) {
appError.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
appError.Get("/:bucket", s3ApiControllerError.ListActions)
@@ -415,6 +420,7 @@ func TestS3ApiController_PutBucketActions(t *testing.T) {
app.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
app.Put("/:bucket", s3ApiController.PutBucketActions)
@@ -504,6 +510,7 @@ func TestS3ApiController_PutActions(t *testing.T) {
app.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
app.Put("/:bucket/:key/*", s3ApiController.PutActions)
@@ -636,6 +643,7 @@ func TestS3ApiController_DeleteBucket(t *testing.T) {
app.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
@@ -658,6 +666,7 @@ func TestS3ApiController_DeleteBucket(t *testing.T) {
appErr.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
appErr.Delete("/:bucket", s3ApiControllerErr.DeleteBucket)
@@ -721,6 +730,7 @@ func TestS3ApiController_DeleteObjects(t *testing.T) {
app.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
app.Post("/:bucket", s3ApiController.DeleteObjects)
@@ -793,6 +803,7 @@ func TestS3ApiController_DeleteActions(t *testing.T) {
app.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
app.Delete("/:bucket/:key/*", s3ApiController.DeleteActions)
@@ -812,6 +823,7 @@ func TestS3ApiController_DeleteActions(t *testing.T) {
appErr.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
appErr.Delete("/:bucket", s3ApiControllerErr.DeleteBucket)
@@ -884,6 +896,7 @@ func TestS3ApiController_HeadBucket(t *testing.T) {
app.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
@@ -905,6 +918,7 @@ func TestS3ApiController_HeadBucket(t *testing.T) {
appErr.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
@@ -982,6 +996,7 @@ func TestS3ApiController_HeadObject(t *testing.T) {
app.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
app.Head("/:bucket/:key/*", s3ApiController.HeadObject)
@@ -1003,6 +1018,7 @@ func TestS3ApiController_HeadObject(t *testing.T) {
appErr.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
appErr.Head("/:bucket/:key/*", s3ApiControllerErr.HeadObject)
@@ -1071,6 +1087,7 @@ func TestS3ApiController_CreateActions(t *testing.T) {
app.Use(func(ctx *fiber.Ctx) error {
ctx.Locals("access", "valid access")
ctx.Locals("isRoot", true)
ctx.Locals("isDebug", false)
return ctx.Next()
})
app.Post("/:bucket/:key/*", s3ApiController.CreateActions)