Skip to content

Commit

Permalink
add parameter to include loggers set to "off"
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Schneider <johannes.schneider@bosch.io>
  • Loading branch information
jokraehe committed Sep 28, 2022
1 parent a022dcc commit 18a8b5f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface LoggingFacade {
*
* @return the LoggerConfig for all known loggers.
*/
List<LoggerConfig> getLoggerConfig();
List<LoggerConfig> getLoggerConfig(boolean includeDisabledLoggers);

/**
* Returns the {@code LoggerConfig}s for the specified {@code loggerNames}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public final class RetrieveLoggerConfig extends AbstractDevOpsCommand<RetrieveLo
*/
public static final String TYPE = TYPE_PREFIX + NAME;

static final JsonFieldDefinition<Boolean> JSON_INCLUDE_DISABLED_LOGGERS =
JsonFactory.newBooleanFieldDefinition("includeDisabledLoggers", FieldType.REGULAR,
JsonSchemaVersion.V_2);

static final JsonFieldDefinition<Boolean> JSON_ALL_KNOWN_LOGGERS =
JsonFactory.newBooleanFieldDefinition("allKnownLoggers", FieldType.REGULAR,
JsonSchemaVersion.V_2);
Expand All @@ -61,24 +65,28 @@ public final class RetrieveLoggerConfig extends AbstractDevOpsCommand<RetrieveLo
JsonFactory.newJsonArrayFieldDefinition("specificLoggers", FieldType.REGULAR,
JsonSchemaVersion.V_2);

private final boolean includeDisabledLoggers;
private final boolean allKnownLoggers;
private final List<String> specificLoggers;

private RetrieveLoggerConfig(@Nullable final String serviceName, @Nullable final String instance,
final boolean allKnownLoggers, final List<String> specificLoggers, final DittoHeaders dittoHeaders) {
final boolean includeDisabledLoggers, final boolean allKnownLoggers, final List<String> specificLoggers,
final DittoHeaders dittoHeaders) {
super(TYPE, serviceName, instance, dittoHeaders);
this.includeDisabledLoggers = includeDisabledLoggers;
this.allKnownLoggers = allKnownLoggers;
this.specificLoggers = Collections.unmodifiableList(new ArrayList<>(specificLoggers));
}

/**
* Returns a new instance of {@code RetrieveLoggerConfig}.
*
* @param includeDisabledLoggers whether to include disabled loggers or not.
* @param dittoHeaders the headers of the request.
* @return a new RetrieveLoggerConfig command.
*/
public static RetrieveLoggerConfig ofAllKnownLoggers(final DittoHeaders dittoHeaders) {
return new RetrieveLoggerConfig(null, null, true, Collections.emptyList(), dittoHeaders);
public static RetrieveLoggerConfig ofAllKnownLoggers(final boolean includeDisabledLoggers, final DittoHeaders dittoHeaders) {
return new RetrieveLoggerConfig(null, null, includeDisabledLoggers, true, Collections.emptyList(), dittoHeaders);
}

/**
Expand All @@ -90,21 +98,23 @@ public static RetrieveLoggerConfig ofAllKnownLoggers(final DittoHeaders dittoHea
*/
public static RetrieveLoggerConfig ofAllKnownLoggers(@Nullable final String serviceName,
final DittoHeaders dittoHeaders) {
return new RetrieveLoggerConfig(serviceName, null, true, Collections.emptyList(), dittoHeaders);
return new RetrieveLoggerConfig(serviceName, null, false, true, Collections.emptyList(), dittoHeaders);
}

/**
* Returns a new instance of {@code RetrieveLoggerConfig}.
*
* @param serviceName the service name to which to send the DevOpsCommand.
* @param instance the instance index of the serviceName to which to send the DevOpsCommand.
* @param includeDisabledLoggers whether to include disabled loggers in the response or not.
* @param dittoHeaders the headers of the request.
* @return a new RetrieveLoggerConfig command.
*/
public static RetrieveLoggerConfig ofAllKnownLoggers(@Nullable final String serviceName,
@Nullable final String instance,
final boolean includeDisabledLoggers,
final DittoHeaders dittoHeaders) {
return new RetrieveLoggerConfig(serviceName, instance, true, Collections.emptyList(), dittoHeaders);
return new RetrieveLoggerConfig(serviceName, instance, includeDisabledLoggers, true, Collections.emptyList(), dittoHeaders);
}

/**
Expand All @@ -115,7 +125,7 @@ public static RetrieveLoggerConfig ofAllKnownLoggers(@Nullable final String serv
* @return a new RetrieveLoggerConfig command.
*/
public static RetrieveLoggerConfig of(final DittoHeaders dittoHeaders, final String... specificLoggers) {
return new RetrieveLoggerConfig(null, null, false,
return new RetrieveLoggerConfig(null, null, false, false,
specificLoggers == null ? Collections.emptyList() : Arrays.asList(specificLoggers), dittoHeaders);
}

Expand All @@ -127,7 +137,7 @@ public static RetrieveLoggerConfig of(final DittoHeaders dittoHeaders, final Str
* @return a new RetrieveLoggerConfig command.
*/
public static RetrieveLoggerConfig of(final DittoHeaders dittoHeaders, final List<String> specificLoggers) {
return new RetrieveLoggerConfig(null, null, false, specificLoggers, dittoHeaders);
return new RetrieveLoggerConfig(null, null, false, false, specificLoggers, dittoHeaders);
}

/**
Expand All @@ -140,7 +150,7 @@ public static RetrieveLoggerConfig of(final DittoHeaders dittoHeaders, final Lis
*/
public static RetrieveLoggerConfig of(@Nullable final String serviceName, final DittoHeaders dittoHeaders,
final String... specificLoggers) {
return new RetrieveLoggerConfig(serviceName, null, false,
return new RetrieveLoggerConfig(serviceName, null, false, false,
specificLoggers == null ? Collections.emptyList() : Arrays.asList(specificLoggers), dittoHeaders);
}

Expand All @@ -155,7 +165,7 @@ public static RetrieveLoggerConfig of(@Nullable final String serviceName, final
*/
public static RetrieveLoggerConfig of(@Nullable final String serviceName, @Nullable final String instance,
final DittoHeaders dittoHeaders, final String... specificLoggers) {
return new RetrieveLoggerConfig(serviceName, instance, false,
return new RetrieveLoggerConfig(serviceName, instance, false, false,
specificLoggers == null ? Collections.emptyList() : Arrays.asList(specificLoggers), dittoHeaders);
}

Expand All @@ -169,22 +179,24 @@ public static RetrieveLoggerConfig of(@Nullable final String serviceName, @Nulla
*/
public static RetrieveLoggerConfig of(@Nullable final String serviceName, final DittoHeaders dittoHeaders,
final List<String> specificLoggers) {
return new RetrieveLoggerConfig(serviceName, null, false, specificLoggers, dittoHeaders);
return new RetrieveLoggerConfig(serviceName, null, false, false, specificLoggers, dittoHeaders);
}

/**
* Returns a new instance of {@code RetrieveLoggerConfig}.
*
* @param serviceName the service name to which to send the DevOpsCommand.
* @param instance the instance index of the serviceName to which to send the DevOpsCommand.
* @param includeDisabledLoggers whether to include disabled loggers or not.
* @param dittoHeaders the headers of the request.
* @param specificLoggers one or more loggers to be retrieved.
* @return a new RetrieveLoggerConfig command.
*/
public static RetrieveLoggerConfig of(@Nullable final String serviceName, @Nullable final String instance,
final boolean includeDisabledLoggers,
final DittoHeaders dittoHeaders,
final List<String> specificLoggers) {
return new RetrieveLoggerConfig(serviceName, instance, false, specificLoggers, dittoHeaders);
return new RetrieveLoggerConfig(serviceName, instance, includeDisabledLoggers, false, specificLoggers, dittoHeaders);
}

/**
Expand Down Expand Up @@ -216,17 +228,18 @@ public static RetrieveLoggerConfig fromJson(final JsonObject jsonObject, final D
final String serviceName = jsonObject.getValue(DevOpsCommand.JsonFields.JSON_SERVICE_NAME).orElse(null);
final String instance = jsonObject.getValue(DevOpsCommand.JsonFields.JSON_INSTANCE).orElse(null);
final boolean isAllKnownLoggers = jsonObject.getValueOrThrow(JSON_ALL_KNOWN_LOGGERS);
final boolean includeDisabledLoggers = jsonObject.getValueOrThrow(JSON_INCLUDE_DISABLED_LOGGERS);

if (isAllKnownLoggers) {
return ofAllKnownLoggers(dittoHeaders);
return ofAllKnownLoggers(includeDisabledLoggers, dittoHeaders);
} else {
final JsonArray loggersJsonArray = jsonObject.getValueOrThrow(JSON_SPECIFIC_LOGGERS);
final List<String> extractedSpecificLoggers = loggersJsonArray.stream()
.filter(JsonValue::isString)
.map(JsonValue::asString)
.toList();

return of(serviceName, instance, dittoHeaders, extractedSpecificLoggers);
return of(serviceName, instance, includeDisabledLoggers, dittoHeaders, extractedSpecificLoggers);
}
});
}
Expand All @@ -238,7 +251,16 @@ public Category getCategory() {

@Override
public RetrieveLoggerConfig setDittoHeaders(final DittoHeaders dittoHeaders) {
return of(getServiceName().orElse(null), getInstance().orElse(null), dittoHeaders, specificLoggers);
return of(getServiceName().orElse(null), getInstance().orElse(null), includeDisabledLoggers, dittoHeaders, specificLoggers);
}

/**
* Returns whether all disabled loggers to include or not.
*
* @return whether all disabled loggers to include or not.
*/
public boolean includeDisabledLoggers() {
return includeDisabledLoggers;
}

/**
Expand Down Expand Up @@ -267,6 +289,7 @@ protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final Js

final Predicate<JsonField> predicate = schemaVersion.and(thePredicate);
jsonObjectBuilder.set(JSON_ALL_KNOWN_LOGGERS, allKnownLoggers, predicate);
jsonObjectBuilder.set(JSON_INCLUDE_DISABLED_LOGGERS, includeDisabledLoggers, predicate);

if (specificLoggers.size() > 0) {
jsonObjectBuilder.set(JSON_SPECIFIC_LOGGERS, specificLoggers.stream()
Expand All @@ -285,8 +308,9 @@ public boolean equals(@Nullable final Object o) {
return false;
}
final RetrieveLoggerConfig that = (RetrieveLoggerConfig) o;
return that.canEqual(this) && Objects.equals(allKnownLoggers, that.allKnownLoggers) && Objects
.equals(specificLoggers, that.specificLoggers) && super.equals(that);
return that.canEqual(this) && Objects.equals(includeDisabledLoggers, that.includeDisabledLoggers)
&& Objects.equals(allKnownLoggers, that.allKnownLoggers)
&& Objects.equals(specificLoggers, that.specificLoggers) && super.equals(that);
}

@Override
Expand All @@ -296,13 +320,13 @@ protected boolean canEqual(@Nullable final Object other) {

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), allKnownLoggers, specificLoggers);
return Objects.hash(super.hashCode(), includeDisabledLoggers, allKnownLoggers, specificLoggers);
}

@Override
public String toString() {
return getClass().getSimpleName() + " [" + super.toString() + "allKnownLoggers=" + allKnownLoggers
+ ", specificLoggers=" + specificLoggers + "]";
return getClass().getSimpleName() + " [" + super.toString() + "includeDisabledLoggers=" + includeDisabledLoggers
+ "allKnownLoggers=" + allKnownLoggers + ", specificLoggers=" + specificLoggers + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ private void handleRetrieveLoggerConfig(final RetrieveLoggerConfig command) {
final List<LoggerConfig> loggerConfigs;

if (command.isAllKnownLoggers()) {
loggerConfigs = loggingFacade.getLoggerConfig();
loggerConfigs = loggingFacade.getLoggerConfig(command.includeDisabledLoggers());
} else {
loggerConfigs = loggingFacade.getLoggerConfig(command.getSpecificLoggers());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public boolean setLogLevel(@Nonnull final LoggerConfig loggerConfig) {

@Override
@Nonnull
public List<LoggerConfig> getLoggerConfig() {
public List<LoggerConfig> getLoggerConfig(final boolean includeDisabledLoggers) {
final Logger rootLogger = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
final List<Logger> loggerList = rootLogger.getLoggerContext().getLoggerList();

return loggerConfigsFor(loggerList);
return loggerConfigsFor(loggerList, includeDisabledLoggers);
}

@Override
Expand All @@ -73,17 +73,24 @@ public List<LoggerConfig> getLoggerConfig(@Nonnull final Iterable<String> logger
.map(logger -> (Logger) LoggerFactory.getLogger(logger))
.toList();

return loggerConfigsFor(loggerList);
return loggerConfigsFor(loggerList, true);
}

private List<LoggerConfig> loggerConfigsFor(final Iterable<Logger> loggers) {
private List<LoggerConfig> loggerConfigsFor(final Iterable<Logger> loggers, final boolean includeDisabledLoggers) {
final List<LoggerConfig> loggerConfigList = new ArrayList<>();

loggers.forEach(logger -> {
final Level level = Optional.ofNullable(logger.getLevel()).orElse(Level.OFF);
final Optional<LogLevel> logLevelOptional = LogLevel.forIdentifier(level.toString());
logLevelOptional
.filter(logLevel -> !logLevel.equals(LogLevel.OFF)) // filter out the "off" loggers
.filter(logLevel -> {
if (includeDisabledLoggers) {
return true;
} else {
// filter out the "off" loggers
return !logLevel.equals(LogLevel.OFF);
}
})
.ifPresent(logLevel -> loggerConfigList.add(ImmutableLoggerConfig.of(logLevel, logger.getName())));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public final class DevOpsRoute extends AbstractRoute {
*/
private static final String PATH_PARAMETER = "path";

/**
* Path parameter for retrieving disabled loggers.
*/
private static final String INCLUDE_DISABLED_LOGGERS_PARAMETER = "includeDisabledLoggers";

private final HttpConfig httpConfig;
private final DevopsAuthenticationDirective devOpsAuthenticationDirective;

Expand Down Expand Up @@ -189,12 +194,12 @@ private Route routeLogging(final RequestContext ctx,
final DittoHeaders dittoHeaders) {

return concat(
get(() ->
get(() -> parameterOptional(INCLUDE_DISABLED_LOGGERS_PARAMETER, idl ->
handlePerRequest(ctx,
RetrieveLoggerConfig.ofAllKnownLoggers(serviceName, instance, dittoHeaders),
RetrieveLoggerConfig.ofAllKnownLoggers(serviceName, instance, idl.map(Boolean::valueOf).orElse(false), dittoHeaders),
transformResponse(serviceName, instance)
)
),
)),
put(() ->
extractDataBytes(payloadSource ->
handlePerRequest(ctx, dittoHeaders, payloadSource,
Expand Down

0 comments on commit 18a8b5f

Please sign in to comment.