From bbd7840785747eb89a03d854228d6cb727a40db1 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 7 Mar 2016 11:07:13 +0000 Subject: [PATCH] Allow LoggingSystem to be disabled In certain environments, such as Jetty configured with centralized logging, Spring Boot's logging system can be problematic. This commit adds support for using the existing LoggingSystem system property, configured with a value of none, to disable the logging system and rely on the standard logging configuration mechanism for whatever logging framework is in use. Closes gh-3571 --- .../main/asciidoc/spring-boot-features.adoc | 5 +++ .../boot/logging/LoggingSystem.java | 26 +++++++++++ .../boot/logging/LoggingSystemTests.java | 45 +++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemTests.java 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); + } + +}