Commit fd757cb3 authored by Dave Syer's avatar Dave Syer

Add logging.level to application.properties

E.g.

logging.level.org.springframework: DEBUG
logging.level.org.hibernate: WARN

Fixed gh-788
parent cc61d92b
......@@ -62,9 +62,12 @@ public class JarCommandIT {
@Test
public void jarCreationWithGrabResolver() throws Exception {
File jar = new File("target/test-app.jar");
Invocation invocation = this.cli.invoke("jar", jar.getAbsolutePath(),
Invocation invocation = this.cli.invoke("run", jar.getAbsolutePath(),
"bad.groovy");
invocation.await();
assertThat(invocation.getErrorOutput(), equalTo(""));
invocation = this.cli.invoke("jar", jar.getAbsolutePath(), "bad.groovy");
invocation.await();
assertEquals(invocation.getErrorOutput(), 0, invocation.getErrorOutput().length());
assertTrue(jar.exists());
......
......@@ -43,7 +43,8 @@ content into your application; rather pick only the properties that you need.
# LOGGING
logging.path=/var/logs
logging.file=myapp.log
logging.config=
logging.config= # location of config file (default classpath:/logback.xml for logback)
logging.level.*= # levels for loggers, e.g. "logging.level.org.springframework=DEBUG" (TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF)
# IDENTITY ({sc-spring-boot}/context/ContextIdApplicationContextInitializer.{sc-ext}[ContextIdApplicationContextInitializer])
spring.application.name=
......
......@@ -833,10 +833,6 @@ Check out {sc-spring-boot-autoconfigure}/web/WebMvcAutoConfiguration.{sc-ext}[`W
[[howto-logging]]
== Logging
[[howto-configure-logback-for-loggin]]
=== Configure Logback for logging
Spring Boot has no mandatory logging dependence, except for the `commons-logging` API, of
which there are many implementations to choose from. To use http://logback.qos.ch[Logback]
you need to include it, and some bindings for `commons-logging` on the classpath. The
......@@ -854,10 +850,32 @@ For example, using Maven:
----
Spring Boot has a `LoggingSystem` abstraction that attempts to configure logging based on
the content of the classpath. If Logback is available it is the first choice. So if you
put a `logback.xml` in the root of your classpath it will be picked up from there. Spring
Boot provides a default base configuration that you can include if you just want to set
levels, e.g.
the content of the classpath. If Logback is available it is the first choice.
If the only change you need to make to logging is to set the levels of various loggers
then you can do that in `application.properties` using the "logging.level" prefix, e.g.
[source,properties,indent=0,subs="verbatim,quotes,attributes"]
----
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
----
You can also set the location of a file to log to (in addition to the console) using
"logging.file".
To configure the more fine grained settings of a logging system you need to use the native configuration
format supported by the `LoggingSystem` in question. By default Spring Boot picks up the native
configuration from its default location for the system (e.g. `classpath:/logback.xml` for Logback), but
you can set the location of the config file using the "logging.config" property.
[[howto-configure-logback-for-loggin]]
=== Configure Logback for logging
If you put a `logback.xml` in the root of your classpath it will be
picked up from there. Spring Boot provides a default base
configuration that you can include if you just want to set levels,
e.g.
[source,xml,indent=0,subs="verbatim,quotes,attributes"]
----
......
......@@ -724,6 +724,18 @@ using a `logging.file` property.
As with console output, `ERROR`, `WARN` and `INFO` level messages are logged by default.
[[boot-features-custom-log-levels]]
=== Log Levels
All the supported logging systems can have the logger levels set in the Spring `Environment`
(so for example in `application.properties`) using "logging.level.*=LEVEL" where "LEVEL" is one of
TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF. Example `application.properties`:
[source,properties,indent=0,subs="verbatim,quotes,attributes"]
----
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
----
[[boot-features-custom-log-configuration]]
......
logging.file: /tmp/logs/app.log
logging.level.org.springframework.security: INFO
management.address: 127.0.0.1
endpoints.shutdown.enabled: true
server.tomcat.basedir: target/tomcat
......
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<!-- logger name="org.springframework.boot" level="DEBUG"/-->
<!-- logger name="org.springframework.security" level="DEBUG"/-->
</configuration>
logging.file: /tmp/logs/app.log
logging.level.org.springframework.integration.file: DEBUG
service.greeting: Hello
debug: true
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.integration.file" level="DEBUG" />
</configuration>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.integration.file" level="DEBUG" />
</configuration>
\ No newline at end of file
# Allow templates to be reloaded at dev time
spring.groovy.template.cache: false
logging.level.org.springframework.web: INFO
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.web" level="DEBUG"/>
</configuration>
spring.thymeleaf.cache: false
logging.level.org.springframework.security: INFO
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<!-- logger name="org.springframework.boot" level="DEBUG"/-->
<logger name="org.springframework.security" level="DEBUG"/>
</configuration>
spring.thymeleaf.cache: false
debug: true
security.basic.enabled: false
# demo only:
security.user.password: password
\ No newline at end of file
security.user.password: password
logging.level.org.springframework.security: INFO
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<!-- logger name="org.springframework.boot" level="DEBUG"/-->
<logger name="org.springframework.security" level="DEBUG"/>
</configuration>
......@@ -23,6 +23,6 @@ package org.springframework.boot.logging;
*/
public enum LogLevel {
TRACE, DEBUG, INFO, WARN, ERROR, FATAL
TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
}
......@@ -19,11 +19,13 @@ package org.springframework.boot.logging;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.ApplicationPid;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationEvent;
......@@ -180,7 +182,6 @@ public class LoggingApplicationListener implements SmartApplicationListener {
try {
ResourceUtils.getURL(value).openStream().close();
system.initialize(value);
return;
}
catch (Exception ex) {
// Swallow exception and continue
......@@ -188,10 +189,31 @@ public class LoggingApplicationListener implements SmartApplicationListener {
this.logger.warn("Logging environment value '" + value
+ "' cannot be opened and will be ignored");
}
else {
system.initialize();
if (this.springBootLogging != null) {
initializeLogLevel(system, this.springBootLogging);
}
}
setLogLevels(system, environment);
system.initialize();
if (this.springBootLogging != null) {
initializeLogLevel(system, this.springBootLogging);
}
public void setLogLevels(LoggingSystem system, Environment environment) {
Map<String, Object> levels = new RelaxedPropertyResolver(environment)
.getSubProperties("logging.level.");
for (Entry<String, Object> entry : levels.entrySet()) {
try {
LogLevel level = LogLevel.valueOf(entry.getValue().toString());
system.setLogLevel(entry.getKey(), level);
}
catch (RuntimeException e) {
this.logger.error("Cannot set level: " + entry.getValue() + " for '"
+ entry.getKey() + "'");
}
}
}
......
......@@ -47,6 +47,7 @@ public class JavaLoggingSystem extends AbstractLoggingSystem {
levels.put(LogLevel.WARN, Level.WARNING);
levels.put(LogLevel.ERROR, Level.SEVERE);
levels.put(LogLevel.FATAL, Level.SEVERE);
levels.put(LogLevel.OFF, Level.OFF);
LEVELS = Collections.unmodifiableMap(levels);
}
......
......@@ -49,6 +49,7 @@ public class Log4JLoggingSystem extends AbstractLoggingSystem {
levels.put(LogLevel.WARN, Level.WARN);
levels.put(LogLevel.ERROR, Level.ERROR);
levels.put(LogLevel.FATAL, Level.ERROR);
levels.put(LogLevel.OFF, Level.OFF);
LEVELS = Collections.unmodifiableMap(levels);
}
......
......@@ -55,6 +55,7 @@ public class LogbackLoggingSystem extends AbstractLoggingSystem {
levels.put(LogLevel.WARN, Level.WARN);
levels.put(LogLevel.ERROR, Level.ERROR);
levels.put(LogLevel.FATAL, Level.ERROR);
levels.put(LogLevel.OFF, Level.OFF);
LEVELS = Collections.unmodifiableMap(levels);
}
......
......@@ -187,6 +187,42 @@ public class LoggingApplicationListenerTests {
assertThat(this.outputCapture.toString(), containsString("testattrace"));
}
@Test
public void parseLevels() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
"logging.level.org.springframework.boot=TRACE");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
this.logger.debug("testatdebug");
this.logger.trace("testattrace");
assertThat(this.outputCapture.toString(), containsString("testatdebug"));
assertThat(this.outputCapture.toString(), containsString("testattrace"));
}
@Test
public void parseLevelsFails() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
"logging.level.org.springframework.boot=GARBAGE");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
this.logger.debug("testatdebug");
assertThat(this.outputCapture.toString(), not(containsString("testatdebug")));
assertThat(this.outputCapture.toString(),
containsString("Cannot set level: GARBAGE"));
}
@Test
public void parseLevelsNone() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
"logging.level.org.springframework.boot=OFF");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
this.logger.debug("testatdebug");
this.logger.fatal("testatfatal");
assertThat(this.outputCapture.toString(), not(containsString("testatdebug")));
assertThat(this.outputCapture.toString(), not(containsString("testatfatal")));
}
@Test
public void parseArgsDisabled() throws Exception {
this.initializer.setParseArgs(false);
......
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