Skip to content

Commit bdbfe75

Browse files
committed
Mods to use new MaskingLoggingFilter (#310).
1 parent 2acc798 commit bdbfe75

File tree

2 files changed

+96
-9
lines changed

2 files changed

+96
-9
lines changed

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

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.gitlab4j.api;
22

33
import java.util.Collections;
4+
import java.util.List;
45
import java.util.Map;
56
import java.util.Optional;
67
import java.util.WeakHashMap;
@@ -15,6 +16,7 @@
1516
import org.gitlab4j.api.models.Session;
1617
import org.gitlab4j.api.models.User;
1718
import org.gitlab4j.api.models.Version;
19+
import org.gitlab4j.api.utils.MaskingLoggingFilter;
1820
import org.gitlab4j.api.utils.Oauth2LoginStreamingOutput;
1921
import org.gitlab4j.api.utils.SecretString;
2022

@@ -618,22 +620,104 @@ public void enableRequestResponseLogging() {
618620

619621
/**
620622
* Enable the logging of the requests to and the responses from the GitLab server API
621-
* using the GitLab4J shared Logger instance.
623+
* using the GitLab4J shared Logger instance. Logging will NOT include entity logging and
624+
* will mask PRIVATE-TOKEN and Authorization headers.
622625
*
623626
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
624627
*/
625628
public void enableRequestResponseLogging(Level level) {
626-
enableRequestResponseLogging(LOGGER, level);
629+
enableRequestResponseLogging(LOGGER, level, 0);
627630
}
628631

629632
/**
630-
* Enable the logging of the requests to and the responses from the GitLab server API.
633+
* Enable the logging of the requests to and the responses from the GitLab server API using the
634+
* specified logger. Logging will NOT include entity logging and will mask PRIVATE-TOKEN
635+
* and Authorization headers..
631636
*
632637
* @param logger the Logger instance to log to
633638
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
634639
*/
635640
public void enableRequestResponseLogging(Logger logger, Level level) {
636-
this.apiClient.enableRequestResponseLogging(logger, level);
641+
enableRequestResponseLogging(logger, level, 0);
642+
}
643+
644+
/**
645+
* Enable the logging of the requests to and the responses from the GitLab server API using the
646+
* GitLab4J shared Logger instance. Logging will mask PRIVATE-TOKEN and Authorization headers.
647+
*
648+
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
649+
* @param maxEntitySize maximum number of entity bytes to be logged. When logging if the maxEntitySize
650+
* is reached, the entity logging will be truncated at maxEntitySize and "...more..." will be added at
651+
* the end of the log entry. If maxEntitySize is <= 0, entity logging will be disabled
652+
*/
653+
public void enableRequestResponseLogging(Level level, int maxEntitySize) {
654+
enableRequestResponseLogging(LOGGER, level, maxEntitySize);
655+
}
656+
657+
/**
658+
* Enable the logging of the requests to and the responses from the GitLab server API using the
659+
* specified logger. Logging will mask PRIVATE-TOKEN and Authorization headers.
660+
*
661+
* @param logger the Logger instance to log to
662+
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
663+
* @param maxEntitySize maximum number of entity bytes to be logged. When logging if the maxEntitySize
664+
* is reached, the entity logging will be truncated at maxEntitySize and "...more..." will be added at
665+
* the end of the log entry. If maxEntitySize is <= 0, entity logging will be disabled
666+
*/
667+
public void enableRequestResponseLogging(Logger logger, Level level, int maxEntitySize) {
668+
enableRequestResponseLogging(logger, level, maxEntitySize, MaskingLoggingFilter.DEFAULT_MASKED_HEADER_NAMES);
669+
}
670+
671+
/**
672+
* Enable the logging of the requests to and the responses from the GitLab server API using the
673+
* GitLab4J shared Logger instance.
674+
*
675+
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
676+
* @param maskedHeaderNames a list of header names that should have the values masked
677+
*/
678+
public void enableRequestResponseLogging(Level level, List<String> maskedHeaderNames) {
679+
apiClient.enableRequestResponseLogging(LOGGER, level, 0, maskedHeaderNames);
680+
}
681+
682+
/**
683+
* Enable the logging of the requests to and the responses from the GitLab server API using the
684+
* specified logger.
685+
*
686+
* @param logger the Logger instance to log to
687+
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
688+
* @param maskedHeaderNames a list of header names that should have the values masked
689+
*/
690+
public void enableRequestResponseLogging(Logger logger, Level level, List<String> maskedHeaderNames) {
691+
apiClient.enableRequestResponseLogging(logger, level, 0, maskedHeaderNames);
692+
}
693+
694+
/**
695+
* Enable the logging of the requests to and the responses from the GitLab server API using the
696+
* GitLab4J shared Logger instance.
697+
*
698+
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
699+
* @param maxEntitySize maximum number of entity bytes to be logged. When logging if the maxEntitySize
700+
* is reached, the entity logging will be truncated at maxEntitySize and "...more..." will be added at
701+
* the end of the log entry. If maxEntitySize is &lt;= 0, entity logging will be disabled
702+
* @param maskedHeaderNames a list of header names that should have the values masked
703+
*/
704+
public void enableRequestResponseLogging(Level level, int maxEntitySize, List<String> maskedHeaderNames) {
705+
apiClient.enableRequestResponseLogging(LOGGER, level, maxEntitySize, maskedHeaderNames);
706+
}
707+
708+
/**
709+
* Enable the logging of the requests to and the responses from the GitLab server API using the
710+
* specified logger.
711+
*
712+
* @param logger the Logger instance to log to
713+
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
714+
* @param maxEntitySize maximum number of entity bytes to be logged. When logging if the maxEntitySize
715+
* is reached, the entity logging will be truncated at maxEntitySize and "...more..." will be added at
716+
* the end of the log entry. If maxEntitySize is &lt;= 0, entity logging will be disabled
717+
* @param maskedHeaderNames a list of header names that should have the values masked
718+
*/
719+
public void enableRequestResponseLogging(Logger logger, Level level, int maxEntitySize, List<String> maskedHeaderNames) {
720+
apiClient.enableRequestResponseLogging(logger, level, maxEntitySize, maskedHeaderNames);
637721
}
638722

639723
/**

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
import org.gitlab4j.api.Constants.TokenType;
3434
import org.gitlab4j.api.GitLabApi.ApiVersion;
3535
import org.gitlab4j.api.utils.JacksonJson;
36+
import org.gitlab4j.api.utils.MaskingLoggingFilter;
3637
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
3738
import org.glassfish.jersey.client.ClientConfig;
3839
import org.glassfish.jersey.client.ClientProperties;
39-
import org.glassfish.jersey.logging.LoggingFeature;
4040
import org.glassfish.jersey.media.multipart.Boundary;
4141
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
4242
import org.glassfish.jersey.media.multipart.MultiPart;
@@ -244,12 +244,15 @@ public GitLabApiClient(ApiVersion apiVersion, String hostUrl, TokenType tokenTyp
244244
*
245245
* @param logger the Logger instance to log to
246246
* @param level the logging level (SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST)
247+
* @param maxEntitySize maximum number of entity bytes to be logged. When logging if the maxEntitySize
248+
* is reached, the entity logging will be truncated at maxEntitySize and "...more..." will be added at
249+
* the end of the log entry. If maxEntitySize is <= 0, entity logging will be disabled
250+
* @param maskedHeaderNames a list of header names that should have the values masked
247251
*/
248-
void enableRequestResponseLogging(Logger logger, Level level) {
252+
void enableRequestResponseLogging(Logger logger, Level level, int maxEntityLength, List<String> maskedHeaderNames) {
249253

250-
LoggingFeature loggingFeature = new LoggingFeature(
251-
logger, level, LoggingFeature.Verbosity.PAYLOAD_TEXT, LoggingFeature.DEFAULT_MAX_ENTITY_SIZE);
252-
clientConfig.register(loggingFeature);
254+
MaskingLoggingFilter loggingFilter = new MaskingLoggingFilter(logger, level, maxEntityLength, maskedHeaderNames);
255+
clientConfig.register(loggingFilter);
253256

254257
// Recreate the Client instance if already created.
255258
if (apiClient != null) {

0 commit comments

Comments
 (0)