Skip to content

Commit 94bac90

Browse files
committed
Added support to log requests/responses when communicating with the GitLab API (#228).
1 parent 8281b07 commit 94bac90

File tree

2 files changed

+100
-12
lines changed

2 files changed

+100
-12
lines changed

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);

0 commit comments

Comments
 (0)