Skip to content

Commit 814facc

Browse files
authored
Merge pull request #229 from gmessner/feature/228-add-request-response-logging
Add request and response logging
2 parents d0ffd09 + 12f5d86 commit 814facc

File tree

7 files changed

+259
-38
lines changed

7 files changed

+259
-38
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To utilize the GitLab API for Java in your project, simply add the following dep
1111
```java
1212
dependencies {
1313
...
14-
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.8.38'
14+
compile group: 'org.gitlab4j', name: 'gitlab4j-api', version: '4.8.39'
1515
}
1616
```
1717

@@ -20,7 +20,7 @@ dependencies {
2020
<dependency>
2121
<groupId>org.gitlab4j</groupId>
2222
<artifactId>gitlab4j-api</artifactId>
23-
<version>4.8.38</version>
23+
<version>4.8.39</version>
2424
</dependency>
2525
```
2626

@@ -96,6 +96,23 @@ GitLabApi gitLabApi = new GitLabApi(ApiVersion.V3, "http://your.gitlab.server.co
9696
As of GitLab 11.0 support for the GitLab API v3 has been removed (see https://about.gitlab.com/2018/06/01/api-v3-removal-impending/). Support for GitLab API v3 will be removed from this library in January 2019. If you are utilizing the v3 support, please update your code before January 2019.
9797

9898

99+
---
100+
## Logging of API Requests and Responses
101+
As of GitLab4J-API 4.8.39 support has been added to log the requests to and the responses from the
102+
GitLab API. Enable logging using one of the following methods on the GitLabApi instance:
103+
```java
104+
GitLabApi gitLabApi = new GitLabApi("http://your.gitlab.server.com", "YOUR_PRIVATE_TOKEN");
105+
106+
// Log using the shared logger and default level of FINE
107+
gitLabApi.enableRequestResponseLogging();
108+
109+
// Log using the shared logger and the INFO level
110+
gitLabApi.enableRequestResponseLogging(java.util.logging.Level.INFO);
111+
112+
// Log using the specified logger and the INFO level
113+
gitLabApi.enableRequestResponseLogging(youtLoggerInstance, java.util.logging.Level.INFO);
114+
```
115+
99116
---
100117
## Results Paging
101118
GitLab4J-API provides an easy to use paging mechanism to page through lists of results from the GitLab API.

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<junit.version>4.12</junit.version>
4747
<mockito.version>2.19.0</mockito.version>
4848
<hamcrest.version>1.3</hamcrest.version>
49+
<systemRules.version>1.18.0</systemRules.version>
4950
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5051
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
5152
</properties>
@@ -258,6 +259,12 @@
258259
<version>${hamcrest.version}</version>
259260
<scope>test</scope>
260261
</dependency>
262+
<dependency>
263+
<groupId>com.github.stefanbirkner</groupId>
264+
<artifactId>system-rules</artifactId>
265+
<version>${systemRules.version}</version>
266+
<scope>test</scope>
267+
</dependency>
261268
</dependencies>
262269

263270
</project>

src/main/java/org/gitlab4j/api/GitLabApi.java

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Map;
55
import java.util.Optional;
66
import java.util.WeakHashMap;
7+
import java.util.logging.Level;
78
import java.util.logging.Logger;
89

910
import javax.ws.rs.core.MediaType;
@@ -23,7 +24,7 @@
2324
*/
2425
public class GitLabApi {
2526

26-
private final static Logger LOG = Logger.getLogger(GitLabApi.class.getName());
27+
private final static Logger LOGGER = Logger.getLogger(GitLabApi.class.getName());
2728

2829
/** GitLab4J default per page. GitLab will ignore anything over 100. */
2930
public static final int DEFAULT_PER_PAGE = 100;
@@ -85,7 +86,7 @@ public String getApiNamespace() {
8586
* @return the GitLab4J shared Logger instance
8687
*/
8788
public static final Logger getLogger() {
88-
return (LOG);
89+
return (LOGGER);
8990
}
9091

9192
/**
@@ -638,6 +639,69 @@ public GitLabApi(ApiVersion apiVersion, String hostUrl, TokenType tokenType, Str
638639
apiClient = new GitLabApiClient(apiVersion, hostUrl, tokenType, authToken, secretToken, clientConfigProperties);
639640
}
640641

642+
/**
643+
* Enable the logging of the requests to and the responses from the GitLab server API
644+
* using the GitLab4J shared Logger instance and Level.FINE as the level.
645+
*
646+
* @return this GitLabApi instance
647+
*/
648+
public GitLabApi withRequestResponseLogging() {
649+
enableRequestResponseLogging();
650+
return (this);
651+
}
652+
653+
/**
654+
* Enable the logging of the requests to and the responses from the GitLab server API
655+
* using the GitLab4J shared Logger instance.
656+
*
657+
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
658+
* @return this GitLabApi instance
659+
*/
660+
public GitLabApi withRequestResponseLogging(Level level) {
661+
enableRequestResponseLogging(level);
662+
return (this);
663+
}
664+
665+
/**
666+
* Enable the logging of the requests to and the responses from the GitLab server API.
667+
*
668+
* @param logger the Logger instance to log to
669+
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
670+
* @return this GitLabApi instance
671+
*/
672+
public GitLabApi withRequestResponseLogging(Logger logger, Level level) {
673+
enableRequestResponseLogging(logger, level);
674+
return (this);
675+
}
676+
677+
/**
678+
* Enable the logging of the requests to and the responses from the GitLab server API
679+
* using the GitLab4J shared Logger instance and Level.FINE as the level.
680+
*/
681+
public void enableRequestResponseLogging() {
682+
enableRequestResponseLogging(LOGGER, Level.FINE);
683+
}
684+
685+
/**
686+
* Enable the logging of the requests to and the responses from the GitLab server API
687+
* using the GitLab4J shared Logger instance.
688+
*
689+
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
690+
*/
691+
public void enableRequestResponseLogging(Level level) {
692+
enableRequestResponseLogging(LOGGER, level);
693+
}
694+
695+
/**
696+
* Enable the logging of the requests to and the responses from the GitLab server API.
697+
*
698+
* @param logger the Logger instance to log to
699+
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
700+
*/
701+
public void enableRequestResponseLogging(Logger logger, Level level) {
702+
this.apiClient.enableRequestResponseLogging(logger, level);
703+
}
704+
641705
/**
642706
* Sets up all future calls to the GitLab API to be done as another user specified by sudoAsUsername.
643707
* To revert back to normal non-sudo operation you must call unsudo(), or pass null as the username.
@@ -1413,5 +1477,4 @@ public WikisApi getWikisApi() {
14131477

14141478
return wikisApi;
14151479
}
1416-
14171480
}

src/main/java/org/gitlab4j/api/GitLabApiClient.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.security.cert.X509Certificate;
1313
import java.util.List;
1414
import java.util.Map;
15+
import java.util.logging.Level;
16+
import java.util.logging.Logger;
1517

1618
import javax.net.ssl.HostnameVerifier;
1719
import javax.net.ssl.SSLContext;
@@ -36,6 +38,7 @@
3638
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
3739
import org.glassfish.jersey.client.ClientConfig;
3840
import org.glassfish.jersey.client.ClientProperties;
41+
import org.glassfish.jersey.logging.LoggingFeature;
3942
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
4043
import org.glassfish.jersey.media.multipart.MultiPart;
4144
import org.glassfish.jersey.media.multipart.MultiPartFeature;
@@ -237,6 +240,24 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
237240
clientConfig.register(MultiPartFeature.class);
238241
}
239242

243+
/**
244+
* Enable the logging of the requests to and the responses from the GitLab server API.
245+
*
246+
* @param logger the Logger instance to log to
247+
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
248+
*/
249+
void enableRequestResponseLogging(Logger logger, Level level) {
250+
251+
LoggingFeature loggingFeature = new LoggingFeature(
252+
logger, level, LoggingFeature.Verbosity.PAYLOAD_TEXT, LoggingFeature.DEFAULT_MAX_ENTITY_SIZE);
253+
clientConfig.register(loggingFeature);
254+
255+
// Recreate the Client instance if already created.
256+
if (apiClient != null) {
257+
createApiClient();
258+
}
259+
}
260+
240261
/**
241262
* Get the auth token being used by this client.
242263
*
@@ -629,18 +650,22 @@ protected Invocation.Builder invocation(URL url, MultivaluedMap<String, String>
629650
return (invocation(url, queryParams, MediaType.APPLICATION_JSON));
630651
}
631652

653+
protected Client createApiClient() {
654+
655+
ClientBuilder clientBuilder = ClientBuilder.newBuilder().withConfig(clientConfig);
656+
657+
if (ignoreCertificateErrors) {
658+
clientBuilder.sslContext(openSslContext).hostnameVerifier(openHostnameVerifier);
659+
}
660+
661+
apiClient = clientBuilder.build();
662+
return (apiClient);
663+
}
664+
632665
protected Invocation.Builder invocation(URL url, MultivaluedMap<String, String> queryParams, String accept) {
633666

634667
if (apiClient == null) {
635-
if (ignoreCertificateErrors) {
636-
apiClient = ClientBuilder.newBuilder()
637-
.withConfig(clientConfig)
638-
.sslContext(openSslContext)
639-
.hostnameVerifier(openHostnameVerifier)
640-
.build();
641-
} else {
642-
apiClient = ClientBuilder.newBuilder().withConfig(clientConfig).build();
643-
}
668+
createApiClient();
644669
}
645670

646671
WebTarget target = apiClient.target(url.toExternalForm()).property(ClientProperties.FOLLOW_REDIRECTS, true);

src/main/java/org/gitlab4j/api/systemhooks/SystemHookManager.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import javax.servlet.http.HttpServletRequest;
1111

12+
import org.gitlab4j.api.GitLabApi;
1213
import org.gitlab4j.api.GitLabApiException;
1314
import org.gitlab4j.api.HookManager;
1415
import org.gitlab4j.api.utils.HttpRequestUtils;
@@ -21,7 +22,7 @@ public class SystemHookManager extends HookManager {
2122

2223
public static final String SYSTEM_HOOK_EVENT = "System Hook";
2324

24-
private final static Logger LOG = Logger.getLogger(SystemHookManager.class.getName());
25+
private final static Logger LOGGER = GitLabApi.getLogger();
2526
private final JacksonJson jacksonJson = new JacksonJson();
2627

2728
// Collection of objects listening for System Hook events.
@@ -55,27 +56,27 @@ public void handleEvent(HttpServletRequest request) throws GitLabApiException {
5556

5657
if (!isValidSecretToken(request)) {
5758
String message = "X-Gitlab-Token mismatch!";
58-
LOG.warning(message);
59+
LOGGER.warning(message);
5960
throw new GitLabApiException(message);
6061
}
6162

6263
String eventName = request.getHeader("X-Gitlab-Event");
63-
LOG.info("handleEvent: X-Gitlab-Event=" + eventName);
64+
LOGGER.info("handleEvent: X-Gitlab-Event=" + eventName);
6465
if (!SYSTEM_HOOK_EVENT.equals(eventName)) {
6566
String message = "Unsupported X-Gitlab-Event, event Name=" + eventName;
66-
LOG.warning(message);
67+
LOGGER.warning(message);
6768
throw new GitLabApiException(message);
6869
}
6970

7071
try {
7172

7273
SystemHookEvent event;
73-
if (LOG.isLoggable(Level.FINE)) {
74-
LOG.fine(HttpRequestUtils.getShortRequestDump("System Hook", true, request));
74+
if (LOGGER.isLoggable(Level.FINE)) {
75+
LOGGER.fine(HttpRequestUtils.getShortRequestDump("System Hook", true, request));
7576
String postData = HttpRequestUtils.getPostDataAsString(request);
76-
LOG.fine("Raw POST data:\n" + postData);
77+
LOGGER.fine("Raw POST data:\n" + postData);
7778
event = jacksonJson.unmarshal(SystemHookEvent.class, postData);
78-
LOG.fine(event.getEventName() + "\n" + jacksonJson.marshal(event) + "\n");
79+
LOGGER.fine(event.getEventName() + "\n" + jacksonJson.marshal(event) + "\n");
7980
} else {
8081
InputStreamReader reader = new InputStreamReader(request.getInputStream());
8182
event = jacksonJson.unmarshal(SystemHookEvent.class, reader);
@@ -84,7 +85,7 @@ public void handleEvent(HttpServletRequest request) throws GitLabApiException {
8485
fireEvent(event);
8586

8687
} catch (Exception e) {
87-
LOG.warning("Error processing JSON data, exception=" +
88+
LOGGER.warning("Error processing JSON data, exception=" +
8889
e.getClass().getSimpleName() + ", error=" + e.getMessage());
8990
throw new GitLabApiException(e);
9091
}
@@ -98,10 +99,10 @@ public void handleEvent(HttpServletRequest request) throws GitLabApiException {
9899
*/
99100
public void handleEvent(SystemHookEvent event) throws GitLabApiException {
100101
if (event != null) {
101-
LOG.info("handleEvent:" + event.getClass().getSimpleName() + ", eventName=" + event.getEventName());
102+
LOGGER.info("handleEvent:" + event.getClass().getSimpleName() + ", eventName=" + event.getEventName());
102103
fireEvent(event);
103104
} else {
104-
LOG.warning("handleEvent: provided event cannot be null!");
105+
LOGGER.warning("handleEvent: provided event cannot be null!");
105106
}
106107
}
107108

@@ -154,7 +155,7 @@ public void fireEvent(SystemHookEvent event) throws GitLabApiException {
154155
fireRepositoryEvent((RepositorySystemHookEvent) event);
155156
} else {
156157
String message = "Unsupported event, event_named=" + event.getEventName();
157-
LOG.warning(message);
158+
LOGGER.warning(message);
158159
throw new GitLabApiException(message);
159160
}
160161
}

0 commit comments

Comments
 (0)