From 66bae956b406d10d5a5ea3373e1da2e4d45c4ff8 Mon Sep 17 00:00:00 2001 From: Nicola Iacovelli Date: Tue, 22 Oct 2024 16:07:34 +0100 Subject: [PATCH] Adding some logs --- foodDetails/controller/FoodDetailsController.go | 4 ++++ foodDetails/service/FoodDetailsService.go | 9 ++++++--- go.mod | 1 + go.sum | 2 ++ 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/foodDetails/controller/FoodDetailsController.go b/foodDetails/controller/FoodDetailsController.go index 223f110..cb7d3b2 100644 --- a/foodDetails/controller/FoodDetailsController.go +++ b/foodDetails/controller/FoodDetailsController.go @@ -2,6 +2,7 @@ package controller import ( foodDetailsService "food-details-integrator-be/foodDetails/service" + "github.com/apsystole/log" "github.com/gin-gonic/gin" "net/http" "strconv" @@ -27,6 +28,7 @@ func (f foodDetailsController) GetFoodDetails(c *gin.Context) { result, err := f.foodDetailsService.GetProductDetails(barcode) if err != nil { + log.Error(err) c.JSON(http.StatusBadRequest, gin.H{ "error": err.Error(), }) @@ -43,12 +45,14 @@ func (f foodDetailsController) GetKcalForQuantity(c *gin.Context) { quantityConverted, errParse := strconv.ParseFloat(quantity, 64) if errParse != nil { + log.Error(errParse) c.JSON(http.StatusBadRequest, gin.H{ "error": errParse.Error(), }) } result, err := f.foodDetailsService.GetKcalForFoodQuantity(barcode, quantityConverted, unit) if err != nil { + log.Error(err) c.JSON(http.StatusBadRequest, gin.H{ "error": err.Error(), }) diff --git a/foodDetails/service/FoodDetailsService.go b/foodDetails/service/FoodDetailsService.go index 54fe127..a419d69 100644 --- a/foodDetails/service/FoodDetailsService.go +++ b/foodDetails/service/FoodDetailsService.go @@ -1,8 +1,8 @@ package service import ( - "fmt" "food-details-integrator-be/foodDetails/model" + "github.com/apsystole/log" "github.com/mashingan/smapping" "github.com/openfoodfacts/openfoodfacts-go" "os" @@ -33,10 +33,11 @@ func (s FoodDetailsService) GetProductDetails(barcode string) (model.FoodDetails if err != nil { foodDetails, err = s.getFoodDetailsFromApi(barcode) } else { - fmt.Println("cache hit") + log.Infoln("Data from cache 🚀for barcode: ", barcode) } err = cs.SetObject(barcode, foodDetails) } else { + log.Infoln("Redis is disabled, fetching data from API") foodDetails, err = s.getFoodDetailsFromApi(barcode) } return foodDetails, err @@ -59,6 +60,8 @@ func (s FoodDetailsService) getFoodDetailsFromApi(barcode string) (model.FoodDet api := openfoodfacts.NewClient("world", "", "") foodDetails := model.FoodDetails{} + log.Infoln("Fetching data from API for barcode: ", barcode) + if os.Getenv("IS_SANDBOX") == "true" { api.Sandbox() } @@ -66,7 +69,7 @@ func (s FoodDetailsService) getFoodDetailsFromApi(barcode string) (model.FoodDet product, err := api.Product(barcode) if err != nil { - fmt.Printf("error %s \n", err) + log.Errorf("Error while fetching data from API for barcode: %s, error: %s", barcode, err) return model.FoodDetails{}, err } else { mappedField := smapping.MapTags(product, "json") diff --git a/go.mod b/go.mod index e8b31b5..b82e1e7 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module food-details-integrator-be go 1.18 require ( + github.com/apsystole/log v0.3.0 github.com/gin-gonic/gin v1.8.1 github.com/go-redis/cache/v8 v8.4.3 github.com/go-redis/redis/v8 v8.11.3 diff --git a/go.sum b/go.sum index e84ff61..8bd0e7f 100644 --- a/go.sum +++ b/go.sum @@ -14,6 +14,8 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= +github.com/apsystole/log v0.3.0 h1:uES5wJvdvzL6Q4KXJt6OQnDtmPCbohFkdx6v7OpbDC4= +github.com/apsystole/log v0.3.0/go.mod h1:BBvif4d0jOPNYgXzNLVdWE//WjefJ9Hwfy/2Y5U+SV0= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=