Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
X
xyqb-user2
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
head_group
xyqb-user2
Commits
2a695227
Commit
2a695227
authored
Apr 20, 2017
by
lee_mingzhu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加日志切面,拦截打印所有InnerApiController类中的所有方法的参数
parent
67b64bff
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
19 deletions
+26
-19
ParamLogAdvice.java
...in/java/cn/quantgroup/xyqb/aspect/log/ParamLogAdvice.java
+26
-19
No files found.
src/main/java/cn/quantgroup/xyqb/aspect/log/ParamLogAdvice.java
View file @
2a695227
...
...
@@ -9,17 +9,14 @@ import javassist.NotFoundException;
import
javassist.bytecode.CodeAttribute
;
import
javassist.bytecode.LocalVariableAttribute
;
import
javassist.bytecode.MethodInfo
;
import
org.aspectj.lang.
Proceeding
JoinPoint
;
import
org.aspectj.lang.annotation.A
round
;
import
org.aspectj.lang.JoinPoint
;
import
org.aspectj.lang.annotation.A
fter
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Pointcut
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.context.annotation.Configuration
;
import
org.w3c.dom.Attr
;
import
java.util.Arrays
;
import
java.util.UUID
;
/**
* Created by 11 on 2017/4/20.
...
...
@@ -30,27 +27,27 @@ public class ParamLogAdvice {
private
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
ParamLogAdvice
.
class
);
/**
* 定义切面,拦截InnerController类中的所有方法
*/
@Pointcut
(
"execution(* cn.quantgroup.xyqb.controller.external.user.InnerController.*(..))"
)
public
void
logPointCut
(){}
@Around
(
"logPointCut()"
)
public
Object
printMethodParam
(
ProceedingJoinPoint
pjp
)
{
Object
obj
=
null
;
try
{
obj
=
pjp
.
proceed
();
}
catch
(
Throwable
t
)
{
t
.
printStackTrace
();
}
Object
[]
params
=
pjp
.
getArgs
();
String
methodName
=
pjp
.
getSignature
().
getName
();
/**
* 后置通知,方法执行后获取方法的入参信息并打印日志
* @param jp
*/
@After
(
"logPointCut()"
)
public
void
printMethodParam
(
JoinPoint
jp
)
{
Object
[]
params
=
jp
.
getArgs
();
String
methodName
=
jp
.
getSignature
().
getName
();
String
[]
paramNames
=
null
;
try
{
paramNames
=
getFieldsName
(
pjp
.
getTarget
().
getClass
(),
p
jp
.
getTarget
().
getClass
().
getName
(),
methodName
);
paramNames
=
getFieldsName
(
jp
.
getTarget
().
getClass
(),
jp
.
getTarget
().
getClass
().
getName
(),
methodName
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
StringBuffer
buffer
=
new
StringBuffer
();
LOGGER
.
info
(
"invoke method:{}, method params:{}"
,
pjp
.
getSignature
().
getName
(),
Arrays
.
toString
(
params
));
if
(
null
!=
paramNames
)
{
for
(
int
i
=
0
;
i
<
paramNames
.
length
;
i
++)
{
buffer
.
append
(
paramNames
[
i
]).
append
(
":"
).
append
(
params
[
i
]).
append
(
" "
);
...
...
@@ -58,17 +55,27 @@ public class ParamLogAdvice {
String
logInfo
=
buffer
.
toString
().
substring
(
0
,
buffer
.
toString
().
length
()
-
1
);
LOGGER
.
info
(
"调用方法,方法名:{} , 方法参数:{}"
,
methodName
,
logInfo
);
}
return
obj
;
}
/**
* 利用javassist获取方法的参数名称
* @param cls
* @param clazzName
* @param methodName
* @return
* @throws NotFoundException
*/
private
static
String
[]
getFieldsName
(
Class
cls
,
String
clazzName
,
String
methodName
)
throws
NotFoundException
{
//获取class定义的容器
ClassPool
pool
=
ClassPool
.
getDefault
();
ClassClassPath
classPath
=
new
ClassClassPath
(
cls
);
pool
.
insertClassPath
(
classPath
);
//从容器中获取编译后的class
CtClass
cc
=
pool
.
get
(
clazzName
);
//反射获取指定方法
CtMethod
cm
=
cc
.
getDeclaredMethod
(
methodName
);
MethodInfo
methodInfo
=
cm
.
getMethodInfo
();
CodeAttribute
codeAttribute
=
methodInfo
.
getCodeAttribute
();
LocalVariableAttribute
attr
=
(
LocalVariableAttribute
)
codeAttribute
.
getAttribute
(
LocalVariableAttribute
.
tag
);
if
(
attr
==
null
)
{
...
...
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