Restore Spring Boot 1.1 logging behavior
Refactor LoggingApplicationListener and LoggingSystem to restore Spring Boot 1.1 logging behavior. The LOG_FILE and LOG_PATH system properties are now set before configuring the logger. The `logging.path` property is now once again optional and will not be used when `logging.file` is specified. The documentation has also been updated to reflect the changes. Fixes gh-2121 Fixes gh-2117
This commit is contained in:
@@ -760,40 +760,35 @@ detection.
|
|||||||
[[boot-features-logging-file-output]]
|
[[boot-features-logging-file-output]]
|
||||||
=== File output
|
=== File output
|
||||||
By default, Spring Boot will only log to the console and will not write log files. If you
|
By default, Spring Boot will only log to the console and will not write log files. If you
|
||||||
want to write log files in addition to the console output you need to set the
|
want to write log files in addition to the console output you need to set a
|
||||||
`logging.file` and/or `logging.path` properties (for example in your
|
`logging.file` or `logging.path` property (for example in your `application.properties`).
|
||||||
`application.properties`). Log files will rotate when they reach 10 Mb.
|
|
||||||
|
|
||||||
As with console output, `ERROR`, `WARN` and `INFO` level messages are logged by default.
|
|
||||||
|
|
||||||
The following table shows how the `logging.*` properties can be used together:
|
The following table shows how the `logging.*` properties can be used together:
|
||||||
|
|
||||||
.Logging properties
|
.Logging properties
|
||||||
[cols="1,1,1,4"]
|
[cols="1,1,1,4"]
|
||||||
|===
|
|===
|
||||||
|logging.path |logging.file |Example |Description
|
|`logging.file` |`logging.path` |Example |Description
|
||||||
|
|
||||||
|_(none)_
|
|_(none)_
|
||||||
|Exact location
|
|
||||||
|`./my.log`
|
|
||||||
|Writes to the specified file.
|
|
||||||
|
|
||||||
|_(none)_
|
|_(none)_
|
||||||
|Simple name
|
|
|
||||||
|
|Console only logging.
|
||||||
|
|
||||||
|
|Specific file
|
||||||
|
|_(none)_
|
||||||
|`my.log`
|
|`my.log`
|
||||||
|Writes the given file in the `temp` folder.
|
|Writes to the specified log file. Names can be an exact location or relative to the
|
||||||
|
current directory.
|
||||||
|
|
||||||
|Specific folder
|
|
||||||
|Simple name
|
|
||||||
|`/logs` & `my.log`
|
|
||||||
|Writes the given file in the specified folder.
|
|
||||||
|
|
||||||
|Specific folder
|
|
||||||
|_(none)_
|
|_(none)_
|
||||||
|`/logs`
|
|Specific folder
|
||||||
|Writes the `spring.log` in the specified folder.
|
|`/var/log`
|
||||||
|
|Writes `spring.log` the specified folder. Names can be an exact location or relative to the
|
||||||
|
current directory.
|
||||||
|===
|
|===
|
||||||
|
|
||||||
|
Log files will rotate when they reach 10 Mb and as with console output, `ERROR`, `WARN`
|
||||||
|
and `INFO` level messages are logged by default.
|
||||||
|
|
||||||
|
|
||||||
[[boot-features-custom-log-levels]]
|
[[boot-features-custom-log-levels]]
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ public abstract class AbstractLoggingSystem extends LoggingSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(String configLocation, String logFile) {
|
public void initialize(String configLocation, LogFile logFile) {
|
||||||
if (StringUtils.hasLength(configLocation)) {
|
if (StringUtils.hasLength(configLocation)) {
|
||||||
// Load a specific configuration
|
// Load a specific configuration
|
||||||
configLocation = SystemPropertyUtils.resolvePlaceholders(configLocation);
|
configLocation = SystemPropertyUtils.resolvePlaceholders(configLocation);
|
||||||
@@ -52,7 +52,7 @@ public abstract class AbstractLoggingSystem extends LoggingSystem {
|
|||||||
// No self initialization has occurred, use defaults
|
// No self initialization has occurred, use defaults
|
||||||
loadDefaults(logFile);
|
loadDefaults(logFile);
|
||||||
}
|
}
|
||||||
else if (StringUtils.hasLength(logFile)) {
|
else if (logFile != null) {
|
||||||
// Self initialization has occurred but the file has changed, reload
|
// Self initialization has occurred but the file has changed, reload
|
||||||
loadConfiguration(selfInitializationConfig, logFile);
|
loadConfiguration(selfInitializationConfig, logFile);
|
||||||
}
|
}
|
||||||
@@ -84,14 +84,14 @@ public abstract class AbstractLoggingSystem extends LoggingSystem {
|
|||||||
* Load sensible defaults for the logging system.
|
* Load sensible defaults for the logging system.
|
||||||
* @param logFile the file to load or {@code null} if no log file is to be written
|
* @param logFile the file to load or {@code null} if no log file is to be written
|
||||||
*/
|
*/
|
||||||
protected abstract void loadDefaults(String logFile);
|
protected abstract void loadDefaults(LogFile logFile);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load a specific configuration.
|
* Load a specific configuration.
|
||||||
* @param location the location of the configuration to load (never {@code null})
|
* @param location the location of the configuration to load (never {@code null})
|
||||||
* @param logFile the file to load or {@code null} if no log file is to be written
|
* @param logFile the file to load or {@code null} if no log file is to be written
|
||||||
*/
|
*/
|
||||||
protected abstract void loadConfiguration(String location, String logFile);
|
protected abstract void loadConfiguration(String location, LogFile logFile);
|
||||||
|
|
||||||
protected final ClassLoader getClassLoader() {
|
protected final ClassLoader getClassLoader() {
|
||||||
return this.classLoader;
|
return this.classLoader;
|
||||||
|
|||||||
@@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2014 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 java.util.Properties;
|
||||||
|
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.core.env.PropertyResolver;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A reference to a log output file. Log output files are specified using
|
||||||
|
* {@code logging.file} or {@code logging.path} {@link Environment} properties. If the
|
||||||
|
* {@code logging.file} property is not specified {@code "spring.log"} will be written in
|
||||||
|
* the {@code logging.path} directory.
|
||||||
|
*
|
||||||
|
* @author Phillip Webb
|
||||||
|
* @since 1.2.1
|
||||||
|
* @see #get(PropertyResolver)
|
||||||
|
*/
|
||||||
|
public class LogFile {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the Spring property that contains the name of the logging configuration
|
||||||
|
* file.
|
||||||
|
*/
|
||||||
|
public static final String FILE_PROPERTY = "logging.file";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the Spring property that contains the path where the logging
|
||||||
|
* configuration can be found.
|
||||||
|
*/
|
||||||
|
public static final String PATH_PROPERTY = "logging.path";
|
||||||
|
|
||||||
|
private final String file;
|
||||||
|
|
||||||
|
private final String path;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link LogFile} instance.
|
||||||
|
* @param file a reference to the file to write
|
||||||
|
*/
|
||||||
|
LogFile(String file) {
|
||||||
|
this(file, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new {@link LogFile} instance.
|
||||||
|
* @param file a reference to the file to write
|
||||||
|
* @param path a reference to the logging path to use if {@code file} is not specified
|
||||||
|
*/
|
||||||
|
LogFile(String file, String path) {
|
||||||
|
Assert.isTrue(StringUtils.hasLength(file) || StringUtils.hasLength(path),
|
||||||
|
"File or Path must not be empty");
|
||||||
|
this.file = file;
|
||||||
|
this.path = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply log file details to {@code LOG_PATH} and {@code LOG_FILE} system properties.
|
||||||
|
*/
|
||||||
|
public void applyToSystemProperties() {
|
||||||
|
applyTo(System.getProperties());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply log file details to {@code LOG_PATH} and {@code LOG_FILE} map entries.
|
||||||
|
*/
|
||||||
|
public void applyTo(Properties properties) {
|
||||||
|
put(properties, "LOG_PATH", this.path);
|
||||||
|
put(properties, "LOG_FILE", toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void put(Properties properties, String key, String value) {
|
||||||
|
if (StringUtils.hasLength(value)) {
|
||||||
|
properties.put(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
if (StringUtils.hasLength(this.file)) {
|
||||||
|
return this.file;
|
||||||
|
}
|
||||||
|
String path = this.path;
|
||||||
|
if (!path.endsWith("/")) {
|
||||||
|
path = path + "/";
|
||||||
|
}
|
||||||
|
return StringUtils.applyRelativePath(path, "spring.log");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a {@link LogFile} from the given Spring {@link Environment}.
|
||||||
|
* @param propertyResolver the {@link PropertyResolver} used to obtain the logging
|
||||||
|
* properties
|
||||||
|
* @return a {@link LogFile} or {@code null} if the environment didn't contain any
|
||||||
|
* suitable properties
|
||||||
|
*/
|
||||||
|
public static LogFile get(PropertyResolver propertyResolver) {
|
||||||
|
String file = propertyResolver.getProperty(FILE_PROPERTY);
|
||||||
|
String path = propertyResolver.getProperty(PATH_PROPERTY);
|
||||||
|
if (StringUtils.hasLength(file) || StringUtils.hasLength(path)) {
|
||||||
|
return new LogFile(file, path);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -71,13 +71,13 @@ public class LoggingApplicationListener implements SmartApplicationListener {
|
|||||||
* The name of the Spring property that contains the path where the logging
|
* The name of the Spring property that contains the path where the logging
|
||||||
* configuration can be found.
|
* configuration can be found.
|
||||||
*/
|
*/
|
||||||
public static final String PATH_PROPERTY = "logging.path";
|
public static final String PATH_PROPERTY = LogFile.PATH_PROPERTY;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The name of the Spring property that contains the name of the logging configuration
|
* The name of the Spring property that contains the name of the logging configuration
|
||||||
* file.
|
* file.
|
||||||
*/
|
*/
|
||||||
public static final String FILE_PROPERTY = "logging.file";
|
public static final String FILE_PROPERTY = LogFile.FILE_PROPERTY;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The name of the System property that contains the process ID.
|
* The name of the System property that contains the process ID.
|
||||||
@@ -160,7 +160,7 @@ public class LoggingApplicationListener implements SmartApplicationListener {
|
|||||||
|
|
||||||
private void initializeSystem(ConfigurableEnvironment environment,
|
private void initializeSystem(ConfigurableEnvironment environment,
|
||||||
LoggingSystem system) {
|
LoggingSystem system) {
|
||||||
String logFile = getLogFile(environment);
|
LogFile logFile = LogFile.get(environment);
|
||||||
String logConfig = environment.getProperty(CONFIG_PROPERTY);
|
String logConfig = environment.getProperty(CONFIG_PROPERTY);
|
||||||
if (StringUtils.hasLength(logConfig)) {
|
if (StringUtils.hasLength(logConfig)) {
|
||||||
try {
|
try {
|
||||||
@@ -179,24 +179,6 @@ public class LoggingApplicationListener implements SmartApplicationListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getLogFile(ConfigurableEnvironment environment) {
|
|
||||||
String file = environment.getProperty(FILE_PROPERTY);
|
|
||||||
String path = environment.getProperty(PATH_PROPERTY);
|
|
||||||
if (StringUtils.hasLength(path) || StringUtils.hasLength(file)) {
|
|
||||||
if (!StringUtils.hasLength(file)) {
|
|
||||||
file = "spring.log";
|
|
||||||
}
|
|
||||||
if (!StringUtils.hasLength(path) && !file.contains("/")) {
|
|
||||||
path = StringUtils.cleanPath(System.getProperty("java.io.tmpdir"));
|
|
||||||
}
|
|
||||||
if (StringUtils.hasLength(path)) {
|
|
||||||
return StringUtils.applyRelativePath(path, file);
|
|
||||||
}
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initializeFinalLoggingLevels(ConfigurableEnvironment environment,
|
private void initializeFinalLoggingLevels(ConfigurableEnvironment environment,
|
||||||
LoggingSystem system) {
|
LoggingSystem system) {
|
||||||
if (this.springBootLogging != null) {
|
if (this.springBootLogging != null) {
|
||||||
|
|||||||
@@ -46,8 +46,8 @@ public abstract class LoggingSystem {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Reset the logging system to be limit output. This method may be called before
|
* Reset the logging system to be limit output. This method may be called before
|
||||||
* {@link #initialize(String, String)} to reduce logging noise until the systems has
|
* {@link #initialize(String, LogFile)} to reduce logging noise until the
|
||||||
* been fully Initialized.
|
* systems has been fully Initialized.
|
||||||
*/
|
*/
|
||||||
public abstract void beforeInitialize();
|
public abstract void beforeInitialize();
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ public abstract class LoggingSystem {
|
|||||||
* @param logFile the log output file that should be written or {@code null} for
|
* @param logFile the log output file that should be written or {@code null} for
|
||||||
* console only output
|
* console only output
|
||||||
*/
|
*/
|
||||||
public abstract void initialize(String configLocation, String logFile);
|
public abstract void initialize(String configLocation, LogFile logFile);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the logging level for a given logger.
|
* Sets the logging level for a given logger.
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import java.util.logging.LogManager;
|
|||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.springframework.boot.logging.AbstractLoggingSystem;
|
import org.springframework.boot.logging.AbstractLoggingSystem;
|
||||||
|
import org.springframework.boot.logging.LogFile;
|
||||||
import org.springframework.boot.logging.LogLevel;
|
import org.springframework.boot.logging.LogLevel;
|
||||||
import org.springframework.boot.logging.LoggingSystem;
|
import org.springframework.boot.logging.LoggingSystem;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
@@ -70,8 +71,8 @@ public class JavaLoggingSystem extends AbstractLoggingSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void loadDefaults(String logFile) {
|
protected void loadDefaults(LogFile logFile) {
|
||||||
if (StringUtils.hasLength(logFile)) {
|
if (logFile != null) {
|
||||||
loadConfiguration(getPackagedConfigFile("logging-file.properties"), logFile);
|
loadConfiguration(getPackagedConfigFile("logging-file.properties"), logFile);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -80,14 +81,14 @@ public class JavaLoggingSystem extends AbstractLoggingSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void loadConfiguration(String location, String logFile) {
|
protected void loadConfiguration(String location, LogFile logFile) {
|
||||||
Assert.notNull(location, "Location must not be null");
|
Assert.notNull(location, "Location must not be null");
|
||||||
try {
|
try {
|
||||||
String configuration = FileCopyUtils.copyToString(new InputStreamReader(
|
String configuration = FileCopyUtils.copyToString(new InputStreamReader(
|
||||||
ResourceUtils.getURL(location).openStream()));
|
ResourceUtils.getURL(location).openStream()));
|
||||||
if (StringUtils.hasLength(logFile)) {
|
if (logFile != null) {
|
||||||
configuration = configuration.replace("${LOG_FILE}",
|
configuration = configuration.replace("${LOG_FILE}",
|
||||||
StringUtils.cleanPath(logFile));
|
StringUtils.cleanPath(logFile.toString()));
|
||||||
}
|
}
|
||||||
LogManager.getLogManager().readConfiguration(
|
LogManager.getLogManager().readConfiguration(
|
||||||
new ByteArrayInputStream(configuration.getBytes()));
|
new ByteArrayInputStream(configuration.getBytes()));
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import java.util.Map;
|
|||||||
import org.apache.log4j.Level;
|
import org.apache.log4j.Level;
|
||||||
import org.apache.log4j.LogManager;
|
import org.apache.log4j.LogManager;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
import org.springframework.boot.logging.LogFile;
|
||||||
import org.springframework.boot.logging.LogLevel;
|
import org.springframework.boot.logging.LogLevel;
|
||||||
import org.springframework.boot.logging.LoggingSystem;
|
import org.springframework.boot.logging.LoggingSystem;
|
||||||
import org.springframework.boot.logging.Slf4JLoggingSystem;
|
import org.springframework.boot.logging.Slf4JLoggingSystem;
|
||||||
@@ -67,8 +68,8 @@ public class Log4JLoggingSystem extends Slf4JLoggingSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void loadDefaults(String logFile) {
|
protected void loadDefaults(LogFile logFile) {
|
||||||
if (StringUtils.hasLength(logFile)) {
|
if (logFile != null) {
|
||||||
loadConfiguration(getPackagedConfigFile("log4j-file.properties"), logFile);
|
loadConfiguration(getPackagedConfigFile("log4j-file.properties"), logFile);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -77,10 +78,10 @@ public class Log4JLoggingSystem extends Slf4JLoggingSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void loadConfiguration(String location, String logFile) {
|
protected void loadConfiguration(String location, LogFile logFile) {
|
||||||
Assert.notNull(location, "Location must not be null");
|
Assert.notNull(location, "Location must not be null");
|
||||||
if (StringUtils.hasLength(logFile)) {
|
if (logFile != null) {
|
||||||
System.setProperty("LOG_FILE", logFile);
|
logFile.applyToSystemProperties();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Log4jConfigurer.initLogging(location);
|
Log4jConfigurer.initLogging(location);
|
||||||
|
|||||||
@@ -26,12 +26,12 @@ import org.apache.logging.log4j.LogManager;
|
|||||||
import org.apache.logging.log4j.core.LoggerContext;
|
import org.apache.logging.log4j.core.LoggerContext;
|
||||||
import org.apache.logging.log4j.core.config.ConfigurationFactory;
|
import org.apache.logging.log4j.core.config.ConfigurationFactory;
|
||||||
import org.apache.logging.log4j.core.config.ConfigurationSource;
|
import org.apache.logging.log4j.core.config.ConfigurationSource;
|
||||||
|
import org.springframework.boot.logging.LogFile;
|
||||||
import org.springframework.boot.logging.LogLevel;
|
import org.springframework.boot.logging.LogLevel;
|
||||||
import org.springframework.boot.logging.LoggingSystem;
|
import org.springframework.boot.logging.LoggingSystem;
|
||||||
import org.springframework.boot.logging.Slf4JLoggingSystem;
|
import org.springframework.boot.logging.Slf4JLoggingSystem;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ResourceUtils;
|
import org.springframework.util.ResourceUtils;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link LoggingSystem} for <a href="http://logging.apache.org/log4j/2.x/">Log4j 2</a>.
|
* {@link LoggingSystem} for <a href="http://logging.apache.org/log4j/2.x/">Log4j 2</a>.
|
||||||
@@ -71,8 +71,8 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void loadDefaults(String logFile) {
|
protected void loadDefaults(LogFile logFile) {
|
||||||
if (StringUtils.hasLength(logFile)) {
|
if (logFile != null) {
|
||||||
loadConfiguration(getPackagedConfigFile("log4j2-file.xml"), logFile);
|
loadConfiguration(getPackagedConfigFile("log4j2-file.xml"), logFile);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -81,10 +81,10 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void loadConfiguration(String location, String logFile) {
|
protected void loadConfiguration(String location, LogFile logFile) {
|
||||||
Assert.notNull(location, "Location must not be null");
|
Assert.notNull(location, "Location must not be null");
|
||||||
if (StringUtils.hasLength(logFile)) {
|
if (logFile != null) {
|
||||||
System.setProperty("LOG_FILE", logFile);
|
logFile.applyToSystemProperties();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
|
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ package org.springframework.boot.logging.logback;
|
|||||||
|
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.boot.logging.LogFile;
|
||||||
|
|
||||||
import ch.qos.logback.classic.Level;
|
import ch.qos.logback.classic.Level;
|
||||||
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
|
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
|
||||||
@@ -50,9 +50,9 @@ class DefaultLogbackConfiguration {
|
|||||||
|
|
||||||
private static final Charset UTF8 = Charset.forName("UTF-8");
|
private static final Charset UTF8 = Charset.forName("UTF-8");
|
||||||
|
|
||||||
private final String logFile;
|
private final LogFile logFile;
|
||||||
|
|
||||||
public DefaultLogbackConfiguration(String logFile) {
|
public DefaultLogbackConfiguration(LogFile logFile) {
|
||||||
this.logFile = logFile;
|
this.logFile = logFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,8 +61,9 @@ class DefaultLogbackConfiguration {
|
|||||||
synchronized (config.getConfigurationLock()) {
|
synchronized (config.getConfigurationLock()) {
|
||||||
base(config);
|
base(config);
|
||||||
Appender<ILoggingEvent> consoleAppender = consoleAppender(config);
|
Appender<ILoggingEvent> consoleAppender = consoleAppender(config);
|
||||||
if (StringUtils.hasLength(this.logFile)) {
|
if (this.logFile != null) {
|
||||||
Appender<ILoggingEvent> fileAppender = fileAppender(config, this.logFile);
|
Appender<ILoggingEvent> fileAppender = fileAppender(config,
|
||||||
|
this.logFile.toString());
|
||||||
config.root(Level.INFO, consoleAppender, fileAppender);
|
config.root(Level.INFO, consoleAppender, fileAppender);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import org.slf4j.ILoggerFactory;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.Marker;
|
import org.slf4j.Marker;
|
||||||
import org.slf4j.impl.StaticLoggerBinder;
|
import org.slf4j.impl.StaticLoggerBinder;
|
||||||
|
import org.springframework.boot.logging.LogFile;
|
||||||
import org.springframework.boot.logging.LogLevel;
|
import org.springframework.boot.logging.LogLevel;
|
||||||
import org.springframework.boot.logging.LoggingSystem;
|
import org.springframework.boot.logging.LoggingSystem;
|
||||||
import org.springframework.boot.logging.Slf4JLoggingSystem;
|
import org.springframework.boot.logging.Slf4JLoggingSystem;
|
||||||
@@ -88,13 +89,13 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void initialize(String configLocation, String logFile) {
|
public void initialize(String configLocation, LogFile logFile) {
|
||||||
getLogger(null).getLoggerContext().getTurboFilterList().remove(FILTER);
|
getLogger(null).getLoggerContext().getTurboFilterList().remove(FILTER);
|
||||||
super.initialize(configLocation, logFile);
|
super.initialize(configLocation, logFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void loadDefaults(String logFile) {
|
protected void loadDefaults(LogFile logFile) {
|
||||||
LoggerContext context = getLoggerContext();
|
LoggerContext context = getLoggerContext();
|
||||||
context.stop();
|
context.stop();
|
||||||
context.reset();
|
context.reset();
|
||||||
@@ -103,10 +104,10 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void loadConfiguration(String location, String logFile) {
|
protected void loadConfiguration(String location, LogFile logFile) {
|
||||||
Assert.notNull(location, "Location must not be null");
|
Assert.notNull(location, "Location must not be null");
|
||||||
if (StringUtils.hasLength(logFile)) {
|
if (logFile != null) {
|
||||||
System.setProperty("LOG_FILE", logFile);
|
logFile.applyToSystemProperties();
|
||||||
}
|
}
|
||||||
LoggerContext context = getLoggerContext();
|
LoggerContext context = getLoggerContext();
|
||||||
context.stop();
|
context.stop();
|
||||||
|
|||||||
@@ -60,6 +60,10 @@ public abstract class AbstractLoggingSystemTests {
|
|||||||
System.clearProperty("PID");
|
System.clearProperty("PID");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected final LogFile getLogFile(String file, String path) {
|
||||||
|
return new LogFile(file, path);
|
||||||
|
}
|
||||||
|
|
||||||
protected final String tmpDir() {
|
protected final String tmpDir() {
|
||||||
String path = StringUtils.cleanPath(System.getProperty(JAVA_IO_TMPDIR));
|
String path = StringUtils.cleanPath(System.getProperty(JAVA_IO_TMPDIR));
|
||||||
if (path.endsWith("/")) {
|
if (path.endsWith("/")) {
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2014 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 java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.core.env.MapPropertySource;
|
||||||
|
import org.springframework.core.env.MutablePropertySources;
|
||||||
|
import org.springframework.core.env.PropertyResolver;
|
||||||
|
import org.springframework.core.env.PropertySource;
|
||||||
|
import org.springframework.core.env.PropertySourcesPropertyResolver;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link LogFile}.
|
||||||
|
*
|
||||||
|
* @author Phillip Webb
|
||||||
|
*/
|
||||||
|
public class LogFileTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void noProperties() throws Exception {
|
||||||
|
PropertyResolver resolver = getPropertyResolver(null, null);
|
||||||
|
LogFile logFile = LogFile.get(resolver);
|
||||||
|
assertThat(logFile, nullValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loggingFile() throws Exception {
|
||||||
|
PropertyResolver resolver = getPropertyResolver("log.file", null);
|
||||||
|
LogFile logFile = LogFile.get(resolver);
|
||||||
|
Properties properties = new Properties();
|
||||||
|
logFile.applyTo(properties);
|
||||||
|
assertThat(logFile.toString(), equalTo("log.file"));
|
||||||
|
assertThat(properties.getProperty("LOG_FILE"), equalTo("log.file"));
|
||||||
|
assertThat(properties.getProperty("LOG_PATH"), nullValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loggingPath() throws Exception {
|
||||||
|
PropertyResolver resolver = getPropertyResolver(null, "logpath");
|
||||||
|
LogFile logFile = LogFile.get(resolver);
|
||||||
|
Properties properties = new Properties();
|
||||||
|
logFile.applyTo(properties);
|
||||||
|
assertThat(logFile.toString(), equalTo("logpath/spring.log"));
|
||||||
|
assertThat(properties.getProperty("LOG_FILE"), equalTo("logpath/spring.log"));
|
||||||
|
assertThat(properties.getProperty("LOG_PATH"), equalTo("logpath"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loggingFileAndPath() throws Exception {
|
||||||
|
PropertyResolver resolver = getPropertyResolver("log.file", "logpath");
|
||||||
|
LogFile logFile = LogFile.get(resolver);
|
||||||
|
Properties properties = new Properties();
|
||||||
|
logFile.applyTo(properties);
|
||||||
|
assertThat(logFile.toString(), equalTo("log.file"));
|
||||||
|
assertThat(properties.getProperty("LOG_FILE"), equalTo("log.file"));
|
||||||
|
assertThat(properties.getProperty("LOG_PATH"), equalTo("logpath"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private PropertyResolver getPropertyResolver(String file, String path) {
|
||||||
|
Map<String, Object> properties = new LinkedHashMap<String, Object>();
|
||||||
|
properties.put("logging.file", file);
|
||||||
|
properties.put("logging.path", path);
|
||||||
|
PropertySource<?> propertySource = new MapPropertySource("properties", properties);
|
||||||
|
MutablePropertySources propertySources = new MutablePropertySources();
|
||||||
|
propertySources.addFirst(propertySource);
|
||||||
|
return new PropertySourcesPropertyResolver(propertySources);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -98,7 +98,7 @@ public class JavaLoggerSystemTests extends AbstractLoggingSystemTests {
|
|||||||
}
|
}
|
||||||
this.loggingSystem.beforeInitialize();
|
this.loggingSystem.beforeInitialize();
|
||||||
this.logger.info("Hidden");
|
this.logger.info("Hidden");
|
||||||
this.loggingSystem.initialize(null, tmpDir() + "/spring.log");
|
this.loggingSystem.initialize(null, getLogFile(null, tmpDir()));
|
||||||
this.logger.info("Hello world");
|
this.logger.info("Hello world");
|
||||||
String output = this.output.toString().trim();
|
String output = this.output.toString().trim();
|
||||||
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
|
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ public class Log4JLoggingSystemTests extends AbstractLoggingSystemTests {
|
|||||||
public void withFile() throws Exception {
|
public void withFile() throws Exception {
|
||||||
this.loggingSystem.beforeInitialize();
|
this.loggingSystem.beforeInitialize();
|
||||||
this.logger.info("Hidden");
|
this.logger.info("Hidden");
|
||||||
this.loggingSystem.initialize(null, tmpDir() + "/spring.log");
|
this.loggingSystem.initialize(null, getLogFile(null, tmpDir()));
|
||||||
this.logger.info("Hello world");
|
this.logger.info("Hello world");
|
||||||
String output = this.output.toString().trim();
|
String output = this.output.toString().trim();
|
||||||
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
|
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
|
||||||
@@ -80,8 +80,8 @@ public class Log4JLoggingSystemTests extends AbstractLoggingSystemTests {
|
|||||||
@Test
|
@Test
|
||||||
public void testNonDefaultConfigLocation() throws Exception {
|
public void testNonDefaultConfigLocation() throws Exception {
|
||||||
this.loggingSystem.beforeInitialize();
|
this.loggingSystem.beforeInitialize();
|
||||||
this.loggingSystem.initialize("classpath:log4j-nondefault.properties", tmpDir()
|
this.loggingSystem.initialize("classpath:log4j-nondefault.properties",
|
||||||
+ "/spring.log");
|
getLogFile(null, tmpDir()));
|
||||||
this.logger.info("Hello world");
|
this.logger.info("Hello world");
|
||||||
String output = this.output.toString().trim();
|
String output = this.output.toString().trim();
|
||||||
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
|
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
|
|||||||
public void withFile() throws Exception {
|
public void withFile() throws Exception {
|
||||||
this.loggingSystem.beforeInitialize();
|
this.loggingSystem.beforeInitialize();
|
||||||
this.logger.info("Hidden");
|
this.logger.info("Hidden");
|
||||||
this.loggingSystem.initialize(null, tmpDir() + "/spring.log");
|
this.loggingSystem.initialize(null, getLogFile(null, tmpDir()));
|
||||||
this.logger.info("Hello world");
|
this.logger.info("Hello world");
|
||||||
String output = this.output.toString().trim();
|
String output = this.output.toString().trim();
|
||||||
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
|
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
|
||||||
@@ -82,8 +82,8 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
|
|||||||
@Test
|
@Test
|
||||||
public void testNonDefaultConfigLocation() throws Exception {
|
public void testNonDefaultConfigLocation() throws Exception {
|
||||||
this.loggingSystem.beforeInitialize();
|
this.loggingSystem.beforeInitialize();
|
||||||
this.loggingSystem.initialize("classpath:log4j2-nondefault.xml", tmpDir()
|
this.loggingSystem.initialize("classpath:log4j2-nondefault.xml",
|
||||||
+ "/tmp.log");
|
getLogFile(tmpDir() + "/tmp.log", null));
|
||||||
this.logger.info("Hello world");
|
this.logger.info("Hello world");
|
||||||
String output = this.output.toString().trim();
|
String output = this.output.toString().trim();
|
||||||
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
|
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
|
|||||||
public void withFile() throws Exception {
|
public void withFile() throws Exception {
|
||||||
this.loggingSystem.beforeInitialize();
|
this.loggingSystem.beforeInitialize();
|
||||||
this.logger.info("Hidden");
|
this.logger.info("Hidden");
|
||||||
this.loggingSystem.initialize(null, tmpDir() + "/spring.log");
|
this.loggingSystem.initialize(null, getLogFile(null, tmpDir()));
|
||||||
this.logger.info("Hello world");
|
this.logger.info("Hello world");
|
||||||
String output = this.output.toString().trim();
|
String output = this.output.toString().trim();
|
||||||
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
|
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
|
||||||
@@ -98,8 +98,8 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
|
|||||||
@Test
|
@Test
|
||||||
public void testNonDefaultConfigLocation() throws Exception {
|
public void testNonDefaultConfigLocation() throws Exception {
|
||||||
this.loggingSystem.beforeInitialize();
|
this.loggingSystem.beforeInitialize();
|
||||||
this.loggingSystem.initialize("classpath:logback-nondefault.xml", tmpDir()
|
this.loggingSystem.initialize("classpath:logback-nondefault.xml",
|
||||||
+ "/tmp.log");
|
getLogFile(tmpDir() + "/tmp.log", null));
|
||||||
this.logger.info("Hello world");
|
this.logger.info("Hello world");
|
||||||
String output = this.output.toString().trim();
|
String output = this.output.toString().trim();
|
||||||
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
|
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
|
||||||
|
|||||||
Reference in New Issue
Block a user