1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

refactor sentinel log

This commit is contained in:
fangjian0423 2019-02-20 17:08:20 +08:00
parent eeedf90d86
commit 9168e60da4
5 changed files with 21 additions and 26 deletions

View File

@ -53,7 +53,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
public abstract class SentinelConverter<T extends AbstractRule> public abstract class SentinelConverter<T extends AbstractRule>
implements Converter<String, List<AbstractRule>> { implements Converter<String, List<AbstractRule>> {
private static final Logger logger = LoggerFactory.getLogger(SentinelConverter.class); private static final Logger log = LoggerFactory.getLogger(SentinelConverter.class);
private final ObjectMapper objectMapper; private final ObjectMapper objectMapper;
@ -68,7 +68,7 @@ public abstract class SentinelConverter<T extends AbstractRule>
public List<AbstractRule> convert(String source) { public List<AbstractRule> convert(String source) {
List<AbstractRule> ruleList = new ArrayList<>(); List<AbstractRule> ruleList = new ArrayList<>();
if (StringUtils.isEmpty(source)) { if (StringUtils.isEmpty(source)) {
logger.warn("converter can not convert rules because source is empty"); log.warn("converter can not convert rules because source is empty");
return ruleList; return ruleList;
} }
try { try {

View File

@ -42,7 +42,7 @@ import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
@EnableConfigurationProperties(SentinelProperties.class) @EnableConfigurationProperties(SentinelProperties.class)
public class SentinelWebAutoConfiguration { public class SentinelWebAutoConfiguration {
private static final Logger logger = LoggerFactory private static final Logger log = LoggerFactory
.getLogger(SentinelWebAutoConfiguration.class); .getLogger(SentinelWebAutoConfiguration.class);
@Autowired @Autowired
@ -65,7 +65,7 @@ public class SentinelWebAutoConfiguration {
Filter filter = new CommonFilter(); Filter filter = new CommonFilter();
registration.setFilter(filter); registration.setFilter(filter);
registration.setOrder(filterConfig.getOrder()); registration.setOrder(filterConfig.getOrder());
logger.info("[Sentinel Starter] register Sentinel with urlPatterns: {}.", log.info("[Sentinel Starter] register Sentinel with urlPatterns: {}.",
filterConfig.getUrlPatterns()); filterConfig.getUrlPatterns());
return registration; return registration;

View File

@ -53,7 +53,7 @@ import com.alibaba.csp.sentinel.slots.block.BlockException;
*/ */
public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProcessor { public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProcessor {
private static final Logger logger = LoggerFactory private static final Logger log = LoggerFactory
.getLogger(SentinelBeanPostProcessor.class); .getLogger(SentinelBeanPostProcessor.class);
private final ApplicationContext applicationContext; private final ApplicationContext applicationContext;
@ -100,14 +100,14 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
return; return;
} }
if (blockClass != void.class && StringUtils.isEmpty(blockMethod)) { if (blockClass != void.class && StringUtils.isEmpty(blockMethod)) {
logger.error( log.error(
"{} class attribute exists but {} method attribute is not exists in bean[{}]", "{} class attribute exists but {} method attribute is not exists in bean[{}]",
type, type, beanName); type, type, beanName);
throw new IllegalArgumentException(type + " class attribute exists but " throw new IllegalArgumentException(type + " class attribute exists but "
+ type + " method attribute is not exists in bean[" + beanName + "]"); + type + " method attribute is not exists in bean[" + beanName + "]");
} }
else if (blockClass == void.class && !StringUtils.isEmpty(blockMethod)) { else if (blockClass == void.class && !StringUtils.isEmpty(blockMethod)) {
logger.error( log.error(
"{} method attribute exists but {} class attribute is not exists in bean[{}]", "{} method attribute exists but {} class attribute is not exists in bean[{}]",
type, type, beanName); type, type, beanName);
throw new IllegalArgumentException(type + " method attribute exists but " throw new IllegalArgumentException(type + " method attribute exists but "
@ -123,7 +123,7 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
String argsStr = Arrays.toString(argList.toArray()); String argsStr = Arrays.toString(argList.toArray());
Method foundMethod = ClassUtils.getStaticMethod(blockClass, blockMethod, args); Method foundMethod = ClassUtils.getStaticMethod(blockClass, blockMethod, args);
if (foundMethod == null) { if (foundMethod == null) {
logger.error( log.error(
"{} static method can not be found in bean[{}]. The right method signature is {}#{}{}, please check your class name, method name and arguments", "{} static method can not be found in bean[{}]. The right method signature is {}#{}{}, please check your class name, method name and arguments",
type, beanName, blockClass.getName(), blockMethod, argsStr); type, beanName, blockClass.getName(), blockMethod, argsStr);
throw new IllegalArgumentException(type throw new IllegalArgumentException(type
@ -134,7 +134,7 @@ public class SentinelBeanPostProcessor implements MergedBeanDefinitionPostProces
} }
if (!ClientHttpResponse.class.isAssignableFrom(foundMethod.getReturnType())) { if (!ClientHttpResponse.class.isAssignableFrom(foundMethod.getReturnType())) {
logger.error( log.error(
"{} method return value in bean[{}] is not ClientHttpResponse: {}#{}{}", "{} method return value in bean[{}] is not ClientHttpResponse: {}#{}{}",
type, beanName, blockClass.getName(), blockMethod, argsStr); type, beanName, blockClass.getName(), blockMethod, argsStr);
throw new IllegalArgumentException(type + " method return value in bean[" throw new IllegalArgumentException(type + " method return value in bean["

View File

@ -52,7 +52,7 @@ import com.alibaba.csp.sentinel.slots.block.AbstractRule;
*/ */
public class SentinelDataSourceHandler implements SmartInitializingSingleton { public class SentinelDataSourceHandler implements SmartInitializingSingleton {
private static final Logger logger = LoggerFactory private static final Logger log = LoggerFactory
.getLogger(SentinelDataSourceHandler.class); .getLogger(SentinelDataSourceHandler.class);
private List<String> dataTypeList = Arrays.asList("json", "xml"); private List<String> dataTypeList = Arrays.asList("json", "xml");
@ -78,7 +78,7 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
try { try {
List<String> validFields = dataSourceProperties.getValidField(); List<String> validFields = dataSourceProperties.getValidField();
if (validFields.size() != 1) { if (validFields.size() != 1) {
logger.error("[Sentinel Starter] DataSource " + dataSourceName log.error("[Sentinel Starter] DataSource " + dataSourceName
+ " multi datasource active and won't loaded: " + " multi datasource active and won't loaded: "
+ dataSourceProperties.getValidField()); + dataSourceProperties.getValidField());
return; return;
@ -90,7 +90,7 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
+ validFields.get(0) + "-datasource"); + validFields.get(0) + "-datasource");
} }
catch (Exception e) { catch (Exception e) {
logger.error("[Sentinel Starter] DataSource " + dataSourceName log.error("[Sentinel Starter] DataSource " + dataSourceName
+ " build error: " + e.getMessage(), e); + " build error: " + e.getMessage(), e);
} }
} }
@ -109,8 +109,8 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
} }
} }
catch (IllegalAccessException e) { catch (IllegalAccessException e) {
logger.error("[Sentinel Starter] DataSource " + dataSourceName log.error("[Sentinel Starter] DataSource " + dataSourceName + " field: "
+ " field: " + field.getName() + " invoke error"); + field.getName() + " invoke error");
throw new RuntimeException("[Sentinel Starter] DataSource " throw new RuntimeException("[Sentinel Starter] DataSource "
+ dataSourceName + " field: " + field.getName() + " invoke error", + dataSourceName + " field: " + field.getName() + " invoke error",
e); e);
@ -157,7 +157,7 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
builder.addPropertyReference("converter", customConvertBeanName); builder.addPropertyReference("converter", customConvertBeanName);
} }
catch (ClassNotFoundException e) { catch (ClassNotFoundException e) {
logger.error("[Sentinel Starter] DataSource " + dataSourceName log.error("[Sentinel Starter] DataSource " + dataSourceName
+ " handle " + " handle "
+ dataSourceProperties.getClass().getSimpleName() + dataSourceProperties.getClass().getSimpleName()
+ " error, class name: " + " error, class name: "
@ -217,14 +217,14 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
ruleConfig = dataSource.loadConfig(); ruleConfig = dataSource.loadConfig();
} }
catch (Exception e) { catch (Exception e) {
logger.error("[Sentinel Starter] DataSource " + dataSourceName log.error("[Sentinel Starter] DataSource " + dataSourceName
+ " loadConfig error: " + e.getMessage(), e); + " loadConfig error: " + e.getMessage(), e);
return; return;
} }
if (ruleConfig instanceof List) { if (ruleConfig instanceof List) {
List convertedRuleList = (List) ruleConfig; List convertedRuleList = (List) ruleConfig;
if (CollectionUtils.isEmpty(convertedRuleList)) { if (CollectionUtils.isEmpty(convertedRuleList)) {
logger.warn("[Sentinel Starter] DataSource {} rule list is empty.", log.warn("[Sentinel Starter] DataSource {} rule list is empty.",
dataSourceName); dataSourceName);
return; return;
} }
@ -235,23 +235,23 @@ public class SentinelDataSourceHandler implements SmartInitializingSingleton {
} }
} }
if (matchCount == 0) { if (matchCount == 0) {
logger.error("[Sentinel Starter] DataSource {} none rules are {} type.", log.error("[Sentinel Starter] DataSource {} none rules are {} type.",
dataSourceName, ruleClass.getSimpleName()); dataSourceName, ruleClass.getSimpleName());
throw new IllegalArgumentException("[Sentinel Starter] DataSource " throw new IllegalArgumentException("[Sentinel Starter] DataSource "
+ dataSourceName + " none rules are " + ruleClass.getSimpleName() + dataSourceName + " none rules are " + ruleClass.getSimpleName()
+ " type."); + " type.");
} }
else if (matchCount != convertedRuleList.size()) { else if (matchCount != convertedRuleList.size()) {
logger.warn("[Sentinel Starter] DataSource {} all rules are not {} type.", log.warn("[Sentinel Starter] DataSource {} all rules are not {} type.",
dataSourceName, ruleClass.getSimpleName()); dataSourceName, ruleClass.getSimpleName());
} }
else { else {
logger.info("[Sentinel Starter] DataSource {} load {} {}", dataSourceName, log.info("[Sentinel Starter] DataSource {} load {} {}", dataSourceName,
convertedRuleList.size(), ruleClass.getSimpleName()); convertedRuleList.size(), ruleClass.getSimpleName());
} }
} }
else { else {
logger.error("[Sentinel Starter] DataSource " + dataSourceName log.error("[Sentinel Starter] DataSource " + dataSourceName
+ " rule class is not List<" + ruleClass.getSimpleName() + " rule class is not List<" + ruleClass.getSimpleName()
+ ">. Class: " + ruleConfig.getClass()); + ">. Class: " + ruleConfig.getClass());
throw new IllegalArgumentException("[Sentinel Starter] DataSource " throw new IllegalArgumentException("[Sentinel Starter] DataSource "

View File

@ -21,8 +21,6 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URI; import java.net.URI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.alibaba.sentinel.annotation.SentinelRestTemplate; import org.springframework.cloud.alibaba.sentinel.annotation.SentinelRestTemplate;
import org.springframework.cloud.alibaba.sentinel.rest.SentinelClientHttpResponse; import org.springframework.cloud.alibaba.sentinel.rest.SentinelClientHttpResponse;
import org.springframework.http.HttpRequest; import org.springframework.http.HttpRequest;
@ -44,9 +42,6 @@ import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
*/ */
public class SentinelProtectInterceptor implements ClientHttpRequestInterceptor { public class SentinelProtectInterceptor implements ClientHttpRequestInterceptor {
private static final Logger logger = LoggerFactory
.getLogger(SentinelProtectInterceptor.class);
private final SentinelRestTemplate sentinelRestTemplate; private final SentinelRestTemplate sentinelRestTemplate;
public SentinelProtectInterceptor(SentinelRestTemplate sentinelRestTemplate) { public SentinelProtectInterceptor(SentinelRestTemplate sentinelRestTemplate) {