Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
yangxiujun
/
paidan_demo
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit a8da2b3a
authored
Jul 06, 2023
by
张晓
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add interceptor ,to show more log
1 parent
cb2bca4d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
83 additions
and
0 deletions
project-dispatch/src/main/java/com/dituhui/pea/dispatch/interceptor/RequestInterceptor.java
project-dispatch/src/main/java/com/dituhui/pea/dispatch/interceptor/RequestInterceptor.java
0 → 100644
View file @
a8da2b3
package
com
.
dituhui
.
pea
.
dispatch
.
interceptor
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
javax.servlet.http.HttpServletRequest
;
import
lombok.extern.slf4j.Slf4j
;
import
org.aspectj.lang.JoinPoint
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.*
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
org.springframework.web.servlet.HandlerInterceptor
;
@Slf4j
@Aspect
@Component
public
class
RequestInterceptor
implements
HandlerInterceptor
{
/**
* 以 controller 包下定义的所有请求为切入点
*/
@Pointcut
(
value
=
"execution(public * com.dituhui.pea.dispatch.controller..*.*(..))"
)
public
void
reqOpenAPILog
()
{
}
/**
* 在切点之前织入
*
* @param joinPoint
* @throws Throwable
*/
@Before
(
"reqOpenAPILog()"
)
public
void
doBefore
(
JoinPoint
joinPoint
)
throws
Throwable
{
// 开始打印请求日志
ServletRequestAttributes
attributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
HttpServletRequest
request
=
attributes
.
getRequest
();
// 打印请求 url
log
.
info
(
"Request URL : {}"
,
request
.
getRequestURL
().
toString
());
// 打印 Http method
log
.
info
(
"HTTP Method : {}"
,
request
.
getMethod
());
// 打印调用 controller 的全路径以及执行方法
log
.
info
(
"Class Method : {}.{}"
,
joinPoint
.
getSignature
().
getDeclaringTypeName
(),
joinPoint
.
getSignature
().
getName
());
// 打印请求的 IP
log
.
info
(
"Request IP : {}"
,
request
.
getRemoteAddr
());
// 打印请求入参
log
.
info
(
"Request Args : {}"
,
new
ObjectMapper
().
writeValueAsString
(
joinPoint
.
getArgs
()));
}
/**
* 在切点之后织入
*
* @throws Throwable
*/
@After
(
"reqOpenAPILog()"
)
public
void
doAfter
()
throws
Throwable
{
// TODO
}
/**
* 环绕
*
* @param proceedingJoinPoint
* @return
* @throws Throwable
*/
@Around
(
"reqOpenAPILog()"
)
public
Object
doAround
(
ProceedingJoinPoint
proceedingJoinPoint
)
throws
Throwable
{
log
.
info
(
"========================================== Start =========================================="
);
long
startTime
=
System
.
currentTimeMillis
();
Object
result
=
proceedingJoinPoint
.
proceed
();
// 打印出参
// log.info("Response Args : {}", result);
log
.
info
(
"Response Args : {}"
,
new
ObjectMapper
().
writeValueAsString
(
result
));
// 执行耗时
log
.
info
(
"Time-Consuming : {} ms"
,
System
.
currentTimeMillis
()
-
startTime
);
log
.
info
(
"=========================================== End ==========================================="
);
return
result
;
}
}
Write
Preview
Markdown
is supported
Attach a file
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 post a comment