Skip to content

Commit

Permalink
SLF4J logging api
Browse files Browse the repository at this point in the history
This is a proof of concept. It demonstrates how SLF4J could be used
as the Logging API.

SFL4J is the standard logging API for open source Java libraries.
  • Loading branch information
sullis committed Dec 24, 2020
1 parent 05e4deb commit 3032865
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 42 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ Twilio.setEdge("sydney");
This will result in the `hostname` transforming from `api.twilio.com` to `api.sydney.au1.twilio.com`.

### Enable Debug Logging
In order to enable debug logging, create a configuration file named log4j2.xml that defines the logger at the root level to at least 'debug'. An example of the configuration file can be found [here](src/main/java/com/twilio/example/log4j2.xml). For more configuration options please see the log4j configuration [documentation](https://logging.apache.org/log4j/2.x/manual/configuration.html).
```java
Twilio.init(accountSid, authToken);
Twilio.setLoggerConfiguration("path/to/log4j2.xml");
```

This library uses SFL4J for logging. Consult the SFL4J documentation
for information about logging configuration:

http://slf4j.org/docs.html

### Environment Variables

Expand Down
27 changes: 20 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
Expand All @@ -231,6 +236,12 @@
<artifactId>archunit</artifactId>
<version>0.14.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
Expand All @@ -240,13 +251,15 @@
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.14.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
Expand Down Expand Up @@ -392,4 +405,4 @@
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
</project>
</project>
16 changes: 0 additions & 16 deletions src/main/java/com/twilio/Twilio.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
import java.util.concurrent.Executors;
import java.io.File;

import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.LogManager;

/**
* Singleton class to initialize Twilio environment.
*/
Expand Down Expand Up @@ -154,19 +151,6 @@ public static synchronized void setEdge(final String edge) {
Twilio.edge = edge;
}

/**
* Set the logger configuration file path.
*
* @param filePath path to logging configuration file
* @param loggerContext defaults to false to get the appropriate logger context for the caller.
*/
public static synchronized void setLoggerConfiguration(final String filePath, final boolean... loggerContext) {
boolean logContext = (loggerContext.length >= 1) ? loggerContext[0] : false;
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(logContext);
File file = new File(filePath);
context.setConfigLocation(file.toURI());
}

/**
* Returns (and initializes if not initialized) the Twilio Rest Client.
*
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/com/twilio/http/TwilioRestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.function.Predicate;
import java.util.Map;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;

public class TwilioRestClient {

public static final int HTTP_STATUS_CODE_CREATED = 201;
Expand All @@ -25,7 +24,7 @@ public class TwilioRestClient {
private final String region;
private final String edge;
private final HttpClient httpClient;
private static final Logger logger = LogManager.getLogger();
private static final Logger logger = LoggerFactory.getLogger(TwilioRestClient.class);

private TwilioRestClient(Builder b) {
this.username = b.username;
Expand Down Expand Up @@ -59,11 +58,14 @@ public Response request(final Request request) {

logRequest(request);
Response response = httpClient.reliableRequest(request);
logger.debug("status code: " + response.getStatusCode());
org.apache.http.Header[] responseHeaders = response.getHeaders();
logger.debug("response headers:");
for (int i = 0; i < responseHeaders.length; i++) {
logger.debug(responseHeaders[i]);

if (logger.isDebugEnabled()) {
logger.debug("status code: {}", response.getStatusCode());
org.apache.http.Header[] responseHeaders = response.getHeaders();
logger.debug("response headers:");
for (int i = 0; i < responseHeaders.length; i++) {
logger.debug("responseHeader: {}", responseHeaders[i]);
}
}

return response;
Expand Down
7 changes: 3 additions & 4 deletions src/test/java/com/twilio/http/LoggingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
import org.junit.Assert;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;

public class LoggingTest {
private static final PrintStream ORIGINAL_SYSTEM_OUT = System.out;
private ByteArrayOutputStream output;
private PrintStream originalStream;

@Before
public void setUp() throws Exception {
Expand All @@ -22,21 +23,19 @@ public void setUp() throws Exception {
public void logCapturingSetup() {
output = new ByteArrayOutputStream();
PrintStream outputStream = new PrintStream(output);
originalStream = System.out;
System.setOut(outputStream);
}

public void finishLogCapturingSetup(Request request) {
TwilioRestClient twilioRestClient = Twilio.getRestClient();
twilioRestClient.logRequest(request);
System.out.flush();
System.setOut(originalStream);
System.setOut(ORIGINAL_SYSTEM_OUT);
}

@Test
public void testDebugLogging() {
logCapturingSetup();
Twilio.setLoggerConfiguration("src/main/java/com/twilio/example/log4j2.xml");
final Request request = new Request(HttpMethod.GET, Domains.API.toString(),
"/2010-04-01/Accounts/AC123/Messages/MM123.json");
request.addHeaderParam("Authorization", "authorization value");
Expand Down
13 changes: 13 additions & 0 deletions src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t] %-5p %c - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>

0 comments on commit 3032865

Please sign in to comment.