MDC改用ThreadContext,原文:org.apache.log4j.MDC and org.apache.log4j.NDC have been replaced by the Thread
Context.
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.GenericApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
/**
* @author wsf 2019/7/9
*/
public class ApplicationStartedEventListener implements GenericApplicationListener {
private static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
private static Class<?>[] EVENT_TYPES = {ApplicationStartingEvent.class,
ApplicationEnvironmentPreparedEvent.class, ApplicationPreparedEvent.class,
ContextClosedEvent.class, ApplicationFailedEvent.class};
private static Class<?>[] SOURCE_TYPES = {SpringApplication.class,
ApplicationContext.class};
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
ConfigurableEnvironment envi = ((ApplicationEnvironmentPreparedEvent) event).getEnvironment();
MutablePropertySources mps = envi.getPropertySources();
PropertySource<?> ps = mps.get("applicationConfig: [classpath:/application.yml]");
if(ps != null && ps.containsProperty("spring.application.name")) {
String appName = (String) ps.getProperty("spring.application.name");
if (StringUtils.isNotBlank(appName)) {
ThreadContext.put("appName", appName);
}
}
String profile = envi.getActiveProfiles()[0];
if (StringUtils.isNotBlank(profile)) {
ThreadContext.put("profile", profile);
}
}
}
@Override
public int getOrder() {
// 设置加载优先级,在日志之前加载
return DEFAULT_ORDER;
}
@Override
public boolean supportsEventType(ResolvableType resolvableType) {
return isAssignableFrom(resolvableType.getRawClass(), EVENT_TYPES);
}
@Override
public boolean supportsSourceType(Class<?> sourceType) {
return isAssignableFrom(sourceType, SOURCE_TYPES);
}
private boolean isAssignableFrom(Class<?> type, Class<?>... supportedTypes) {
if (type != null) {
for (Class<?> supportedType : supportedTypes) {
if (supportedType.isAssignableFrom(type)) {
return true;
}
}
}
return false;
}
}