Skip to content

Commit

Permalink
Enhance ConfigLoggingBundle to log a minimized Dropwizard configurati…
Browse files Browse the repository at this point in the history
…on (with constructor default values excluded). (#2011)
  • Loading branch information
motlin committed May 10, 2024
2 parents 8d18165 + 97071ac commit fae4180
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
7 changes: 6 additions & 1 deletion liftwizard-logging/liftwizard-bundle-logging-config/pom.xml
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2020 Craig Motlin
~ Copyright 2024 Craig Motlin
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -62,6 +62,11 @@
<artifactId>jackson-core</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Expand Up @@ -75,8 +75,14 @@ private static void logConfiguration(
@Nonnull ObjectMapper objectMapper)
throws JsonProcessingException
{
String configurationString = objectMapper.writeValueAsString(configuration);
LOGGER.info("Inferred Dropwizard configuration:\n{}", configurationString);
ObjectMapper nonDefaultObjectMapper = objectMapper.copy();
nonDefaultObjectMapper.setMixInResolver(new JsonIncludeNonDefaultMixInResolver());

String configurationString = nonDefaultObjectMapper.writeValueAsString(configuration);
LOGGER.info("Dropwizard configuration (minimized):\n{}", configurationString);

String fullConfigurationString = objectMapper.writeValueAsString(configuration);
LOGGER.debug("Dropwizard configuration (full):\n{}", fullConfigurationString);

Optional<Object> maybeDefaultConfiguration = ConfigLoggingBundle
.getConstructor(configuration)
Expand All @@ -89,7 +95,7 @@ private static void logConfiguration(
if (LOGGER.isDebugEnabled())
{
String defaultConfigurationString = objectMapper.writeValueAsString(defaultConfiguration);
LOGGER.debug("Default Dropwizard configuration:\n{}", defaultConfigurationString);
LOGGER.debug("Dropwizard configuration default values:\n{}", defaultConfigurationString);
}
}

Expand Down
@@ -0,0 +1,25 @@
/*
* Copyright 2024 Craig Motlin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.liftwizard.dropwizard.bundle.config.logging;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

@JsonInclude(Include.NON_DEFAULT)
public class JsonIncludeNonDefaultMixIn
{
}
@@ -0,0 +1,35 @@
/*
* Copyright 2024 Craig Motlin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.liftwizard.dropwizard.bundle.config.logging;

import com.fasterxml.jackson.databind.introspect.ClassIntrospector.MixInResolver;

public class JsonIncludeNonDefaultMixInResolver
implements MixInResolver
{
@Override
public Class<?> findMixInClassFor(Class<?> cls)
{
return JsonIncludeNonDefaultMixIn.class;
}

@Override
public MixInResolver copy()
{
return this;
}
}

0 comments on commit fae4180

Please sign in to comment.