org.springframework.boot.logging.LoggingSystem Java Examples

The following examples show how to use org.springframework.boot.logging.LoggingSystem. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContextRefresherTests.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void loggingSystemNotInitialized() {
	System.setProperty(LoggingSystem.SYSTEM_PROPERTY,
			TestLoggingSystem.class.getName());
	TestLoggingSystem system = (TestLoggingSystem) LoggingSystem
			.get(getClass().getClassLoader());
	then(system.getCount()).isEqualTo(0);
	try (ConfigurableApplicationContext context = SpringApplication.run(Empty.class,
			"--spring.main.web-application-type=none", "--debug=false",
			"--spring.main.bannerMode=OFF",
			"--spring.cloud.bootstrap.name=refresh")) {
		then(system.getCount()).isEqualTo(4);
		ContextRefresher refresher = new ContextRefresher(context, this.scope);
		refresher.refresh();
		then(system.getCount()).isEqualTo(4);
	}
}
 
Example #2
Source File: PropertySourceBootstrapConfiguration.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
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("Error opening logging config file " + logConfig, ex);
		}
	}
}
 
Example #3
Source File: LoggingEnvironmentApplicationListener.java    From spring-cloud-formula with Apache License 2.0 6 votes vote down vote up
/**
 * deducing groupId, artifactId
 *
 * @param event
 */
private void onApplicationStartingEvent(ApplicationStartingEvent event) {
    if (ClassUtils.isPresent("ch.qos.logback.core.Appender",
            event.getSpringApplication().getClassLoader())) {
        // base package
        Class<?> mainClass = event.getSpringApplication().getMainApplicationClass();
        if (mainClass != null) {
            String basePackage = mainClass.getPackage().getName();
            System.setProperty("BASE_PACKAGE", basePackage);
        } else {
            System.setProperty("BASE_PACKAGE", "");
            logger.warn("can not set BASE_PACKAGE correctly");
        }

        // set logging system impl
        System.setProperty(LoggingSystem.SYSTEM_PROPERTY, FormulaLogbackSystem.class.getName());
    }
}
 
Example #4
Source File: LoggerTest.java    From logger-spring-boot with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example #5
Source File: LoggingRebinder.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
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, resolveLogLevel(level));
	}
	catch (RuntimeException ex) {
		this.logger.error("Cannot set level: " + level + " for '" + name + "'");
	}
}
 
Example #6
Source File: LoggingRebinder.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
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());
	}
}
 
Example #7
Source File: LoggingRebinder.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(EnvironmentChangeEvent event) {
	if (this.environment == null) {
		return;
	}
	LoggingSystem system = LoggingSystem.get(LoggingSystem.class.getClassLoader());
	setLogLevels(system, this.environment);
}
 
Example #8
Source File: DebugLogging.java    From artifactory-resource with Apache License 2.0 5 votes vote down vote up
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);
	}
}
 
Example #9
Source File: LoggerTest.java    From logger-spring-boot with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #10
Source File: LoggerTest.java    From logger-spring-boot with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #11
Source File: LoggerTest.java    From logger-spring-boot with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example #12
Source File: LoggerTest.java    From logger-spring-boot with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #13
Source File: LoggerTest.java    From logger-spring-boot with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #14
Source File: LoggerTest.java    From logger-spring-boot with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example #15
Source File: LoggerTest.java    From logger-spring-boot with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #16
Source File: LoggerTest.java    From logger-spring-boot with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example #17
Source File: LoggerTest.java    From logger-spring-boot with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example #18
Source File: LoggingDslTests.java    From spring-fu with Apache License 2.0 5 votes vote down vote up
@Test
void changeDefaultROOTLogLevel() {
	var app = application(it -> it.logging(log -> log.level(LogLevel.DEBUG)));
	app.run();
	var loggingSystem = LoggingSystem.get(LoggingDslTests.class.getClassLoader());
	assertEquals(LogLevel.DEBUG, loggingSystem.getLoggerConfiguration("ROOT").getEffectiveLevel());
}
 
Example #19
Source File: LoggingDslTests.java    From spring-fu with Apache License 2.0 5 votes vote down vote up
@Test
void changePackageLogLevel() {
	var packageName = "org.springframework";
	var app = application(it -> it.logging(log -> log.level(packageName, LogLevel.DEBUG)));
	app.run();
	var loggingSystem = LoggingSystem.get(LoggingDslTests.class.getClassLoader());
	assertEquals(LogLevel.DEBUG, loggingSystem.getLoggerConfiguration(packageName).getEffectiveLevel());
}
 
Example #20
Source File: LoggingDslTests.java    From spring-fu with Apache License 2.0 5 votes vote down vote up
@Test
void changeClassLogLevel() {
	var loggingSystem = LoggingSystem.get(LoggingDslTests.class.getClassLoader());
	loggingSystem.setLogLevel("ROOT", LogLevel.INFO);
	var app = application(it -> it.logging(log -> log.level(DefaultListableBeanFactory.class, LogLevel.DEBUG)));
	app.run();
	assertEquals(LogLevel.DEBUG, loggingSystem.getLoggerConfiguration("org.springframework.beans.factory.support.DefaultListableBeanFactory").getEffectiveLevel());
}
 
Example #21
Source File: LoggerTest.java    From logger-spring-boot with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #22
Source File: LoggerTest.java    From logger-spring-boot with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #23
Source File: LoggerTest.java    From logger-spring-boot with Apache License 2.0 5 votes vote down vote up
@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"));
}
 
Example #24
Source File: LoggerTest.java    From logger-spring-boot with Apache License 2.0 4 votes vote down vote up
@Test
public void isLogTraceDisabled() {
    LoggingSystem.get(ClassLoader.getSystemClassLoader())
            .setLogLevel(org.slf4j.Logger.ROOT_LOGGER_NAME, LogLevel.OFF);
    assertFalse(logger.isEnabled(LogLevel.TRACE, "logger name"));
}
 
Example #25
Source File: ContextRefresherTests.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
@After
public void close() {
	System.clearProperty(LoggingSystem.SYSTEM_PROPERTY);
	TestLoggingSystem.count = 0;
}
 
Example #26
Source File: LoggingRebinderTests.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
@After
public void reset() {
	LoggingSystem.get(getClass().getClassLoader())
			.setLogLevel("org.springframework.web", LogLevel.INFO);
}
 
Example #27
Source File: LoggingSystemShutdownListener.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
private void shutdownLogging() {
	LoggingSystem loggingSystem = LoggingSystem
			.get(ClassUtils.getDefaultClassLoader());
	loggingSystem.cleanUp();
	loggingSystem.beforeInitialize();
}
 
Example #28
Source File: LogbackLoggingSystem.java    From super-cloudops with Apache License 2.0 4 votes vote down vote up
private boolean isAlreadyInitialized(LoggerContext loggerContext) {
	return loggerContext.getObject(LoggingSystem.class.getName()) != null;
}
 
Example #29
Source File: LogbackLoggingSystem.java    From super-cloudops with Apache License 2.0 4 votes vote down vote up
private void markAsInitialized(LoggerContext loggerContext) {
	loggerContext.putObject(LoggingSystem.class.getName(), new Object());
}
 
Example #30
Source File: LogbackLoggingSystem.java    From super-cloudops with Apache License 2.0 4 votes vote down vote up
private void markAsUninitialized(LoggerContext loggerContext) {
	loggerContext.removeObject(LoggingSystem.class.getName());
}