diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index ef7b8e4a7e..16af70c90a 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -1204,6 +1204,11 @@ the classpath, and further customized by providing a suitable configuration file root of the classpath, or in a location specified by the Spring `Environment` property `logging.config`. +You can force Spring Boot to use a particular logging system using the +`org.springframework.boot.logging.LoggingSystem` system property. The value should be +the fully-qualified class name of a `LoggingSystem` implementation. You can also disable +Spring Boot's logging configuration entirely by used a value of `none`. + NOTE: Since logging is initialized *before* the `ApplicationContext` is created, it isn't possible to control logging from `@PropertySources` in Spring `@Configuration` files. System properties and the conventional Spring Boot external configuration files work just diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java index bb63ed1749..4664f8fde6 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingSystem.java @@ -37,6 +37,12 @@ public abstract class LoggingSystem { */ public static final String SYSTEM_PROPERTY = LoggingSystem.class.getName(); + /** + * The value of the {@link #SYSTEM_PROPERTY} that can be used to indicate that no + * {@link LoggingSystem} should be used. + */ + public static final String NONE = "none"; + private static final Map SYSTEMS; static { @@ -101,6 +107,9 @@ public abstract class LoggingSystem { public static LoggingSystem get(ClassLoader classLoader) { String loggingSystem = System.getProperty(SYSTEM_PROPERTY); if (StringUtils.hasLength(loggingSystem)) { + if (NONE.equals(loggingSystem)) { + return new NoOpLoggingSystem(); + } return get(classLoader, loggingSystem); } for (Map.Entry entry : SYSTEMS.entrySet()) { @@ -122,4 +131,21 @@ public abstract class LoggingSystem { } } + /** + * {@link LoggingSystem} that does nothing. + */ + static class NoOpLoggingSystem extends LoggingSystem { + + @Override + public void beforeInitialize() { + + } + + @Override + public void setLogLevel(String loggerName, LogLevel level) { + + } + + } + } diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemTests.java new file mode 100644 index 0000000000..56e263c9f5 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemTests.java @@ -0,0 +1,45 @@ +/* + * Copyright 2012-2016 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. + */ + +package org.springframework.boot.logging; + +import org.junit.After; +import org.junit.Test; + +import org.springframework.boot.logging.LoggingSystem.NoOpLoggingSystem; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link LoggingSystem}. + * + * @author Andy Wilkinson + */ +public class LoggingSystemTests { + + @After + public void clearSystemProperty() { + System.clearProperty(LoggingSystem.SYSTEM_PROPERTY); + } + + @Test + public void loggingSystemCanBeDisabled() { + System.setProperty(LoggingSystem.SYSTEM_PROPERTY, LoggingSystem.NONE); + LoggingSystem loggingSystem = LoggingSystem.get(getClass().getClassLoader()); + assertThat(loggingSystem).isInstanceOf(NoOpLoggingSystem.class); + } + +}