Search in sources :

Example 1 with LoggingInitializationContext

use of org.springframework.boot.logging.LoggingInitializationContext in project spring-boot by spring-projects.

the class LoggingApplicationListener method initializeSystem.

private void initializeSystem(ConfigurableEnvironment environment, LoggingSystem system, LogFile logFile) {
    LoggingInitializationContext initializationContext = new LoggingInitializationContext(environment);
    String logConfig = environment.getProperty(CONFIG_PROPERTY);
    if (ignoreLogConfig(logConfig)) {
        system.initialize(initializationContext, null, logFile);
    } else {
        try {
            ResourceUtils.getURL(logConfig).openStream().close();
            system.initialize(initializationContext, logConfig, logFile);
        } catch (Exception ex) {
            // NOTE: We can't use the logger here to report the problem
            System.err.println("Logging system failed to initialize " + "using configuration from '" + logConfig + "'");
            ex.printStackTrace(System.err);
            throw new IllegalStateException(ex);
        }
    }
}
Also used : LoggingInitializationContext(org.springframework.boot.logging.LoggingInitializationContext)

Example 2 with LoggingInitializationContext

use of org.springframework.boot.logging.LoggingInitializationContext in project spring-boot by spring-projects.

the class SpringBootJoranConfiguratorTests method setup.

@Before
public void setup() {
    this.environment = new MockEnvironment();
    this.initializationContext = new LoggingInitializationContext(this.environment);
    this.configurator = new SpringBootJoranConfigurator(this.initializationContext);
    StaticLoggerBinder binder = StaticLoggerBinder.getSingleton();
    this.context = (LoggerContext) binder.getLoggerFactory();
    this.logger = this.context.getLogger(getClass());
}
Also used : LoggingInitializationContext(org.springframework.boot.logging.LoggingInitializationContext) StaticLoggerBinder(org.slf4j.impl.StaticLoggerBinder) MockEnvironment(org.springframework.mock.env.MockEnvironment) Before(org.junit.Before)

Example 3 with LoggingInitializationContext

use of org.springframework.boot.logging.LoggingInitializationContext in project spring-boot by spring-projects.

the class LogbackLoggingSystemTests method setup.

@Before
public void setup() {
    this.loggingSystem.cleanUp();
    this.logger = new SLF4JLogFactory().getInstance(getClass().getName());
    this.environment = new MockEnvironment();
    this.initializationContext = new LoggingInitializationContext(this.environment);
}
Also used : LoggingInitializationContext(org.springframework.boot.logging.LoggingInitializationContext) MockEnvironment(org.springframework.mock.env.MockEnvironment) SLF4JLogFactory(org.apache.commons.logging.impl.SLF4JLogFactory) Before(org.junit.Before)

Example 4 with LoggingInitializationContext

use of org.springframework.boot.logging.LoggingInitializationContext in project spring-boot by spring-projects.

the class LogbackLoggingSystemTests method testConsolePatternProperty.

@Test
public void testConsolePatternProperty() {
    MockEnvironment environment = new MockEnvironment();
    environment.setProperty("logging.pattern.console", "%logger %msg");
    LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(environment);
    this.loggingSystem.initialize(loggingInitializationContext, null, null);
    this.logger.info("Hello world");
    String output = this.output.toString().trim();
    assertThat(getLineWithText(output, "Hello world")).doesNotContain("INFO");
}
Also used : LoggingInitializationContext(org.springframework.boot.logging.LoggingInitializationContext) MockEnvironment(org.springframework.mock.env.MockEnvironment) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 5 with LoggingInitializationContext

use of org.springframework.boot.logging.LoggingInitializationContext in project spring-boot by spring-projects.

the class LogbackLoggingSystemTests method testFilePatternProperty.

@Test
public void testFilePatternProperty() throws Exception {
    MockEnvironment environment = new MockEnvironment();
    environment.setProperty("logging.pattern.file", "%logger %msg");
    LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext(environment);
    File file = new File(tmpDir(), "logback-test.log");
    LogFile logFile = getLogFile(file.getPath(), null);
    this.loggingSystem.initialize(loggingInitializationContext, null, logFile);
    this.logger.info("Hello world");
    String output = this.output.toString().trim();
    assertThat(getLineWithText(output, "Hello world")).contains("INFO");
    assertThat(getLineWithText(file, "Hello world")).doesNotContain("INFO");
}
Also used : LogFile(org.springframework.boot.logging.LogFile) LoggingInitializationContext(org.springframework.boot.logging.LoggingInitializationContext) MockEnvironment(org.springframework.mock.env.MockEnvironment) Matchers.containsString(org.hamcrest.Matchers.containsString) LogFile(org.springframework.boot.logging.LogFile) File(java.io.File) Test(org.junit.Test)

Aggregations

LoggingInitializationContext (org.springframework.boot.logging.LoggingInitializationContext)6 MockEnvironment (org.springframework.mock.env.MockEnvironment)5 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Test (org.junit.Test)3 Before (org.junit.Before)2 File (java.io.File)1 SLF4JLogFactory (org.apache.commons.logging.impl.SLF4JLogFactory)1 StaticLoggerBinder (org.slf4j.impl.StaticLoggerBinder)1 LogFile (org.springframework.boot.logging.LogFile)1