Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Q
qa-platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
QA
qa-platform
Commits
d46badee
Commit
d46badee
authored
Jul 30, 2020
by
黎博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add Request Aspect
parent
c25f15a3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
75 deletions
+75
-75
LogAspect.java
...ava/cn/quantgroup/qaplatform/common/aspect/LogAspect.java
+75
-75
No files found.
src/main/java/cn/quantgroup/qaplatform/common/aspect/LogAspect.java
View file @
d46badee
...
...
@@ -29,80 +29,80 @@ import java.util.Map;
/**
* 日志切面
*/
//
@Aspect
//
@Component
@Aspect
@Component
public
class
LogAspect
{
//
private Logger logger = LoggerFactory.getLogger(LogAspect.class);
//
private Map<String, Object> logMap = new HashMap<>();
//
//
@Pointcut("execution(public * cn.quantgroup.qaplatform.controller..*.*(..))")
//
public void apiLog() {}
//
//
/**
//
* 前置通知:在连接点之前执行的通知
//
*
//
* @param joinPoint
//
* @throws Throwable
//
*/
//
@Before("apiLog()")
//
public void doBefore(JoinPoint joinPoint) throws JsonProcessingException {
//
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//
HttpServletRequest request = servletRequestAttributes.getRequest();
//
Signature signature = joinPoint.getSignature();
//
MethodSignature methodSignature = (MethodSignature) signature;
//
Method method = methodSignature.getMethod();
//
logMap.put("url", request.getRequestURL().toString());
//
logMap.put("method", request.getMethod());
//
logMap.put("host", request.getRemoteAddr());
//
logMap.put("class", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
//
logMap.put("requestParams", getParameter(method, joinPoint.getArgs()));
//
// 记录下请求内容
//
logger.info("请求信息:" + JsonTransUtils.mapToJson(logMap));
//
}
//
//
/**
//
* 后置通知:在连接点之后执行的通知
//
*
//
* @param ret
//
* @throws Throwable
//
*/
//
@AfterReturning(returning = "ret", pointcut = "apiLog()")
//
public void doAfterReturning(Object ret) throws Throwable {
//
// 处理完请求,返回内容
//
logger.info("返回结果:" + JsonTransUtils.objToJson(ret));
//
}
//
//
/**
//
* 根据方法和传入参数获取请求参数
//
*/
//
private Object getParameter(Method method, Object[] args) {
//
List<Object> argList = new ArrayList<>();
//
Parameter[] parameters = method.getParameters();
//
for(int i=0; i < parameters.length; i++) {
//
//将RequestBody注解修饰的参数作为请求参数
//
RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class);
//
if (requestBody != null) {
//
argList.add(args[i]);
//
}
//
//
//将RequestParam注解修饰的参数作为请求参数
//
RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class);
//
if (requestParam != null) {
//
Map<String, Object> map = new HashMap<>();
//
String key = parameters[i].getName();
//
if (!StringUtils.isEmpty(requestParam.value())) {
//
key = requestParam.value();
//
}
//
map.put(key, args[i]);
//
argList.add(map);
//
}
//
}
//
if (argList.size() == 0) {
//
return null;
//
} else if (argList.size() == 1) {
//
return argList.get(0);
//
} else {
//
return argList;
//
}
//
}
private
Logger
logger
=
LoggerFactory
.
getLogger
(
LogAspect
.
class
);
private
Map
<
String
,
Object
>
logMap
=
new
HashMap
<>();
@Pointcut
(
"execution(public * cn.quantgroup.qaplatform.controller..*.*(..))"
)
public
void
apiLog
()
{}
/**
* 前置通知:在连接点之前执行的通知
*
* @param joinPoint
* @throws Throwable
*/
@Before
(
"apiLog()"
)
public
void
doBefore
(
JoinPoint
joinPoint
)
throws
JsonProcessingException
{
ServletRequestAttributes
servletRequestAttributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
HttpServletRequest
request
=
servletRequestAttributes
.
getRequest
();
Signature
signature
=
joinPoint
.
getSignature
();
MethodSignature
methodSignature
=
(
MethodSignature
)
signature
;
Method
method
=
methodSignature
.
getMethod
();
logMap
.
put
(
"url"
,
request
.
getRequestURL
().
toString
());
logMap
.
put
(
"method"
,
request
.
getMethod
());
logMap
.
put
(
"host"
,
request
.
getRemoteAddr
());
logMap
.
put
(
"class"
,
joinPoint
.
getSignature
().
getDeclaringTypeName
()
+
"."
+
joinPoint
.
getSignature
().
getName
());
logMap
.
put
(
"requestParams"
,
getParameter
(
method
,
joinPoint
.
getArgs
()));
// 记录下请求内容
logger
.
info
(
"请求信息:"
+
JsonTransUtils
.
mapToJson
(
logMap
));
}
/**
* 后置通知:在连接点之后执行的通知
*
* @param ret
* @throws Throwable
*/
@AfterReturning
(
returning
=
"ret"
,
pointcut
=
"apiLog()"
)
public
void
doAfterReturning
(
Object
ret
)
throws
Throwable
{
// 处理完请求,返回内容
logger
.
info
(
"返回结果:"
+
JsonTransUtils
.
objToJson
(
ret
));
}
/**
* 根据方法和传入参数获取请求参数
*/
private
Object
getParameter
(
Method
method
,
Object
[]
args
)
{
List
<
Object
>
argList
=
new
ArrayList
<>();
Parameter
[]
parameters
=
method
.
getParameters
();
for
(
int
i
=
0
;
i
<
parameters
.
length
;
i
++)
{
//将RequestBody注解修饰的参数作为请求参数
RequestBody
requestBody
=
parameters
[
i
].
getAnnotation
(
RequestBody
.
class
);
if
(
requestBody
!=
null
)
{
argList
.
add
(
args
[
i
]);
}
//将RequestParam注解修饰的参数作为请求参数
RequestParam
requestParam
=
parameters
[
i
].
getAnnotation
(
RequestParam
.
class
);
if
(
requestParam
!=
null
)
{
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
String
key
=
parameters
[
i
].
getName
();
if
(!
StringUtils
.
isEmpty
(
requestParam
.
value
()))
{
key
=
requestParam
.
value
();
}
map
.
put
(
key
,
args
[
i
]);
argList
.
add
(
map
);
}
}
if
(
argList
.
size
()
==
0
)
{
return
null
;
}
else
if
(
argList
.
size
()
==
1
)
{
return
argList
.
get
(
0
);
}
else
{
return
argList
;
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment