Add logging in API Gateway

This commit is contained in:
Amit Garg 2020-08-09 14:18:41 +01:00 committed by Amit Garg
parent 1e90d0d645
commit d219a104c7
4 changed files with 57 additions and 2 deletions

View File

@ -23,11 +23,16 @@
package com.iluwatar.api.gateway; package com.iluwatar.api.gateway;
import static org.slf4j.LoggerFactory.getLogger;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers; import java.net.http.HttpResponse.BodyHandlers;
import org.slf4j.Logger;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
@ -35,6 +40,8 @@ import org.springframework.stereotype.Component;
*/ */
@Component @Component
public class ImageClientImpl implements ImageClient { public class ImageClientImpl implements ImageClient {
private static final Logger LOGGER = getLogger(ImageClientImpl.class);
/** /**
* Makes a simple HTTP Get request to the Image microservice. * Makes a simple HTTP Get request to the Image microservice.
* *
@ -49,12 +56,26 @@ public class ImageClientImpl implements ImageClient {
.build(); .build();
try { try {
LOGGER.info("Sending request to fetch image path");
var httpResponse = httpClient.send(httpGet, BodyHandlers.ofString()); var httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
logResponse(httpResponse);
return httpResponse.body(); return httpResponse.body();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
e.printStackTrace(); LOGGER.error("Failure occurred while getting image path", e);
} }
return null; 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;
}
} }

View File

@ -23,18 +23,26 @@
package com.iluwatar.api.gateway; package com.iluwatar.api.gateway;
import static org.slf4j.LoggerFactory.getLogger;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers; import java.net.http.HttpResponse.BodyHandlers;
import org.slf4j.Logger;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
* An adapter to communicate with the Price microservice. * An adapter to communicate with the Price microservice.
*/ */
@Component @Component
public class PriceClientImpl implements PriceClient { public class PriceClientImpl implements PriceClient {
private static final Logger LOGGER = getLogger(PriceClientImpl.class);
/** /**
* Makes a simple HTTP Get request to the Price microservice. * Makes a simple HTTP Get request to the Price microservice.
* *
@ -49,12 +57,26 @@ public class PriceClientImpl implements PriceClient {
.build(); .build();
try { try {
LOGGER.info("Sending request to fetch price info");
var httpResponse = httpClient.send(httpGet, BodyHandlers.ofString()); var httpResponse = httpClient.send(httpGet, BodyHandlers.ofString());
logResponse(httpResponse);
return httpResponse.body(); return httpResponse.body();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
e.printStackTrace(); LOGGER.error("Failure occurred while getting price info", e);
} }
return null; 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;
}
} }

View File

@ -23,15 +23,20 @@
package com.iluwatar.image.microservice; 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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/** /**
* Exposes the Image microservice's endpoints. * Exposes the Image microservice's endpoints.
*/ */
@RestController @RestController
public class ImageController { public class ImageController {
private static final Logger LOGGER = getLogger(ImageController.class);
/** /**
* An endpoint for a user to retrieve an image path. * An endpoint for a user to retrieve an image path.
@ -40,6 +45,7 @@ public class ImageController {
*/ */
@RequestMapping(value = "/image-path", method = RequestMethod.GET) @RequestMapping(value = "/image-path", method = RequestMethod.GET)
public String getImagePath() { public String getImagePath() {
LOGGER.info("Successfully found image path");
return "/product-image.png"; return "/product-image.png";
} }
} }

View File

@ -23,15 +23,20 @@
package com.iluwatar.price.microservice; 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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/** /**
* Exposes the Price microservice's endpoints. * Exposes the Price microservice's endpoints.
*/ */
@RestController @RestController
public class PriceController { public class PriceController {
private static final Logger LOGGER = getLogger(PriceController.class);
/** /**
* An endpoint for a user to retrieve a product's price. * An endpoint for a user to retrieve a product's price.
@ -40,6 +45,7 @@ public class PriceController {
*/ */
@RequestMapping(value = "/price", method = RequestMethod.GET) @RequestMapping(value = "/price", method = RequestMethod.GET)
public String getPrice() { public String getPrice() {
LOGGER.info("Successfully found price info");
return "20"; return "20";
} }
} }