Skip to content

Commit

Permalink
refactor: 将 EnableAopLogMethod 拆分为2个注解 "EnableAopLogMethod/EnableAopL…
Browse files Browse the repository at this point in the history
…ogController",语意和功能清晰明了
  • Loading branch information
myoss committed Dec 28, 2018
1 parent b551f40 commit 185d9a0
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public class ApmConstants {
* 使用slf4j记录方法的入参和出参,配置前缀
*/
public static final String MONITOR_METHOD_CONFIG_PREFIX = CONFIG_PREFIX + ".method";
/**
* 使用slf4j记录 {@link org.springframework.stereotype.Controller} 的信息,配置前缀
*/
public static final String MONITOR_CONTROLLER_CONFIG_PREFIX = CONFIG_PREFIX + ".controller";
/**
* 每天定时检查日志文件,是否已经滚动生成新的文件,没有就触发生成新的文件,配置前缀
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;

import app.myoss.cloud.apm.log.method.aspectj.annotation.EnableAopLogMethod;
import app.myoss.cloud.apm.log.method.aspectj.annotation.MonitorMethodAdvice;
Expand Down Expand Up @@ -60,16 +59,6 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
scanner.addIncludeFilter(new AnnotationTypeFilter(MonitorMethodAdvice.class));
}

// 监控 controller 异常
boolean enableAopLogControllerException = attributes.getBoolean("enableAopLogControllerException");
if (!enableAopLogControllerException) {
scanner.addExcludeFilter(new AnnotationTypeFilter(ControllerAdvice.class));
} else {
scanner.addIncludeFilter(new AnnotationTypeFilter(ControllerAdvice.class));
// app.myoss.cloud.web.spring.web.method.aspectj.AopLogControllerExceptionHandler
scanPackages.add("app.myoss.cloud.web.spring.web.method.aspectj");
}

// 扫描注册
String packageName = ClassUtils.getPackageName(AopLogMethodRegistrar.class);
scanPackages.add(packageName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,7 @@ public class MonitorMethodProperties {
*
* @see AbstractMonitorMethod#toJSONString(Object)
*/
private String dateFormat = "yyyy-MM-dd HH:mm:ss.SSS";

/**
* Controller异常时返回的errorCode
*
* @see AopLogControllerExceptionHandler#outputException(Throwable,
* org.springframework.http.HttpHeaders,
* org.springframework.http.HttpStatus,
* javax.servlet.http.HttpServletRequest)
*/
private String controllerExceptionErrorCode = "systemException";
/**
* Controller异常时返回的errorCode
*
* @see AopLogControllerExceptionHandler#outputException(Throwable,
* org.springframework.http.HttpHeaders,
* org.springframework.http.HttpStatus,
* javax.servlet.http.HttpServletRequest)
*/
private String controllerExceptionErrorMsg = "We'll be back soon ...";
private String dateFormat = "yyyy-MM-dd HH:mm:ss.SSS";

/**
* 初始化属性
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
* <li>{@link LogMethodAfter}
* <li>{@link LogMethodAround}
* <li>{@link LogUnMonitor}
* <li>{@code app.myoss.cloud.web.spring.web.method.aspectj.AopLogControllerExceptionHandler}
* </ul>
* 使用例子:
*
Expand Down Expand Up @@ -68,13 +67,4 @@
* @return 默认开启
*/
boolean enableAopLogMethod() default true;

/**
* 开启自动记录 controller异常,
* 使用:{@code app.myoss.cloud.web.spring.web.method.aspectj.AopLogControllerExceptionHandler}
* 处理异常信息
*
* @return 默认开启(如果不是WebApplication,是不起作用的)
*/
boolean enableAopLogControllerException() default true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* @author Jerry.Chen
* @since 2018年12月15日 下午10:11:48
*/
@EnableAopLogMethod
@EnableScheduling
@Configurable
public class ApmAutoConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.web.client.RestTemplate;

import app.myoss.cloud.web.spring.boot.config.http.RestTemplate4OkHttp3ClientAutoConfiguration;
import app.myoss.cloud.web.spring.web.method.aspectj.annatation.EnableAopLogController;
import app.myoss.cloud.web.utils.RestClient;

/**
Expand All @@ -35,6 +36,7 @@
* @author Jerry.Chen
* @since 2018年12月26日 下午2:11:02
*/
@EnableAopLogController
@AutoConfigureAfter(RestTemplate4OkHttp3ClientAutoConfiguration.class)
@Configuration
public class WebAutoConfiguration extends AbstractWebMvcConfigurer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@

import com.alibaba.fastjson.JSON;

import app.myoss.cloud.apm.log.method.aspectj.MonitorMethodProperties;
import app.myoss.cloud.apm.spring.cloud.sleuth.trace.ApplicationEventTracer;
import app.myoss.cloud.core.constants.MyossConstants;
import app.myoss.cloud.web.spring.web.servlet.support.EmptyBodyCheckingHttpInputMessage;
Expand All @@ -58,21 +57,21 @@
* {@link ResponseEntityExceptionHandler#handleExceptionInternal(Exception, Object, HttpHeaders, HttpStatus, WebRequest)}
* ,因为所有的异常处理最后一步都会走到这里,对外的错误异常统一输出,完美解决。
* <p>
* 如果要开启此功能,{@link app.myoss.cloud.apm.log.method.aspectj.annotation.EnableAopLogMethod}
* 如果要开启此功能,{@link app.myoss.cloud.web.spring.web.method.aspectj.annatation.EnableAopLogController}
* <p>
* 如果要禁用掉此功能,{@link app.myoss.cloud.apm.log.method.aspectj.annotation.EnableAopLogMethod#enableAopLogControllerException()}
* 如果要禁用掉此功能,{@link app.myoss.cloud.web.spring.web.method.aspectj.annatation.EnableAopLogController#enableAopLogControllerException()}
*
* @author Jerry.Chen
* @since 2018年4月11日 下午12:10:23
* @see ResponseEntityExceptionHandler
* @see app.myoss.cloud.apm.log.method.aspectj.annotation.EnableAopLogMethod
* @see app.myoss.cloud.web.spring.web.method.aspectj.annatation.EnableAopLogController
*/
@ConditionalOnWebApplication
@Slf4j
@ControllerAdvice
public class AopLogControllerExceptionHandler extends ResponseEntityExceptionHandler {
@Autowired
protected MonitorMethodProperties properties;
protected MonitorControllerProperties properties;

/**
* 获取 HTTP 请求响应状态码
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2018-2018 https://github.com/myoss
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package app.myoss.cloud.web.spring.web.method.aspectj;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;

import app.myoss.cloud.web.spring.web.method.aspectj.annatation.EnableAopLogController;

/**
* 扫描当前package下的 {@link org.springframework.stereotype.Component},并进行 Bean 的自动注册
*
* @author Jerry.Chen
* @since 2018年12月28日 下午3:25:48
*/
public class AopLogControllerRegistrar implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {
private ResourceLoader resourceLoader;

@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
AnnotationAttributes attributes = AnnotationAttributes
.fromMap(importingClassMetadata.getAnnotationAttributes(EnableAopLogController.class.getName()));
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);
scanner.resetFilters(false);
scanner.setResourceLoader(this.resourceLoader);
List<String> scanPackages = new ArrayList<>();

// 监控 controller 异常
boolean enableAopLogControllerException = attributes.getBoolean("enableAopLogControllerException");
if (!enableAopLogControllerException) {
scanner.addExcludeFilter(new AnnotationTypeFilter(ControllerAdvice.class));
} else {
scanner.addIncludeFilter(new AnnotationTypeFilter(ControllerAdvice.class));
}

// 扫描注册
String packageName = ClassUtils.getPackageName(AopLogControllerRegistrar.class);
scanPackages.add(packageName);
scanner.scan(scanPackages.toArray(new String[0]));
}

@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2018-2018 https://github.com/myoss
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package app.myoss.cloud.web.spring.web.method.aspectj;

import org.springframework.boot.context.properties.ConfigurationProperties;

import app.myoss.cloud.apm.constants.ApmConstants;
import lombok.Data;

/**
* 使用slf4j记录 {@link org.springframework.stereotype.Controller} 的信息,属性配置
*
* @author Jerry.Chen
* @since 2018年4月13日 下午12:15:37
*/
@Data
@ConfigurationProperties(prefix = ApmConstants.MONITOR_CONTROLLER_CONFIG_PREFIX)
public class MonitorControllerProperties {
/**
* Controller异常时返回的errorCode
*
* @see AopLogControllerExceptionHandler#outputException(Throwable,
* org.springframework.http.HttpHeaders,
* org.springframework.http.HttpStatus,
* javax.servlet.http.HttpServletRequest)
*/
private String controllerExceptionErrorCode = "systemException";
/**
* Controller异常时返回的errorCode
*
* @see AopLogControllerExceptionHandler#outputException(Throwable,
* org.springframework.http.HttpHeaders,
* org.springframework.http.HttpStatus,
* javax.servlet.http.HttpServletRequest)
*/
private String controllerExceptionErrorMsg = "We'll be back soon ...";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2018-2018 https://github.com/myoss
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package app.myoss.cloud.web.spring.web.method.aspectj.annatation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Import;

import app.myoss.cloud.web.spring.web.method.aspectj.AopLogControllerRegistrar;
import app.myoss.cloud.web.spring.web.method.aspectj.MonitorControllerProperties;

/**
* 开启自动记录 {@link org.springframework.stereotype.Controller} 信息
* <ul>
* <li>{@link app.myoss.cloud.web.spring.web.method.aspectj.AopLogControllerExceptionHandler}
* </ul>
* 使用例子:
*
* <pre>
* &#064;EnableAopLogController
* &#064;Configuration
* public class Config {
* }
* </pre>
*
* @author Jerry.Chen
* @since 2018年12月28日 下午3:25:48
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@EnableConfigurationProperties(MonitorControllerProperties.class)
@Import(AopLogControllerRegistrar.class)
public @interface EnableAopLogController {
/**
* 开启自动记录 controller异常,
* 使用:{@link app.myoss.cloud.web.spring.web.method.aspectj.AopLogControllerExceptionHandler}
* 处理异常信息
*
* @return 默认开启(如果不是WebApplication,是不起作用的)
*/
boolean enableAopLogControllerException() default true;
}

0 comments on commit 185d9a0

Please sign in to comment.