@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(); assertTrue("Wrong console output pattern:\n" + output, getLineWithText(output, "Hello world").contains("INFO")); assertFalse("Wrong file output pattern:\n" + output, getLineWithText(file, "Hello world").contains("INFO")); }
private void reinitializeLoggingSystem(ConfigurableEnvironment environment, String oldLogConfig, LogFile oldLogFile) { Map<String, Object> props = Binder.get(environment) .bind("logging", Bindable.mapOf(String.class, Object.class)).orElseGet(Collections::emptyMap); if (!props.isEmpty()) { String logConfig = environment.resolvePlaceholders("${logging.config:}"); LogFile logFile = LogFile.get(environment); LoggingSystem system = LoggingSystem .get(LoggingSystem.class.getClassLoader()); try { ResourceUtils.getURL(logConfig).openStream().close(); // Three step initialization that accounts for the clean up of the logging // context before initialization. Spring Boot doesn't initialize a logging // system that hasn't had this sequence applied (since 1.4.1). system.cleanUp(); system.beforeInitialize(); system.initialize(new LoggingInitializationContext(environment), logConfig, logFile); } catch (Exception ex) { PropertySourceBootstrapConfiguration.logger .warn("Logging config file location '" + logConfig + "' cannot be opened and will be ignored"); } } }
@Override protected void loadDefaults(final LoggingInitializationContext context, final LogFile file) { final String configFileName = "classpath:META-INF/log4j/default/log4j2" + ((file == null) ? Strings.EMPTY : "-file") + ".xml"; loadConfiguration(context, configFileName, file); }
@Override protected void loadConfiguration(final LoggingInitializationContext context, final String location, final LogFile ignored) { // log file name is available via ${sys:LOG_FILE} (see log4j2-file.xml default config) final URI configLocation = NetUtils.toURI(location); loggerContext = (LoggerContext) LogManager.getContext( getClassLoader(), false, this, configLocation); }
@Override protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) { if (logFile != null) { loadConfiguration(getPackagedConfigFile("logging-file.properties"), logFile); } else { loadConfiguration(getPackagedConfigFile("logging.properties"), logFile); } }
@Override public void initialize(LoggingInitializationContext initializationContext, String configLocation, LogFile logFile) { getLogger(null).getLoggerContext().getTurboFilterList().remove(FILTER); super.initialize(initializationContext, configLocation, logFile); if (StringUtils.hasText(System.getProperty(CONFIGURATION_FILE_PROPERTY))) { getLogger(LogbackLoggingSystem.class.getName()).warn( "Ignoring '" + CONFIGURATION_FILE_PROPERTY + "' system property. " + "Please use 'logging.config' instead."); } }
@Override protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) { LoggerContext context = getLoggerContext(); stopAndReset(context); LogbackConfigurator configurator = new LogbackConfigurator(context); context.putProperty("LOG_LEVEL_PATTERN", initializationContext.getEnvironment().resolvePlaceholders( "${logging.pattern.level:${LOG_LEVEL_PATTERN:%5p}}")); new DefaultLogbackConfiguration(initializationContext, logFile) .apply(configurator); context.setPackagingDataEnabled(true); }
@Override protected void loadConfiguration(LoggingInitializationContext initializationContext, String location, LogFile logFile) { super.loadConfiguration(initializationContext, location, logFile); LoggerContext loggerContext = getLoggerContext(); stopAndReset(loggerContext); try { configureByResourceUrl(initializationContext, loggerContext, ResourceUtils.getURL(location)); } catch (Exception ex) { throw new IllegalStateException( "Could not initialize Logback logging from " + location, ex); } List<Status> statuses = loggerContext.getStatusManager().getCopyOfStatusList(); StringBuilder errors = new StringBuilder(); for (Status status : statuses) { if (status.getLevel() == Status.ERROR) { errors.append(errors.length() > 0 ? String.format("%n") : ""); errors.append(status.toString()); } } if (errors.length() > 0) { throw new IllegalStateException( String.format("Logback configuration error detected: %n%s", errors)); } }
private void configureByResourceUrl( LoggingInitializationContext initializationContext, LoggerContext loggerContext, URL url) throws JoranException { if (url.toString().endsWith("xml")) { JoranConfigurator configurator = new SpringBootJoranConfigurator( initializationContext); configurator.setContext(loggerContext); configurator.doConfigure(url); } else { new ContextInitializer(loggerContext).configureByResource(url); } }
@Override protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) { if (logFile != null) { loadConfiguration(getPackagedConfigFile("log4j2-file.xml"), logFile); } else { loadConfiguration(getPackagedConfigFile("log4j2.xml"), logFile); } }
@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"); }
@Test public void testLevelPatternProperty() { MockEnvironment environment = new MockEnvironment(); environment.setProperty("logging.pattern.level", "X%clr(%p)X"); 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")).contains("XINFOX"); }
@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"); }
@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()); }
@Override public void initialize(LoggingInitializationContext initializationContext, String configLocation, LogFile logFile) { getLogger(null).getLoggerContext().getTurboFilterList().remove(FILTER); super.initialize(initializationContext, configLocation, logFile); if (StringUtils.hasText(System.getProperty(CONFIGURATION_FILE_PROPERTY))) { getLogger(LogbackLoggingSystem.class.getName()).warn( "Ignoring '" + CONFIGURATION_FILE_PROPERTY + "' system property. " + "Please use 'logging.path' instead."); } }
@Override protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) { LoggerContext context = getLoggerContext(); stopAndReset(context); LogbackConfigurator configurator = new LogbackConfigurator(context); context.putProperty("LOG_LEVEL_PATTERN", initializationContext.getEnvironment().resolvePlaceholders( "${logging.pattern.level:${LOG_LEVEL_PATTERN:%5p}}")); new DefaultLogbackConfiguration(initializationContext, logFile) .apply(configurator); }
@Override protected void loadConfiguration(LoggingInitializationContext initializationContext, String location, LogFile logFile) { Assert.notNull(location, "Location must not be null"); if (logFile != null) { logFile.applyToSystemProperties(); } LoggerContext loggerContext = getLoggerContext(); stopAndReset(loggerContext); try { configureByResourceUrl(initializationContext, loggerContext, ResourceUtils.getURL(location)); } catch (Exception ex) { throw new IllegalStateException( "Could not initialize Logback logging from " + location, ex); } List<Status> statuses = loggerContext.getStatusManager().getCopyOfStatusList(); StringBuilder errors = new StringBuilder(); for (Status status : statuses) { if (status.getLevel() == Status.ERROR) { errors.append(errors.length() > 0 ? "\n" : ""); errors.append(status.toString()); } } if (errors.length() > 0) { throw new IllegalStateException( "Logback configuration error " + "detected: \n" + errors); } }
@Override protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) { if (logFile != null) { loadConfiguration(getPackagedConfigFile("log4j-file.properties"), logFile); } else { loadConfiguration(getPackagedConfigFile("log4j.properties"), logFile); } }
@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(); assertFalse("Wrong output pattern:\n" + output, getLineWithText(output, "Hello world").contains("INFO")); }
@Test public void testLevelPatternProperty() { MockEnvironment environment = new MockEnvironment(); environment.setProperty("logging.pattern.level", "X%clr(%p)X"); LoggingInitializationContext loggingInitializationContext = new LoggingInitializationContext( environment); this.loggingSystem.initialize(loggingInitializationContext, null, null); this.logger.info("Hello world"); String output = this.output.toString().trim(); assertTrue("Wrong output pattern:\n" + output, getLineWithText(output, "Hello world").contains("XINFOX")); }
@Override protected void reinitialize(final LoggingInitializationContext context) { if (loggerContext != null) { loggerContext.reconfigure(); } }
@Override protected void loadConfiguration(LoggingInitializationContext initializationContext, String location, LogFile logFile) { loadConfiguration(location, logFile); }
DefaultLogbackConfiguration(LoggingInitializationContext initializationContext, LogFile logFile) { this.patterns = getPatternsResolver(initializationContext.getEnvironment()); this.logFile = logFile; }
SpringBootJoranConfigurator(LoggingInitializationContext initializationContext) { this.initializationContext = initializationContext; }
@Override protected void reinitialize(LoggingInitializationContext initializationContext) { getLoggerContext().reset(); getLoggerContext().getStatusManager().clear(); loadConfiguration(initializationContext, getSelfInitializationConfig(), null); }
@Override public void initialize(LoggingInitializationContext initializationContext, String configLocation, LogFile logFile) { getLoggerContext().getConfiguration().removeFilter(FILTER); super.initialize(initializationContext, configLocation, logFile); }