Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
open-source
/
glsx-smart-gateway-server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit baef1512
authored
Jun 15, 2021
by
zhouhaibao
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
[+]智能网关添加时间打印
1 parent
dfd1ae78
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
9 deletions
src/main/java/com/glsx/plat/gateway/filter/LoggingGlobalFilter.java
src/main/java/com/glsx/plat/gateway/filter/LoggingGlobalFilter.java
View file @
baef151
package
com
.
glsx
.
plat
.
gateway
.
filter
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.cloud.gateway.filter.GatewayFilterChain
;
import
org.springframework.cloud.gateway.filter.GlobalFilter
;
import
org.springframework.core.Ordered
;
import
org.springframework.core.io.buffer.DataBuffer
;
import
org.springframework.http.server.reactive.ServerHttpRequestDecorator
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.server.ServerWebExchange
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
@Component
@Slf4j
public
class
LoggingGlobalFilter
implements
GlobalFilter
,
Ordered
{
@Override
public
Mono
<
Void
>
filter
(
ServerWebExchange
exchange
,
GatewayFilterChain
chain
)
{
return
chain
.
filter
(
exchange
.
mutate
().
request
(
new
ServerHttpRequestDecorator
(
exchange
.
getRequest
())
{
@Override
public
Flux
<
DataBuffer
>
getBody
()
{
//body开启日志,记录操作body的filter
return
exchange
.
getRequest
().
getBody
().
log
();
String
info
=
String
.
format
(
"Method:{%s} Host:{%s} Path:{%s} Query:{%s}"
,
exchange
.
getRequest
().
getMethod
().
name
(),
exchange
.
getRequest
().
getURI
().
getHost
(),
exchange
.
getRequest
().
getURI
().
getPath
(),
exchange
.
getRequest
().
getQueryParams
());
log
.
info
(
info
);
exchange
.
getAttributes
().
put
(
"startTime"
,
System
.
currentTimeMillis
());
return
chain
.
filter
(
exchange
).
then
(
Mono
.
fromRunnable
(()
->
{
Long
startTime
=
exchange
.
getAttribute
(
"startTime"
);
if
(
startTime
!=
null
)
{
Long
executeTime
=
(
System
.
currentTimeMillis
()
-
startTime
);
log
.
info
(
exchange
.
getRequest
().
getURI
().
getRawPath
()
+
" : "
+
executeTime
+
"ms"
);
}
}).
build
());
}));
}
@Override
...
...
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