Merge branch '1.3.x'

This commit is contained in:
Phillip Webb 2016-03-28 12:52:49 -07:00
commit 07313d1245
8 changed files with 125 additions and 35 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* 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.
@ -16,6 +16,7 @@
package org.springframework.boot.logging;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
@ -168,4 +169,8 @@ public abstract class AbstractLoggingSystem extends LoggingSystem {
return defaultPath;
}
protected final void applySystemProperties(Environment environment, LogFile logFile) {
new LoggingSytemProperties(environment).apply(logFile);
}
}

View File

@ -25,7 +25,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.ApplicationPid;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
@ -103,12 +102,12 @@ public class LoggingApplicationListener implements GenericApplicationListener {
/**
* The name of the System property that contains the process ID.
*/
public static final String PID_KEY = "PID";
public static final String PID_KEY = LoggingSytemProperties.PID_KEY;
/**
* The name of the System property that contains the exception conversion word.
*/
public static final String EXCEPTION_CONVERSION_WORD = "LOG_EXCEPTION_CONVERSION_WORD";
public static final String EXCEPTION_CONVERSION_WORD = LoggingSytemProperties.EXCEPTION_CONVERSION_WORD;
/**
* The name of the System property that contains the log file.
@ -123,17 +122,17 @@ public class LoggingApplicationListener implements GenericApplicationListener {
/**
* The name of the System property that contains the console log pattern.
*/
public static final String CONSOLE_LOG_PATTERN = "CONSOLE_LOG_PATTERN";
public static final String CONSOLE_LOG_PATTERN = LoggingSytemProperties.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";
public static final String FILE_LOG_PATTERN = LoggingSytemProperties.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";
public static final String LOG_LEVEL_PATTERN = LoggingSytemProperties.LOG_LEVEL_PATTERN;
/**
* The name of the {@link LoggingSystem} bean.
@ -248,7 +247,7 @@ public class LoggingApplicationListener implements GenericApplicationListener {
*/
protected void initialize(ConfigurableEnvironment environment,
ClassLoader classLoader) {
setSystemProperties(environment);
new LoggingSytemProperties(environment).apply();
LogFile logFile = LogFile.get(environment);
if (logFile != null) {
logFile.applyToSystemProperties();
@ -259,28 +258,6 @@ public class LoggingApplicationListener implements GenericApplicationListener {
registerShutdownHookIfNecessary(environment, this.loggingSystem);
}
private void setSystemProperties(ConfigurableEnvironment environment) {
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(
environment, "logging.");
setSystemProperty(propertyResolver, EXCEPTION_CONVERSION_WORD,
"exception-conversion-word");
setSystemProperty(propertyResolver, CONSOLE_LOG_PATTERN, "pattern.console");
setSystemProperty(propertyResolver, FILE_LOG_PATTERN, "pattern.file");
setSystemProperty(propertyResolver, LOG_LEVEL_PATTERN, "pattern.level");
setSystemProperty(PID_KEY, new ApplicationPid().toString());
}
private void setSystemProperty(RelaxedPropertyResolver propertyResolver,
String systemPropertyName, String propertyName) {
setSystemProperty(systemPropertyName, propertyResolver.getProperty(propertyName));
}
private void setSystemProperty(String name, String value) {
if (System.getProperty(name) == null && value != null) {
System.setProperty(name, value);
}
}
private void initializeEarlyLoggingLevel(ConfigurableEnvironment environment) {
if (this.parseArgs && this.springBootLogging == null) {
if (isSet(environment, "debug")) {

View File

@ -0,0 +1,76 @@
/*
* 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.springframework.boot.ApplicationPid;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.core.env.Environment;
/**
* Utility to set system properties that can later be used by log configuration files.
*
* @author Andy Wilkinson
* @author Phillip Webb
*/
class LoggingSytemProperties {
static final String PID_KEY = "PID";
static final String EXCEPTION_CONVERSION_WORD = "LOG_EXCEPTION_CONVERSION_WORD";
static final String CONSOLE_LOG_PATTERN = "CONSOLE_LOG_PATTERN";
static final String FILE_LOG_PATTERN = "FILE_LOG_PATTERN";
static final String LOG_LEVEL_PATTERN = "LOG_LEVEL_PATTERN";
private final Environment environment;
LoggingSytemProperties(Environment environment) {
this.environment = environment;
}
public void apply() {
apply(null);
}
public void apply(LogFile logFile) {
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(
this.environment, "logging.");
setSystemProperty(propertyResolver, EXCEPTION_CONVERSION_WORD,
"exception-conversion-word");
setSystemProperty(propertyResolver, CONSOLE_LOG_PATTERN, "pattern.console");
setSystemProperty(propertyResolver, FILE_LOG_PATTERN, "pattern.file");
setSystemProperty(propertyResolver, LOG_LEVEL_PATTERN, "pattern.level");
setSystemProperty(PID_KEY, new ApplicationPid().toString());
if (logFile != null) {
logFile.applyToSystemProperties();
}
}
private void setSystemProperty(RelaxedPropertyResolver propertyResolver,
String systemPropertyName, String propertyName) {
setSystemProperty(systemPropertyName, propertyResolver.getProperty(propertyName));
}
private void setSystemProperty(String name, String value) {
if (System.getProperty(name) == null && value != null) {
System.setProperty(name, value);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* 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.
@ -18,6 +18,7 @@ package org.springframework.boot.logging;
import org.slf4j.bridge.SLF4JBridgeHandler;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
@ -45,6 +46,15 @@ public abstract class Slf4JLoggingSystem extends AbstractLoggingSystem {
removeJdkLoggingBridgeHandler();
}
@Override
protected void loadConfiguration(LoggingInitializationContext initializationContext,
String location, LogFile logFile) {
Assert.notNull(location, "Location must not be null");
if (initializationContext != null) {
applySystemProperties(initializationContext.getEnvironment(), logFile);
}
}
private void configureJdkLoggingBridgeHandler() {
try {
if (isBridgeHandlerAvailable()) {

View File

@ -152,6 +152,7 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
@Override
protected void loadConfiguration(LoggingInitializationContext initializationContext,
String location, LogFile logFile) {
super.loadConfiguration(initializationContext, location, logFile);
loadConfiguration(location, logFile);
}

View File

@ -128,7 +128,7 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem {
@Override
protected void loadConfiguration(LoggingInitializationContext initializationContext,
String location, LogFile logFile) {
Assert.notNull(location, "Location must not be null");
super.loadConfiguration(initializationContext, location, logFile);
LoggerContext loggerContext = getLoggerContext();
stopAndReset(loggerContext);
try {

View File

@ -62,8 +62,15 @@ public abstract class AbstractLoggingSystemTests {
}
protected final LogFile getLogFile(String file, String path) {
return getLogFile(file, path, true);
}
protected final LogFile getLogFile(String file, String path,
boolean applyToSystemProperties) {
LogFile logFile = new LogFile(file, path);
logFile.applyToSystemProperties();
if (applyToSystemProperties) {
logFile.applyToSystemProperties();
}
return logFile;
}

View File

@ -68,11 +68,13 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
private LoggingInitializationContext initializationContext;
private MockEnvironment environment;
@Before
public void setup() {
this.logger = new SLF4JLogFactory().getInstance(getClass().getName());
this.initializationContext = new LoggingInitializationContext(
new MockEnvironment());
this.environment = new MockEnvironment();
this.initializationContext = new LoggingInitializationContext(this.environment);
}
@Override
@ -301,6 +303,18 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
}
}
@Test
public void reinitializeShouldSetSytemProperty() throws Exception {
// gh-5491
this.loggingSystem.beforeInitialize();
this.logger.info("Hidden");
this.loggingSystem.initialize(this.initializationContext, null, null);
LogFile logFile = getLogFile(tmpDir() + "/example.log", null, false);
this.loggingSystem.initialize(this.initializationContext,
"classpath:logback-nondefault.xml", logFile);
assertThat(System.getProperty("LOG_FILE")).endsWith("example.log");
}
private String getLineWithText(File file, String outputSearch) throws Exception {
return getLineWithText(FileCopyUtils.copyToString(new FileReader(file)),
outputSearch);