From 69b72874ea940f42d8a35c52951446ec24ff8fa8 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sun, 12 Mar 2017 21:55:58 -0700 Subject: [PATCH] Extract logging listeners Separate logging ApplicationListeners classes to a different package so that the root `logging` package is no longer aware of `context` concerns. Fixes gh-8611 --- .../BackgroundPreinitializer.java | 4 +- .../devtools/RemoteSpringApplication.java | 4 +- .../src/checkstyle/import-control.xml | 6 ++ .../ClasspathLoggingApplicationListener.java | 5 +- .../logging/LoggingApplicationListener.java | 59 +++---------------- .../boot/context/logging/package-info.java | 20 +++++++ .../springframework/boot/logging/LogFile.java | 6 +- .../boot/logging/LoggingSystemProperties.java | 48 ++++++++++++--- .../main/resources/META-INF/spring.factories | 6 +- ...ngApplicationListenerIntegrationTests.java | 3 +- .../LoggingApplicationListenerTests.java | 8 ++- 11 files changed, 95 insertions(+), 74 deletions(-) rename spring-boot/src/main/java/org/springframework/boot/{ => context}/logging/ClasspathLoggingApplicationListener.java (95%) rename spring-boot/src/main/java/org/springframework/boot/{ => context}/logging/LoggingApplicationListener.java (88%) create mode 100644 spring-boot/src/main/java/org/springframework/boot/context/logging/package-info.java rename spring-boot/src/test/java/org/springframework/boot/{ => context}/logging/LoggingApplicationListenerIntegrationTests.java (96%) rename spring-boot/src/test/java/org/springframework/boot/{ => context}/logging/LoggingApplicationListenerTests.java (98%) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java index 43de3e2285..f1008e2b48 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -21,7 +21,7 @@ import javax.validation.Validation; import org.apache.catalina.mbeans.MBeanFactory; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; -import org.springframework.boot.logging.LoggingApplicationListener; +import org.springframework.boot.context.logging.LoggingApplicationListener; import org.springframework.context.ApplicationListener; import org.springframework.core.annotation.Order; import org.springframework.format.support.DefaultFormattingConversionService; diff --git a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/RemoteSpringApplication.java b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/RemoteSpringApplication.java index a2005bbe53..036075cd13 100644 --- a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/RemoteSpringApplication.java +++ b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/RemoteSpringApplication.java @@ -26,12 +26,12 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.WebApplicationType; import org.springframework.boot.context.config.AnsiOutputApplicationListener; import org.springframework.boot.context.config.ConfigFileApplicationListener; +import org.springframework.boot.context.logging.ClasspathLoggingApplicationListener; +import org.springframework.boot.context.logging.LoggingApplicationListener; import org.springframework.boot.devtools.remote.client.RemoteClientConfiguration; import org.springframework.boot.devtools.restart.RestartInitializer; import org.springframework.boot.devtools.restart.RestartScopeInitializer; import org.springframework.boot.devtools.restart.Restarter; -import org.springframework.boot.logging.ClasspathLoggingApplicationListener; -import org.springframework.boot.logging.LoggingApplicationListener; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationListener; import org.springframework.core.io.ClassPathResource; diff --git a/spring-boot-parent/src/checkstyle/import-control.xml b/spring-boot-parent/src/checkstyle/import-control.xml index d519b32ec6..72997b56cc 100644 --- a/spring-boot-parent/src/checkstyle/import-control.xml +++ b/spring-boot-parent/src/checkstyle/import-control.xml @@ -3,6 +3,12 @@ + + + + + + diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/ClasspathLoggingApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/logging/ClasspathLoggingApplicationListener.java similarity index 95% rename from spring-boot/src/main/java/org/springframework/boot/logging/ClasspathLoggingApplicationListener.java rename to spring-boot/src/main/java/org/springframework/boot/context/logging/ClasspathLoggingApplicationListener.java index e31eea4080..1f59c21a98 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/ClasspathLoggingApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/logging/ClasspathLoggingApplicationListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.logging; +package org.springframework.boot.context.logging; import java.net.URLClassLoader; import java.util.Arrays; @@ -36,6 +36,7 @@ import org.springframework.core.ResolvableType; * context class loader (TCCL) at {@code DEBUG} level. * * @author Andy Wilkinson + * @since 2.0.0 */ public final class ClasspathLoggingApplicationListener implements GenericApplicationListener { diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java similarity index 88% rename from spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java rename to spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java index 9625b3c1bc..5292b86689 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.logging; +package org.springframework.boot.context.logging; import java.util.List; import java.util.Map; @@ -31,6 +31,11 @@ import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEven import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.event.ApplicationStartingEvent; +import org.springframework.boot.logging.LogFile; +import org.springframework.boot.logging.LogLevel; +import org.springframework.boot.logging.LoggingInitializationContext; +import org.springframework.boot.logging.LoggingSystem; +import org.springframework.boot.logging.LoggingSystemProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; @@ -72,6 +77,7 @@ import org.springframework.util.StringUtils; * @author Dave Syer * @author Phillip Webb * @author Andy Wilkinson + * @since 2.0.0 * @see LoggingSystem#get(ClassLoader) */ public class LoggingApplicationListener implements GenericApplicationListener { @@ -94,57 +100,6 @@ public class LoggingApplicationListener implements GenericApplicationListener { */ public static final String REGISTER_SHUTDOWN_HOOK_PROPERTY = "logging.register-shutdown-hook"; - /** - * The name of the Spring property that contains the directory where log files are - * written. - * @deprecated as of 1.5 in favor of {@link LogFile#PATH_PROPERTY} - */ - @Deprecated - public static final String PATH_PROPERTY = LogFile.PATH_PROPERTY; - - /** - * The name of the Spring property that contains the name of the log file. Names can - * be an exact location or relative to the current directory. - * @deprecated as of 1.5 in favor of {@link LogFile#FILE_PROPERTY} - */ - @Deprecated - public static final String FILE_PROPERTY = LogFile.FILE_PROPERTY; - - /** - * The name of the System property that contains the process ID. - */ - public static final String PID_KEY = "PID"; - - /** - * The name of the System property that contains the exception conversion word. - */ - public static final String EXCEPTION_CONVERSION_WORD = "LOG_EXCEPTION_CONVERSION_WORD"; - - /** - * The name of the System property that contains the log file. - */ - public static final String LOG_FILE = "LOG_FILE"; - - /** - * The name of the System property that contains the log path. - */ - public static final String LOG_PATH = "LOG_PATH"; - - /** - * The name of the System property that contains the console log pattern. - */ - public static final String CONSOLE_LOG_PATTERN = "CONSOLE_LOG_PATTERN"; - - /** - * The name of the System property that contains the file log pattern. - */ - public static final String FILE_LOG_PATTERN = "FILE_LOG_PATTERN"; - - /** - * The name of the System property that contains the log level pattern. - */ - public static final String LOG_LEVEL_PATTERN = "LOG_LEVEL_PATTERN"; - /** * The name of the {@link LoggingSystem} bean. */ diff --git a/spring-boot/src/main/java/org/springframework/boot/context/logging/package-info.java b/spring-boot/src/main/java/org/springframework/boot/context/logging/package-info.java new file mode 100644 index 0000000000..99ea6e987a --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/context/logging/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2012-2017 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. + * 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. + */ + +/** + * Logging integration with Spring's Application Context. + */ +package org.springframework.boot.context.logging; diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/LogFile.java b/spring-boot/src/main/java/org/springframework/boot/logging/LogFile.java index 5d545b165b..39d8f050d6 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/LogFile.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/LogFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -83,8 +83,8 @@ public class LogFile { * @param properties the properties to apply to */ public void applyTo(Properties properties) { - put(properties, LoggingApplicationListener.LOG_PATH, this.path); - put(properties, LoggingApplicationListener.LOG_FILE, toString()); + put(properties, LoggingSystemProperties.LOG_PATH, this.path); + put(properties, LoggingSystemProperties.LOG_FILE, toString()); } private void put(Properties properties, String key, String value) { diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java index 4ad47f0816..da7c9e16c1 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystemProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 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. @@ -19,28 +19,60 @@ package org.springframework.boot.logging; import org.springframework.boot.ApplicationPid; import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.core.env.Environment; +import org.springframework.util.Assert; /** * Utility to set system properties that can later be used by log configuration files. * * @author Andy Wilkinson * @author Phillip Webb + * @since 2.0.0 */ -class LoggingSystemProperties { +public class LoggingSystemProperties { - static final String PID_KEY = LoggingApplicationListener.PID_KEY; + /** + * The name of the System property that contains the process ID. + */ + public static final String PID_KEY = "PID"; - static final String EXCEPTION_CONVERSION_WORD = LoggingApplicationListener.EXCEPTION_CONVERSION_WORD; + /** + * The name of the System property that contains the exception conversion word. + */ + public static final String EXCEPTION_CONVERSION_WORD = "LOG_EXCEPTION_CONVERSION_WORD"; - static final String CONSOLE_LOG_PATTERN = LoggingApplicationListener.CONSOLE_LOG_PATTERN; + /** + * The name of the System property that contains the log file. + */ + public static final String LOG_FILE = "LOG_FILE"; - static final String FILE_LOG_PATTERN = LoggingApplicationListener.FILE_LOG_PATTERN; + /** + * The name of the System property that contains the log path. + */ + public static final String LOG_PATH = "LOG_PATH"; - static final String LOG_LEVEL_PATTERN = LoggingApplicationListener.LOG_LEVEL_PATTERN; + /** + * The name of the System property that contains the console log pattern. + */ + public static final String CONSOLE_LOG_PATTERN = "CONSOLE_LOG_PATTERN"; + + /** + * The name of the System property that contains the file log pattern. + */ + public static final String FILE_LOG_PATTERN = "FILE_LOG_PATTERN"; + + /** + * The name of the System property that contains the log level pattern. + */ + public static final String LOG_LEVEL_PATTERN = "LOG_LEVEL_PATTERN"; private final Environment environment; - LoggingSystemProperties(Environment environment) { + /** + * Create a new {@link LoggingSystemProperties} instance. + * @param environment the source environment + */ + public LoggingSystemProperties(Environment environment) { + Assert.notNull(environment, "Environment must not be null"); this.environment = environment; } diff --git a/spring-boot/src/main/resources/META-INF/spring.factories b/spring-boot/src/main/resources/META-INF/spring.factories index be922ea469..f5dd50ab5e 100644 --- a/spring-boot/src/main/resources/META-INF/spring.factories +++ b/spring-boot/src/main/resources/META-INF/spring.factories @@ -22,9 +22,9 @@ org.springframework.boot.context.FileEncodingApplicationListener,\ org.springframework.boot.context.config.AnsiOutputApplicationListener,\ org.springframework.boot.context.config.ConfigFileApplicationListener,\ org.springframework.boot.context.config.DelegatingApplicationListener,\ -org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener,\ -org.springframework.boot.logging.ClasspathLoggingApplicationListener,\ -org.springframework.boot.logging.LoggingApplicationListener +org.springframework.boot.context.logging.ClasspathLoggingApplicationListener,\ +org.springframework.boot.context.logging.LoggingApplicationListener,\ +org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener # Environment Post Processors org.springframework.boot.env.EnvironmentPostProcessor=\ diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerIntegrationTests.java b/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerIntegrationTests.java similarity index 96% rename from spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerIntegrationTests.java rename to spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerIntegrationTests.java index 46c3d6cc67..f33e22876c 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerIntegrationTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerIntegrationTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.logging; +package org.springframework.boot.context.logging; import org.junit.Rule; import org.junit.Test; @@ -24,6 +24,7 @@ import org.slf4j.LoggerFactory; import org.springframework.boot.WebApplicationType; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.event.ApplicationStartingEvent; +import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.testutil.InternalOutputCapture; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java similarity index 98% rename from spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java rename to spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java index 64561e9155..aa60edf786 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.logging; +package org.springframework.boot.context.logging; import java.io.File; import java.io.IOException; @@ -40,6 +40,12 @@ import org.springframework.boot.ApplicationPid; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.boot.context.event.ApplicationStartingEvent; +import org.springframework.boot.logging.AbstractLoggingSystem; +import org.springframework.boot.logging.LogFile; +import org.springframework.boot.logging.LogLevel; +import org.springframework.boot.logging.LoggerConfiguration; +import org.springframework.boot.logging.LoggingInitializationContext; +import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.logging.java.JavaLoggingSystem; import org.springframework.boot.testutil.InternalOutputCapture; import org.springframework.context.ApplicationEvent;