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
67b64bff
Commit
67b64bff
authored
Apr 20, 2017
by
lee_mingzhu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加日志切面,拦截打印所有InnerApiController类中的所有方法的参数
parent
86bb1bac
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
84 additions
and
0 deletions
+84
-0
ParamLogAdvice.java
...in/java/cn/quantgroup/xyqb/aspect/log/ParamLogAdvice.java
+84
-0
No files found.
src/main/java/cn/quantgroup/xyqb/aspect/log/ParamLogAdvice.java
0 → 100644
View file @
67b64bff
package
cn
.
quantgroup
.
xyqb
.
aspect
.
log
;
import
javassist.ClassClassPath
;
import
javassist.ClassPool
;
import
javassist.CtClass
;
import
javassist.CtMethod
;
import
javassist.Modifier
;
import
javassist.NotFoundException
;
import
javassist.bytecode.CodeAttribute
;
import
javassist.bytecode.LocalVariableAttribute
;
import
javassist.bytecode.MethodInfo
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.Around
;
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.
*/
@Aspect
@Configuration
public
class
ParamLogAdvice
{
private
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
ParamLogAdvice
.
class
);
@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
();
String
[]
paramNames
=
null
;
try
{
paramNames
=
getFieldsName
(
pjp
.
getTarget
().
getClass
(),
pjp
.
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
(
" "
);
}
String
logInfo
=
buffer
.
toString
().
substring
(
0
,
buffer
.
toString
().
length
()
-
1
);
LOGGER
.
info
(
"调用方法,方法名:{} , 方法参数:{}"
,
methodName
,
logInfo
);
}
return
obj
;
}
private
static
String
[]
getFieldsName
(
Class
cls
,
String
clazzName
,
String
methodName
)
throws
NotFoundException
{
ClassPool
pool
=
ClassPool
.
getDefault
();
ClassClassPath
classPath
=
new
ClassClassPath
(
cls
);
pool
.
insertClassPath
(
classPath
);
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
)
{
return
null
;
}
String
[]
paramNames
=
new
String
[
cm
.
getParameterTypes
().
length
];
int
pos
=
Modifier
.
isStatic
(
cm
.
getModifiers
())
?
0
:
1
;
for
(
int
i
=
0
;
i
<
paramNames
.
length
;
i
++){
paramNames
[
i
]
=
attr
.
variableName
(
i
+
pos
);
}
return
paramNames
;
}
}
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