Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ private static void setLogLevelBasedOnSamplingRate(double samplingRate) {
}

float sample = SAMPLER.get().nextFloat();
if (effectiveSamplingRate > sample) {
// only change the log level if it is less verbose than DEBUG, i.e. don't change from e.g. TRACE to DEBUG
if (effectiveSamplingRate > sample && !LOG.isTraceEnabled()) {
LoggingManager loggingManager = LoggingManagerRegistry.getLoggingManager();
loggingManager.setLogLevel(Level.DEBUG);
LOG.debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,34 @@ void initializeLogging_withInvalidSamplingRate_shouldSkipSampling() {
assertThat(MDC.get(PowertoolsLoggedFields.SAMPLING_RATE.getName())).isNull();
}

@Test
void initializeLogging_withSamplingRate_whenLogLevelIsTrace_shouldNotLowerLevelToDebug() throws IllegalAccessException {
// GIVEN
writeStaticField(LoggingConstants.class, "POWERTOOLS_LOG_LEVEL", "TRACE", true);
reinitializeLogLevel();
assertThat(LOG.isTraceEnabled()).isTrue();

// WHEN
PowertoolsLogging.initializeLogging(context, 1.0);

// THEN
assertThat(LOG.isTraceEnabled()).isTrue();
}

@Test
void initializeLogging_withSamplingRate_whenLogLevelIsInfo_shouldRaiseLevelToDebug() throws IllegalAccessException {
// GIVEN
writeStaticField(LoggingConstants.class, "POWERTOOLS_LOG_LEVEL", "INFO", true);
reinitializeLogLevel();
assertThat(LOG.isDebugEnabled()).isFalse();

// WHEN
PowertoolsLogging.initializeLogging(context, 1.0);

// THEN
assertThat(LOG.isDebugEnabled()).isTrue();
}

@Test
void initializeLogging_withEnvVarAndParameter_shouldUseEnvVarPrecedence() throws IllegalAccessException {
// GIVEN
Expand Down