Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suport full annotation style #329

Merged
merged 9 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
*/
package com.alipay.sofa.healthcheck.initializer;

import com.alipay.sofa.common.log.Constants;
import com.alipay.sofa.healthcheck.configuration.HealthCheckConstants;
import com.alipay.sofa.healthcheck.log.SofaBootHealthCheckLoggerFactory;
import com.alipay.sofa.infra.log.space.SofaBootLogSpaceIsolationInit;
import com.alipay.sofa.infra.utils.SOFABootEnvUtils;
import org.slf4j.Logger;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
Expand All @@ -34,19 +32,16 @@ public class SofaBootHealthCheckInitializer
implements
ApplicationContextInitializer<ConfigurableApplicationContext> {

private static final Logger LOGGER = SofaBootHealthCheckLoggerFactory
.getLogger(SofaBootHealthCheckInitializer.class);

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
Environment environment = applicationContext.getEnvironment();
if (SOFABootEnvUtils.isSpringCloudBootstrapEnvironment(environment)) {
return;
}
// init logging.level.com.alipay.sofa.runtime argument
String healthCheckLogLevelKey = Constants.LOG_LEVEL_PREFIX
+ HealthCheckConstants.SOFABOOT_HEALTH_LOG_SPACE;
SofaBootLogSpaceIsolationInit.initSofaBootLogger(environment, healthCheckLogLevelKey);

SofaBootHealthCheckLoggerFactory.getLogger(SofaBootHealthCheckInitializer.class).info(
"SOFABoot HealthCheck Starting!");
LOGGER.info("SOFABoot HealthCheck Starting!");
}

}
13 changes: 10 additions & 3 deletions infra-sofa-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,17 @@
</properties>

