Commit bbd78407 authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent a578488b
......@@ -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
......
......@@ -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<String, String> 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<String, String> 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) {
}
}
}
/*
* 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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment