Skip to content

Commit

Permalink
Skip redundant LoggerContext reset
Browse files Browse the repository at this point in the history
  • Loading branch information
mches committed Aug 16, 2024
1 parent dd2ed5f commit 4589afb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
* @author Dave Syer
* @author Andy Wilkinson
* @author Ben Hale
* @author Mark Chesney
* @since 1.0.0
*/
public class LogbackLoggingSystem extends AbstractLoggingSystem implements BeanFactoryInitializationAotProcessor {
Expand Down Expand Up @@ -210,7 +211,7 @@ private boolean initializeFromAotGeneratedArtifactsIfPossible(LoggingInitializat
applySystemProperties(initializationContext.getEnvironment(), logFile);
}
LoggerContext loggerContext = getLoggerContext();
stopAndReset(loggerContext);
stopOrReset(loggerContext);
SpringBootJoranConfigurator configurator = new SpringBootJoranConfigurator(initializationContext);
configurator.setContext(loggerContext);
boolean configuredUsingAotGeneratedArtifacts = configurator.configureUsingAotGeneratedArtifacts();
Expand All @@ -223,7 +224,7 @@ private boolean initializeFromAotGeneratedArtifactsIfPossible(LoggingInitializat
@Override
protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) {
LoggerContext context = getLoggerContext();
stopAndReset(context);
stopOrReset(context);
withLoggingSuppressed(() -> {
boolean debug = Boolean.getBoolean("logback.debug");
if (debug) {
Expand All @@ -245,7 +246,7 @@ protected void loadDefaults(LoggingInitializationContext initializationContext,
protected void loadConfiguration(LoggingInitializationContext initializationContext, String location,
LogFile logFile) {
LoggerContext loggerContext = getLoggerContext();
stopAndReset(loggerContext);
stopOrReset(loggerContext);
withLoggingSuppressed(() -> {
if (initializationContext != null) {
applySystemProperties(initializationContext.getEnvironment(), logFile);
Expand Down Expand Up @@ -298,9 +299,13 @@ private void configureByResourceUrl(LoggingInitializationContext initializationC
}
}

private void stopAndReset(LoggerContext loggerContext) {
loggerContext.stop();
loggerContext.reset();
private void stopOrReset(LoggerContext loggerContext) {
if (loggerContext.isStarted()) {
loggerContext.stop();
}
else {
loggerContext.reset();
}
if (isBridgeHandlerInstalled()) {
addLevelChangePropagator(loggerContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
Expand Down Expand Up @@ -93,8 +94,9 @@
* @author Scott Frederick
* @author Jonatan Ivanov
* @author Moritz Halbritter
* @author Mark Chesney
*/
@ExtendWith(OutputCaptureExtension.class)
@ExtendWith({ MockitoExtension.class, OutputCaptureExtension.class })
@ClassPathExclusions({ "log4j-core-*.jar", "log4j-api-*.jar" })
class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {

Expand Down

0 comments on commit 4589afb

Please sign in to comment.