<dependencies>
<!--strictly depend on log-sofa-boot-starter-->
<dependency>
<groupId>com.alipay.sofa.common</groupId>
<artifactId>log-sofa-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
Expand Down Expand Up @@ -155,19 +162,19 @@
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.3</version>
<version>2.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.3</version>
<version>2.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.3</version>
<version>2.7</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public class CommonMiddlewareConstants {

public static final String SOFA_BOOTSTRAP = "sofaBootstrap";

public static final String SPRING_CLOUD_BOOTSTRAP = "bootstrap";
public static final String SOFA_HIGH_PRIORITY_CONFIG = "sofaHighPriorityConfig";

/**
* {@link org.springframework.boot.ResourceBanner#getVersionsMap}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
*/
package com.alipay.sofa.infra.initializer;

import com.alipay.sofa.common.log.Constants;
import com.alipay.sofa.infra.log.InfraHealthCheckLoggerFactory;
import com.alipay.sofa.infra.log.space.SofaBootLogSpaceIsolationInit;
import com.alipay.sofa.infra.utils.SOFABootEnvUtils;
import org.slf4j.Logger;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.Ordered;
Expand All @@ -35,21 +34,17 @@ public class SOFABootInfrastructureSpringContextInitializer
ApplicationContextInitializer<ConfigurableApplicationContext>,
Ordered {

private final static Logger LOGGER = InfraHealthCheckLoggerFactory
.getLogger(SOFABootInfrastructureSpringContextInitializer.class);

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
//init log
Environment environment = applicationContext.getEnvironment();
if (SOFABootEnvUtils.isSpringCloudBootstrapEnvironment(environment)) {
return;
}
String infraLogLevelKey = Constants.LOG_LEVEL_PREFIX
+ InfraHealthCheckLoggerFactory.INFRASTRUCTURE_LOG_SPACE;
SofaBootLogSpaceIsolationInit.initSofaBootLogger(environment, infraLogLevelKey);

InfraHealthCheckLoggerFactory.getLogger(
SOFABootInfrastructureSpringContextInitializer.class).info(
"SOFABoot Infrastructure Starting!");

LOGGER.info("SOFABoot Infrastructure Starting!");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import com.alipay.sofa.common.log.env.LogEnvUtils;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.*;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

Expand All @@ -42,24 +47,23 @@
public class SofaBootstrapRunListener implements
ApplicationListener<ApplicationEnvironmentPreparedEvent>,
Ordered {
private final static String LOGGING_PATH = "logging.path";
private final static String LOGGING_LEVEL = "logging.level";
private static AtomicBoolean executed = new AtomicBoolean(false);

private static AtomicBoolean executed = new AtomicBoolean(false);
private final static MapPropertySource HIGH_PRIORITY_CONFIG = new MapPropertySource(
CommonMiddlewareConstants.SOFA_HIGH_PRIORITY_CONFIG,
new HashMap<String, Object>());

/**
* config log settings
*/
private void assemblyLogSetting(ConfigurableEnvironment environment) {
if (StringUtils.hasText(environment.getProperty(LOGGING_PATH))) {
System.getProperties().setProperty(LOGGING_PATH, environment.getProperty(LOGGING_PATH));
}
for (PropertySource propertySource : environment.getPropertySources()) {
if (!(propertySource instanceof EnumerablePropertySource)) {
continue;
}
for (String key : ((EnumerablePropertySource) propertySource).getPropertyNames()) {
if (key.startsWith(LOGGING_LEVEL)) {
System.setProperty(key, environment.getProperty(key));
if (LogEnvUtils.filterAllLogConfig(key)) {
HIGH_PRIORITY_CONFIG.getSource().put(key, environment.getProperty(key));
}
}
}
Expand All @@ -71,7 +75,7 @@ private void assemblyLogSetting(ConfigurableEnvironment environment) {
*/
private void assemblyRequireProperties(ConfigurableEnvironment environment) {
if (StringUtils.hasText(environment.getProperty(CommonMiddlewareConstants.APP_NAME_KEY))) {
System.getProperties().setProperty(CommonMiddlewareConstants.APP_NAME_KEY,
HIGH_PRIORITY_CONFIG.getSource().put(CommonMiddlewareConstants.APP_NAME_KEY,
environment.getProperty(CommonMiddlewareConstants.APP_NAME_KEY));
}
}
Expand Down Expand Up @@ -104,12 +108,13 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
SpringApplication application = event.getSpringApplication();
if (SOFABootEnvUtils.isSpringCloud() && executed.compareAndSet(false, true)) {
StandardEnvironment bootstrapEnvironment = new StandardEnvironment();
for (PropertySource<?> source : event.getEnvironment().getPropertySources()) {
if (source instanceof PropertySource.StubPropertySource) {
for (PropertySource propertySource : bootstrapEnvironment.getPropertySources()) {
if (propertySource instanceof PropertySource.StubPropertySource) {
continue;
}
bootstrapEnvironment.getPropertySources().addLast(source);
bootstrapEnvironment.getPropertySources().addLast(propertySource);
}

List<Class> sources = new ArrayList<>();
for (Object s : application.getSources()) {
if (s instanceof Class) {
Expand All @@ -123,18 +128,25 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
.environment(bootstrapEnvironment).sources(sources.toArray(new Class[] {}))
.registerShutdownHook(false).logStartupInfo(false).web(false).listeners()
.initializers().build(event.getArgs());

ApplicationEnvironmentPreparedEvent bootstrapEvent = new ApplicationEnvironmentPreparedEvent(
bootstrapApplication, event.getArgs(), bootstrapEnvironment);
for (ApplicationListener listener : application.getListeners()) {
if (listener instanceof ConfigFileApplicationListener) {
listener.onApplicationEvent(bootstrapEvent);

for (ApplicationListener applicationListener : application.getListeners()) {
if (applicationListener instanceof ConfigFileApplicationListener) {
applicationListener.onApplicationEvent(bootstrapEvent);
}
}

assemblyLogSetting(bootstrapEnvironment);
assemblyRequireProperties(bootstrapEnvironment);
assemblyEnvironmentMark(environment);
} else {
unAssemblyEnvironmentMark(environment);
}
if (environment.getPropertySources().contains(
CommonMiddlewareConstants.SPRING_CLOUD_BOOTSTRAP)) {
environment.getPropertySources().addLast(HIGH_PRIORITY_CONFIG);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @author yangguanchao
* @since 2018/04/09
*/
@Deprecated
public class SofaBootLogSpaceIsolationInit {

/***
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<configuration status="OFF">
<Properties>
<!-- 将系统属性进行一次本地转换 -->
<Property name="INFRASTRUCTURE_LOG_LEVEL">${sys:logging.level.com.alipay.sofa.infra}</Property>
<Property name="INFRASTRUCTURE_LOG_PATH">${sys:logging.path}</Property>
<Property name="INFRASTRUCTURE_FILE_ENCODING">${sys:file.encoding}</Property>
<Property name="INFRASTRUCTURE_LOG_LEVEL">${ctx:logging.level.com.alipay.sofa.infra}</Property>
<Property name="INFRASTRUCTURE_LOG_PATH">${ctx:logging.path}</Property>
<Property name="INFRASTRUCTURE_FILE_ENCODING">${ctx:file.encoding}</Property>
</Properties>

<appenders>
Expand Down
Loading