protected void loadConfiguration(String location, LogFile logFile) { Assert.notNull(location, "Location must not be null"); try { String configuration = FileCopyUtils.copyToString( new InputStreamReader(ResourceUtils.getURL(location).openStream())); if (logFile != null) { configuration = configuration.replace("${LOG_FILE}", StringUtils.cleanPath(logFile.toString())); } LogManager.getLogManager().readConfiguration( new ByteArrayInputStream(configuration.getBytes())); } catch (Exception ex) { throw new IllegalStateException( "Could not initialize Java logging from " + location, ex); } }
protected void loadConfiguration(String location, LogFile logFile) { Assert.notNull(location, "Location must not be null"); if (logFile != null) { logFile.applyToSystemProperties(); } try { LoggerContext ctx = getLoggerContext(); URL url = ResourceUtils.getURL(location); ConfigurationSource source = getConfigurationSource(url); ctx.start(ConfigurationFactory.getInstance().getConfiguration(source)); } catch (Exception ex) { throw new IllegalStateException( "Could not initialize Log4J2 logging from " + location, ex); } }
@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)); } }
@Override protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) { if (logFile != null) { loadConfiguration(getPackagedConfigFile("log4j2-file.xml"), logFile); } else { loadConfiguration(getPackagedConfigFile("log4j2.xml"), logFile); } }
protected void loadConfiguration(String location, LogFile logFile) { Assert.notNull(location, "Location must not be null"); try { LoggerContext ctx = getLoggerContext(); URL url = ResourceUtils.getURL(location); ConfigurationSource source = getConfigurationSource(url); ctx.start(ConfigurationFactory.getInstance().getConfiguration(source)); } catch (Exception ex) { throw new IllegalStateException( "Could not initialize Log4J2 logging from " + location, ex); } }
@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"); }
@Test public void reinitializeShouldSetSystemProperty() throws Exception { // gh-5491 this.loggingSystem.beforeInitialize(); this.logger.info("Hidden"); this.loggingSystem.initialize(this.initializationContext, null, null); LogFile logFile = getLogFile(tmpDir() + "/example.log", null, false); this.loggingSystem.initialize(this.initializationContext, "classpath:logback-nondefault.xml", logFile); assertThat(System.getProperty("LOG_FILE")).endsWith("example.log"); }
private Resource getLogFileResource() { if (this.externalFile != null) { return new FileSystemResource(this.externalFile); } LogFile logFile = LogFile.get(getEnvironment()); if (logFile == null) { logger.debug("Missing 'logging.file' or 'logging.path' properties"); return null; } return new FileSystemResource(logFile.toString()); }
private Resource getLogFileResource() { if (this.externalFile != null) { return new FileSystemResource(this.externalFile); } LogFile logFile = LogFile.get(this.environment); if (logFile == null) { logger.debug("Missing 'logging.file' or 'logging.path' properties"); return null; } return new FileSystemResource(logFile.toString()); }
@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); } }
protected void loadConfiguration(String location, LogFile logFile) { Assert.notNull(location, "Location must not be null"); if (logFile != null) { logFile.applyToSystemProperties(); } try { Log4jConfigurer.initLogging(location); } catch (Exception ex) { throw new IllegalStateException( "Could not initialize Log4J logging from " + location, ex); } }
private Resource getLogFileResource() { LogFile logFile = LogFile.get(this.environment); if (logFile == null) { logger.debug("Missing 'logging.file' or 'logging.path' properties"); return null; } FileSystemResource resource = new FileSystemResource(logFile.toString()); if (!resource.exists()) { if (logger.isWarnEnabled()) { logger.debug("Log file '" + resource + "' does not exist"); } return null; } return resource; }
@Override public void initialize(ConfigurableApplicationContext applicationContext) { CompositePropertySource composite = new CompositePropertySource( BOOTSTRAP_PROPERTY_SOURCE_NAME); AnnotationAwareOrderComparator.sort(this.propertySourceLocators); boolean empty = true; ConfigurableEnvironment environment = applicationContext.getEnvironment(); for (PropertySourceLocator locator : this.propertySourceLocators) { PropertySource<?> source = null; source = locator.locate(environment); if (source == null) { continue; } logger.info("Located property source: " + source); composite.addPropertySource(source); empty = false; } if (!empty) { MutablePropertySources propertySources = environment.getPropertySources(); String logConfig = environment.resolvePlaceholders("${logging.config:}"); LogFile logFile = LogFile.get(environment); if (propertySources.contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) { propertySources.remove(BOOTSTRAP_PROPERTY_SOURCE_NAME); } insertPropertySources(propertySources, composite); reinitializeLoggingSystem(environment, logConfig, logFile); setLogLevels(applicationContext, environment); handleIncludedProfiles(environment); } }
@Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { PropertySource ps = new MapPropertySource("LogFileLocationPS", Collections.singletonMap(LogFile.FILE_PROPERTY, SettingsService.getLogFile().getAbsolutePath())); event.getEnvironment().getPropertySources().addLast(ps); }
@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; }
@Override public void initialize(LoggingInitializationContext initializationContext, String configLocation, LogFile logFile) { getLoggerContext().getConfiguration().removeFilter(FILTER); super.initialize(initializationContext, configLocation, logFile); }
@Override protected void loadConfiguration(LoggingInitializationContext initializationContext, String location, LogFile logFile) { super.loadConfiguration(initializationContext, location, logFile); loadConfiguration(location, logFile); }