Repository: iluwatar/java-design-patterns
Issue: Logging in API Gateway
Labels: type: bug
epic: pattern
info: good first issue

At the moment api-gateway pattern does not use a proper logger as in other examples. In this issue we fix that.

Issue comments:
I can work on it, if you would like it to assign it to me?
Thanks @amit1307 please go ahead
Hey, I was trying to start on this one but might need some more info if that's okay.

My understanding of this issue is that we don't have any logger configured for `api-gateway` but after it seems like there is nothing logged at all by any modules under `api-gateway`. So is the requirement here is to add logger as well as the logs or just the logger? Could you please clarify that?
True, there's only a couple of `e.printStackTrace()` currently. I think it would be good to add some basic logging in addition to adding the library. Just so that it's easy to follow the general steps from the output.
Sure, thanks for the info. I should get back in 1-2 days with something.
Hey, have raised the PR to add some logs. Please let me know if you have any feedback on the change.

Pull request review comments:

Patch that resolved the issue:
diff --git a/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java b/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java
index 52dd065ffc35..6fea815fc86d 100644
--- a/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java
+++ b/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/ImageClientImpl.java
@@ -23,11 +23,16 @@
 
 package com.iluwatar.api.gateway;
 
+import static org.slf4j.LoggerFactory.getLogger;
+
 import java.io.IOException;
 import java.net.URI;
 import java.net.http.HttpClient;
 import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
 import java.net.http.HttpResponse.BodyHandlers;
+
+import org.slf4j.Logger;
 import org.springframework.stereotype.Component;
 
 /**
@@ -35,6 +40,8 @@
  */
 @Component
 public class ImageClientImpl implements ImageClient {
+  private static final Logger LOGGER = getLogger(ImageClientImpl.class);
+
   /**
    * Makes a simple HTTP Get request to the Image microservice.
    *
@@ -49,12 +56,26 @@ public String getImagePath() {
         .build();
 
     try {
+      LOGGER.info("Sending request to fetch image path");
       var httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
+      logResponse(httpResponse);
       return httpResponse.body();
     } catch (IOException | InterruptedException e) {
-      e.printStackTrace();
+      LOGGER.error("Failure occurred while getting image path", e);
     }
 
     return null;
   }
+
+  private void logResponse(HttpResponse<String> httpResponse) {
+    if (isSuccessResponse(httpResponse.statusCode())) {
+      LOGGER.info("Image path received successfully");
+    } else {
+      LOGGER.warn("Image path request failed");
+    }
+  }
+
+  private boolean isSuccessResponse(int responseCode) {
+    return responseCode >= 200 && responseCode <= 299;
+  }
 }
diff --git a/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java b/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java
index 0dc44a51bced..f773d0d54c7a 100644
--- a/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java
+++ b/api-gateway/api-gateway-service/src/main/java/com/iluwatar/api/gateway/PriceClientImpl.java
@@ -23,18 +23,26 @@
 
 package com.iluwatar.api.gateway;
 
+import static org.slf4j.LoggerFactory.getLogger;
+
 import java.io.IOException;
 import java.net.URI;
 import java.net.http.HttpClient;
 import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
 import java.net.http.HttpResponse.BodyHandlers;
+
+import org.slf4j.Logger;
 import org.springframework.stereotype.Component;
 
+
 /**
  * An adapter to communicate with the Price microservice.
  */
 @Component
 public class PriceClientImpl implements PriceClient {
+  private static final Logger LOGGER = getLogger(PriceClientImpl.class);
+
   /**
    * Makes a simple HTTP Get request to the Price microservice.
    *
@@ -49,12 +57,26 @@ public String getPrice() {
         .build();
 
     try {
+      LOGGER.info("Sending request to fetch price info");
       var httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
+      logResponse(httpResponse);
       return httpResponse.body();
     } catch (IOException | InterruptedException e) {
-      e.printStackTrace();
+      LOGGER.error("Failure occurred while getting price info", e);
     }
 
     return null;
   }
+
+  private void logResponse(HttpResponse<String> httpResponse) {
+    if (isSuccessResponse(httpResponse.statusCode())) {
+      LOGGER.info("Price info received successfully");
+    } else {
+      LOGGER.warn("Price info request failed");
+    }
+  }
+
+  private boolean isSuccessResponse(int responseCode) {
+    return responseCode >= 200 && responseCode <= 299;
+  }
 }
diff --git a/api-gateway/image-microservice/src/main/java/com/iluwatar/image/microservice/ImageController.java b/api-gateway/image-microservice/src/main/java/com/iluwatar/image/microservice/ImageController.java
index b1f6dd3f7eb6..a96ef4f413a7 100644
--- a/api-gateway/image-microservice/src/main/java/com/iluwatar/image/microservice/ImageController.java
+++ b/api-gateway/image-microservice/src/main/java/com/iluwatar/image/microservice/ImageController.java
@@ -23,15 +23,20 @@
 
 package com.iluwatar.image.microservice;
 
+import static org.slf4j.LoggerFactory.getLogger;
+
+import org.slf4j.Logger;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
+
 /**
  * Exposes the Image microservice's endpoints.
  */
 @RestController
 public class ImageController {
+  private static final Logger LOGGER = getLogger(ImageController.class);
 
   /**
    * An endpoint for a user to retrieve an image path.
@@ -40,6 +45,7 @@ public class ImageController {
    */
   @RequestMapping(value = "/image-path", method = RequestMethod.GET)
   public String getImagePath() {
+    LOGGER.info("Successfully found image path");
     return "/product-image.png";
   }
 }
diff --git a/api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceController.java b/api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceController.java
index cf2f5eb4f83b..dbcd59952945 100644
--- a/api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceController.java
+++ b/api-gateway/price-microservice/src/main/java/com/iluwatar/price/microservice/PriceController.java
@@ -23,15 +23,20 @@
 
 package com.iluwatar.price.microservice;
 
+import static org.slf4j.LoggerFactory.getLogger;
+
+import org.slf4j.Logger;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
+
 /**
  * Exposes the Price microservice's endpoints.
  */
 @RestController
 public class PriceController {
+  private static final Logger LOGGER = getLogger(PriceController.class);
 
   /**
    * An endpoint for a user to retrieve a product's price.
@@ -40,6 +45,7 @@ public class PriceController {
    */
   @RequestMapping(value = "/price", method = RequestMethod.GET)
   public String getPrice() {
+    LOGGER.info("Successfully found price info");
     return "20";
   }
 }


Test patch: