Skip to content

Commit

Permalink
For pre-initialization phase use console only
Browse files Browse the repository at this point in the history
Added basic-* log configuration files that are loaded in the
beforeInitialization() phase. They don't contain any file
logger so no empty spring.log is ever created.

Fixes spring-projectsgh-303
  • Loading branch information
Dave Syer committed Feb 19, 2014
1 parent 825fc2f commit e663b44
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ public void initialize() {
return;
}
}
// Fallback to the non-prefixed value
initialize(getPackagedConfigFile(this.paths[this.paths.length - 1]));
}

protected void initializeWithSensibleDefaults() {
initialize(getPackagedConfigFile(this.paths[this.paths.length - 1]));
initialize(getPackagedConfigFile("basic-" + this.paths[this.paths.length - 1]));
}

protected final String getPackagedConfigFile(String fileName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
handlers = java.util.logging.ConsoleHandler
.level = INFO

java.util.logging.ConsoleHandler.formatter = org.springframework.boot.logging.java.SimpleFormatter
java.util.logging.ConsoleHandler.level = ALL

org.hibernate.validator.internal.util.Version.level = WARNING
org.apache.coyote.http11.Http11NioProtocol.level = WARNING
org.crsh.plugin.level = WARNING
org.apache.tomcat.util.net.NioSelectorPool.level = WARNING
org.apache.catalina.startup.DigesterFactory.level = SEVERE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
log4j.rootCategory=INFO, CONSOLE, FILE

PID=????
LOG_PATTERN=[%d{yyyy-MM-dd HH:mm:ss.SSS}] boot%X{context} - ${PID} %5p [%t] --- %c{1}: %m%n

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=${LOG_PATTERN}

log4j.category.org.hibernate.validator.internal.util.Version=WARN
log4j.category.org.apache.coyote.http11.Http11NioProtocol=WARN
log4j.category.org.crsh.plugin=WARN
log4j.category.org.apache.tomcat.util.net.NioSelectorPool=WARN
log4j.category.org.apache.catalina.startup.DigesterFactory=ERROR
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<included>
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-/tmp/}spring.log}"/>
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } [%t] --- %-40.40logger{39} : %m%n%wex"/>
<property name="CONSOLE_LOG_PATTERN" value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t{14}]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wex"/>

<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<include resource="org/springframework/boot/logging/logback/default-logback.xml"/>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-/tmp/}spring.log}"/>
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } [%t] --- %-40.40logger{39} : %m%n%wex"/>

<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
Expand All @@ -28,22 +21,9 @@
</triggeringPolicy>
</appender>

<appender name="DEBUG_LEVEL_REMAPPER" class="org.springframework.boot.logging.logback.LevelRemappingAppender">
<destinationLogger>org.springframework.boot</destinationLogger>
</appender>

<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>

<logger name="org.hibernate.validator.internal.util.Version" level="WARN"/>
<logger name="org.apache.coyote.http11.Http11NioProtocol" level="WARN"/>
<logger name="org.crsh.plugin" level="WARN"/>
<logger name="org.apache.tomcat.util.net.NioSelectorPool" level="WARN"/>
<logger name="org.apache.catalina.startup.DigesterFactory" level="ERROR"/>
<logger name="org.thymeleaf" additivity="false">
<appender-ref ref="DEBUG_LEVEL_REMAPPER"/>
</logger>

</included>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<included>
<property name="CONSOLE_LOG_PATTERN" value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t{14}]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wex"/>

<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>

<appender name="DEBUG_LEVEL_REMAPPER" class="org.springframework.boot.logging.logback.LevelRemappingAppender">
<destinationLogger>org.springframework.boot</destinationLogger>
</appender>

<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>

<logger name="org.hibernate.validator.internal.util.Version" level="WARN"/>
<logger name="org.apache.coyote.http11.Http11NioProtocol" level="WARN"/>
<logger name="org.crsh.plugin" level="WARN"/>
<logger name="org.apache.tomcat.util.net.NioSelectorPool" level="WARN"/>
<logger name="org.apache.catalina.startup.DigesterFactory" level="ERROR"/>
<logger name="org.thymeleaf" additivity="false">
<appender-ref ref="DEBUG_LEVEL_REMAPPER"/>
</logger>

</included>
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void init() throws SecurityException, IOException {
this.initializer.onApplicationEvent(new ApplicationStartedEvent(
new SpringApplication(), NO_ARGS));
new File("target/foo.log").delete();
new File("/tmp/spring.log").delete();
}

@After
Expand All @@ -85,13 +86,14 @@ public void clear() {
}

@Test
public void testDefaultConfigLocation() {
public void testBaseConfigLocation() {
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
this.logger.info("Hello world");
String output = this.outputCapture.toString().trim();
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
assertFalse("Wrong output:\n" + output, output.contains("???"));
assertTrue(new File("/tmp/spring.log").exists());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.boot.logging.logback;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.SLF4JLogFactory;
import org.junit.After;
Expand All @@ -27,6 +29,7 @@
import org.springframework.util.StringUtils;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

Expand All @@ -48,6 +51,7 @@ public class LogbackLoggingSystemTests {
@Before
public void setup() {
this.logger = new SLF4JLogFactory().getInstance(getClass().getName());
new File("/tmp/spring.log").delete();
}

@After
Expand All @@ -65,6 +69,7 @@ public void testNonDefaultConfigLocation() throws Exception {
String output = this.output.toString().trim();
assertTrue("Wrong output:\n" + output, output.contains("Hello world"));
assertTrue("Wrong output:\n" + output, output.contains("/tmp/spring.log"));
assertFalse(new File("/tmp/spring.log").exists());
}

@Test(expected = IllegalStateException.class)
Expand Down

0 comments on commit e663b44

Please sign in to comment.