Home / Class/ JavaLoggingSystemTests Class — spring-boot Architecture

JavaLoggingSystemTests Class — spring-boot Architecture

Architecture documentation for the JavaLoggingSystemTests class in JavaLoggingSystemTests.java from the spring-boot codebase.

Entity Profile

Relationship Graph

Source Code

core/spring-boot/src/test/java/org/springframework/boot/logging/java/JavaLoggingSystemTests.java lines 54–206

@ExtendWith(OutputCaptureExtension.class)
class JavaLoggingSystemTests extends AbstractLoggingSystemTests {

	private static final FileFilter SPRING_LOG_FILTER = (pathname) -> pathname.getName().startsWith("spring.log");

	private final JavaLoggingSystem loggingSystem = new JavaLoggingSystem(getClass().getClassLoader());

	private Logger logger;

	private Locale defaultLocale;

	@BeforeEach
	void init() {
		this.logger = Logger.getLogger(getClass().getName());
		this.defaultLocale = Locale.getDefault();
		Locale.setDefault(Locale.ENGLISH);
	}

	@AfterEach
	void resetLogger() {
		this.logger.setLevel(Level.OFF);
		this.loggingSystem.getShutdownHandler().run();
	}

	@AfterEach
	void restoreLocale() {
		Locale.setDefault(this.defaultLocale);
	}

	@Test
	void noFile(CapturedOutput output) {
		this.loggingSystem.beforeInitialize();
		this.logger.info("Hidden");
		this.loggingSystem.initialize(getInitializationContext(), null, null);
		this.logger.info("Hello world");
		assertThat(output).contains("Hello world").doesNotContain("Hidden");
		assertThat(new File(tmpDir() + "/spring.log")).doesNotExist();
	}

	@Test
	void withFile(CapturedOutput output) {
		File temp = new File(tmpDir());
		File[] logFiles = temp.listFiles(SPRING_LOG_FILTER);
		for (File file : logFiles) {
			file.delete();
		}
		this.loggingSystem.beforeInitialize();
		this.logger.info("Hidden");
		this.loggingSystem.initialize(getInitializationContext(), null, getLogFile(null, tmpDir()));
		this.logger.info("Hello world");
		assertThat(output).contains("Hello world").doesNotContain("Hidden");
		assertThat(temp.listFiles(SPRING_LOG_FILTER)).isNotEmpty();
	}

	@Test
	void testCustomFormatter(CapturedOutput output) {
		this.loggingSystem.beforeInitialize();
		this.loggingSystem.initialize(getInitializationContext(), null, null);
		this.logger.info("Hello world");
		assertThat(output).contains("Hello world").contains("???? INFO [");
	}

	@Test
	void testSystemPropertyInitializesFormat(CapturedOutput output) {
		System.setProperty(LoggingSystemProperty.PID.getEnvironmentVariableName(), "1234");
		this.loggingSystem.beforeInitialize();
		this.loggingSystem.initialize(getInitializationContext(),
				"classpath:" + ClassUtils.addResourcePathToPackagePath(getClass(), "logging.properties"), null);
		this.logger.info("Hello world");
		this.logger.info("Hello world");
		assertThat(output).contains("Hello world").contains("1234 INFO [");
	}

	@Test
	@WithResource(name = "logging-nondefault.properties", content = """
			handlers = java.util.logging.ConsoleHandler
			.level = INFO
			""")
	void testNonDefaultConfigLocation(CapturedOutput output) {
		this.loggingSystem.beforeInitialize();
		this.loggingSystem.initialize(getInitializationContext(), "classpath:logging-nondefault.properties", null);
		this.logger.info("Hello world");
		assertThat(output).contains("INFO: Hello");
	}

	@Test
	void testNonexistentConfigLocation() {
		this.loggingSystem.beforeInitialize();
		assertThatIllegalStateException().isThrownBy(() -> this.loggingSystem.initialize(getInitializationContext(),
				"classpath:logging-nonexistent.properties", null));
	}

	@Test
	void getSupportedLevels() {
		assertThat(this.loggingSystem.getSupportedLogLevels()).isEqualTo(
				EnumSet.of(LogLevel.TRACE, LogLevel.DEBUG, LogLevel.INFO, LogLevel.WARN, LogLevel.ERROR, LogLevel.OFF));
	}

	@Test
	void setLevel(CapturedOutput output) {
		this.loggingSystem.beforeInitialize();
		this.loggingSystem.initialize(getInitializationContext(), null, null);
		this.logger.fine("Hello");
		this.loggingSystem.setLogLevel("org.springframework.boot", LogLevel.DEBUG);
		this.logger.fine("Hello");
		assertThat(StringUtils.countOccurrencesOf(output.toString(), "Hello")).isOne();
	}

	@Test
	void setLevelToNull(CapturedOutput output) {
		this.loggingSystem.beforeInitialize();
		this.loggingSystem.initialize(getInitializationContext(), null, null);
		this.logger.fine("Hello");
		this.loggingSystem.setLogLevel("org.springframework.boot", LogLevel.DEBUG);
		this.logger.fine("Hello");
		this.loggingSystem.setLogLevel("org.springframework.boot", null);
		this.logger.fine("Hello");
		assertThat(StringUtils.countOccurrencesOf(output.toString(), "Hello")).isOne();
	}

	@Test
	void getLoggerConfigurations() {
		this.loggingSystem.beforeInitialize();
		this.loggingSystem.initialize(getInitializationContext(), null, null);
		this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG);
		List<LoggerConfiguration> configurations = this.loggingSystem.getLoggerConfigurations();
		assertThat(configurations).isNotEmpty();
		assertThat(configurations.get(0).getName()).isEqualTo(LoggingSystem.ROOT_LOGGER_NAME);
	}

	@Test
	void getLoggerConfiguration() {
		this.loggingSystem.beforeInitialize();
		this.loggingSystem.initialize(getInitializationContext(), null, null);
		this.loggingSystem.setLogLevel(getClass().getName(), LogLevel.DEBUG);
		LoggerConfiguration configuration = this.loggingSystem.getLoggerConfiguration(getClass().getName());
		assertThat(configuration)
			.isEqualTo(new LoggerConfiguration(getClass().getName(), LogLevel.DEBUG, LogLevel.DEBUG));
	}

	@Test
	void shouldNotContainAnsiEscapeCodes(CapturedOutput output) {
		this.loggingSystem.beforeInitialize();
		this.loggingSystem.initialize(getInitializationContext(), null, null);
		this.logger.info("Hello world");
		assertThat(output).doesNotContain("\033[");
	}

	private LoggingInitializationContext getInitializationContext() {
		return new LoggingInitializationContext(null);
	}

}

Domain

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free