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"); } } }
@Test public void loggingSystemNotInitialized() { System.setProperty(LoggingSystem.SYSTEM_PROPERTY, TestLoggingSystem.class.getName()); TestLoggingSystem system = (TestLoggingSystem) LoggingSystem .get(getClass().getClassLoader()); assertThat(system.getCount()).isEqualTo(0); try (ConfigurableApplicationContext context = SpringApplication.run(Empty.class, "--spring.main.webEnvironment=false", "--debug=false", "--spring.main.bannerMode=OFF", "--spring.cloud.bootstrap.name=refresh")) { assertThat(system.getCount()).isEqualTo(4); ContextRefresher refresher = new ContextRefresher(context, scope); refresher.refresh(); assertThat(system.getCount()).isEqualTo(4); } }
@Test public void logTrace() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.TRACE); logger.log(LogLevel.TRACE, "logger name", "trace message"); assertThat(capture.toString(), containsString("TRACE logger name - trace message")); }
@Test public void logTraceError() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.TRACE); logger.log(LogLevel.TRACE, "logger name", "trace message", new Exception("error message")); Pattern pattern = Pattern.compile("TRACE logger name - trace message.*java.lang.Exception: error message", Pattern.DOTALL | Pattern.MULTILINE); assertTrue(pattern.matcher(capture.toString()).find()); }
@Test public void logDebug() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.DEBUG); logger.log(LogLevel.DEBUG, "logger name", "debug message"); assertThat(capture.toString(), containsString("DEBUG logger name - debug message")); }
@Test public void logDebugError() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.DEBUG); logger.log(LogLevel.DEBUG, "logger name", "debug message", new Exception("error message")); Pattern pattern = Pattern.compile("DEBUG logger name - debug message.*java.lang.Exception: error message", Pattern.DOTALL | Pattern.MULTILINE); assertTrue(pattern.matcher(capture.toString()).find()); }
@Test public void logInfo() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.INFO); logger.log(LogLevel.INFO, "logger name", "info message"); assertThat(capture.toString(), containsString("INFO logger name - info message")); }
@Test public void logInfoError() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.INFO); logger.log(LogLevel.INFO, "logger name", "info message", new Exception("error message")); Pattern pattern = Pattern.compile("INFO logger name - info message.*java.lang.Exception: error message", Pattern.DOTALL | Pattern.MULTILINE); assertTrue(pattern.matcher(capture.toString()).find()); }
@Test public void logWarning() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.WARN); logger.log(LogLevel.WARN, "logger name", "warn message"); assertThat(capture.toString(), containsString("WARN logger name - warn message")); }
@Test public void logWarningError() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.WARN); logger.log(LogLevel.WARN, "logger name", "warn message", new Exception("error message")); Pattern pattern = Pattern.compile("WARN logger name - warn message.*java.lang.Exception: error message", Pattern.DOTALL | Pattern.MULTILINE); assertTrue(pattern.matcher(capture.toString()).find()); }
@Test public void logError() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.ERROR); logger.log(LogLevel.ERROR, "logger name", "error message"); assertThat(capture.toString(), containsString("ERROR logger name - error message")); }
@Test public void logErrorError() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.ERROR); logger.log(LogLevel.ERROR, "logger name", "error message", new Exception("error message")); Pattern pattern = Pattern.compile("ERROR logger name - error message.*java.lang.Exception: error message", Pattern.DOTALL | Pattern.MULTILINE); assertTrue(pattern.matcher(capture.toString()).find()); }
@Test public void logFatal() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.FATAL); logger.log(LogLevel.FATAL, "logger name", "fatal message"); assertThat(capture.toString(), containsString("ERROR logger name - fatal message")); }
@Test public void logFatalError() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.FATAL); logger.log(LogLevel.FATAL, "logger name", "fatal message", new Exception("error message")); Pattern pattern = Pattern.compile("ERROR logger name - fatal message.*java.lang.Exception: error message", Pattern.DOTALL | Pattern.MULTILINE); assertTrue(pattern.matcher(capture.toString()).find()); }
@Test public void logTraceErrorOff() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.OFF); logger.log(LogLevel.TRACE, "logger name", "trace message", new Exception("error message")); Pattern pattern = Pattern.compile("TRACE logger name - trace message.*java.lang.Exception: error message", Pattern.DOTALL | Pattern.MULTILINE); assertFalse(pattern.matcher(capture.toString()).find()); }
@Override public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) { if (source != null && source != ConfigurationSource.NULL_SOURCE) { if (LoggingSystem.get(loggerContext.getClass().getClassLoader()) != null) { return new DefaultConfiguration(); } } return null; }
public static void setEnabled(boolean enabled) { if (enabled) { LoggingSystem system = LoggingSystem .get(Thread.currentThread().getContextClassLoader()); system.setLogLevel("org.springframework", LogLevel.DEBUG); system.setLogLevel("io.spring.concourse.artifactoryresource", LogLevel.DEBUG); } }
@Override public void onApplicationEvent(EnvironmentChangeEvent event) { if (this.environment == null) { return; } LoggingSystem system = LoggingSystem.get(LoggingSystem.class.getClassLoader()); setLogLevels(system, this.environment); }
protected void setLogLevels(LoggingSystem system, Environment environment) { Map<String, String> levels = Binder.get(environment) .bind("logging.level", STRING_STRING_MAP).orElseGet(Collections::emptyMap); for (Entry<String, String> entry : levels.entrySet()) { setLogLevel(system, environment, entry.getKey(), entry.getValue().toString()); } }
private void setLogLevel(LoggingSystem system, Environment environment, String name, String level) { try { if (name.equalsIgnoreCase("root")) { name = null; } level = environment.resolvePlaceholders(level); system.setLogLevel(name, LogLevel.valueOf(level.toUpperCase())); } catch (RuntimeException ex) { this.logger.error("Cannot set level: " + level + " for '" + name + "'"); } }
@BeforeClass public static void setErrorLogging() { LoggingSystem.get(ClassLoader.getSystemClassLoader()).setLogLevel(Logger.ROOT_LOGGER_NAME, LogLevel.DEBUG); }
@BeforeClass public static void setErrorLogging() { LoggingSystem.get(ClassLoader.getSystemClassLoader()).setLogLevel(Logger.ROOT_LOGGER_NAME, LogLevel.INFO); }
@BeforeClass public static void setErrorLogging() { LoggingSystem.get(ClassLoader.getSystemClassLoader()).setLogLevel(Logger.ROOT_LOGGER_NAME, LogLevel.ERROR); }
@Test public void isLogTraceEnabled() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.TRACE); assertTrue(logger.isEnabled(LogLevel.TRACE, "logger name")); }
@Test public void isLogDebugEnabled() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.DEBUG); assertTrue(logger.isEnabled(LogLevel.DEBUG, "logger name")); }
@Test public void isLogInfoEnabled() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.INFO); assertTrue(logger.isEnabled(LogLevel.INFO, "logger name")); }
@Test public void isLogErrorEnabled() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.ERROR); assertTrue(logger.isEnabled(LogLevel.ERROR, "logger name")); }
@Test public void isLogFatalEnabled() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.FATAL); assertTrue(logger.isEnabled(LogLevel.FATAL, "logger name")); }
@Test public void isLogTraceDisabled() { LoggingSystem.get(ClassLoader.getSystemClassLoader()) .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.OFF); assertFalse(logger.isEnabled(LogLevel.TRACE, "logger name")); }
public LoggersService(LoggingSystem loggingSystem) { Assert.notNull(loggingSystem, "LoggingSystem must not be null"); this.loggingSystem = loggingSystem; }
private void shutdownLogging() { LoggingSystem loggingSystem = LoggingSystem .get(ClassUtils.getDefaultClassLoader()); loggingSystem.cleanUp(); loggingSystem.beforeInitialize(); }
@After public void reset() { LoggingSystem.get(getClass().getClassLoader()) .setLogLevel("org.springframework.web", LogLevel.INFO); }
@After public void close() { System.clearProperty(LoggingSystem.SYSTEM_PROPERTY); TestLoggingSystem.count = 0; }