Skip to content
Permalink
Browse files

added logging

  • Loading branch information...
Rakesh Pawar
Rakesh Pawar committed May 22, 2018
1 parent 6810e8d commit c0c65ea0dff1663a892973ec71216a1eefddfdbc
Showing with 108 additions and 8 deletions.
  1. +35 −0 log4j/log4j2.xml
  2. +44 −5 pom.xml
  3. +29 −3 src/main/java/com/github/agogs/holidayapi/api/impl/HolidayAPIConsumer.java
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="30">
<Properties>
<Property name="log-pattern">%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
</Property>
</Properties>

<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="${log-pattern}"/>
</Console>
<RollingFile name="rollingFileAppender"
fileName="logs/holiday-api-consumer.log"
filePattern="logs/holiday-api-consumer.log.%d{yyyy-MM-dd-hh}.gz">
<PatternLayout pattern="${log-pattern}"/>
<Policies>
<SizeBasedTriggeringPolicy size="100 MB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
</Appenders>

<!-- Logger levels: TRACE, DEBUG, INFO, WARN, ERROR, FATAL -->
<Loggers>
<AsyncLogger name="com.github.agogs.holidayapi" level="DEBUG" additivity="false" includeLocation="true">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="rollingFileAppender"/>
</AsyncLogger>

<Root level="INFO">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="rollingFileAppender"/>
</Root>
</Loggers>
</Configuration>
49 pom.xml
@@ -38,6 +38,13 @@

<dependencies>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>

<!--JSON libs -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
@@ -48,16 +55,48 @@
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>

<!--Logging dependencies-->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.6</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jul</artifactId>
<version>2.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>

<!--Test dependencies-->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
@@ -4,6 +4,8 @@
import com.github.agogs.holidayapi.api.APIConsumer;
import com.github.agogs.holidayapi.model.HolidayAPIResponse;
import com.github.agogs.holidayapi.model.QueryParams;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
@@ -19,16 +21,20 @@
*/
public class HolidayAPIConsumer implements APIConsumer {

private static final Logger log = LogManager.getLogger(HolidayAPIConsumer.class);

//store the base url in a variable for later use
private String baseURl;
//to make a https connection
private URLConnection connection;

/**
* Parameterized constructor
*
* @param baseURl - the base url of the API
*/
public HolidayAPIConsumer(String baseURl) {
log.debug("instantiating with base url : {}", baseURl);
this.baseURl = baseURl;
}

@@ -44,13 +50,23 @@ public HolidayAPIConsumer(String baseURl) {
@Override
public HolidayAPIResponse getHolidays(QueryParams queryParams) throws IOException {

log.info("get holidays API call with query params {}", queryParams.toString());

//get the response string
String json = getHolidaysAsString(queryParams);

log.info("got json string response");
log.debug("got json string response : {}", json);

//ObjectMapper to map the json string to a java object
ObjectMapper mapper = new ObjectMapper();

HolidayAPIResponse response = mapper.readValue(json, HolidayAPIResponse.class);

log.info("returning response");
log.debug("returning response : {}", response);
//map and return the response encapsulated in HolidayAPIResponse object
return mapper.readValue(json, HolidayAPIResponse.class);
return response;
}

/**
@@ -63,9 +79,13 @@ public HolidayAPIResponse getHolidays(QueryParams queryParams) throws IOExceptio
@Override
public String getHolidaysAsString(QueryParams queryParams) throws IOException {

log.info("get holidays API call with query params {}", queryParams.toString());

log.info("constructing url with query param string");
//construct the complete url
URL url = new URL(this.baseURl + "?" + queryParams.queryString());

log.info("connecting to API endpoint");
//attempt the https connection
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.connect();
@@ -76,23 +96,29 @@ public String getHolidaysAsString(QueryParams queryParams) throws IOException {
String line = null;

int responseCode = connection.getResponseCode();
log.info("response code : {}", responseCode);

//check for the response code
if ( responseCode == 200) {
if (responseCode == 200) {
log.info("preparing to read content from inputstream");
//read from inputstream if response code is 200
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
} else {
log.info("preparing to read content from errorstream");
//read from errorstream when the response code is not 200
bufferedReader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
}

log.info("reading response content");
//read the contents of the response
while ((line = bufferedReader.readLine()) != null) {
builder.append(line);
}
bufferedReader.close();
String response = builder.toString();

log.info("returning response string {}", response);
//return the response
return builder.toString();
return response;
}
}

0 comments on commit c0c65ea

Please sign in to comment.
You can’t perform that action at this time.