Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
test-in-sleuth
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
仲光辉
test-in-sleuth
Commits
f08ada1e
Commit
f08ada1e
authored
Oct 19, 2020
by
仲光辉
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
尝试修复日志信息序列化(JSON)潜在的问题 2020/10/19 16:19
parent
b145c175
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
3 deletions
+23
-3
RequestLogAspect.java
...n/dankal/test/common/service/aspect/RequestLogAspect.java
+23
-3
No files found.
01-test-in-sleuth-common/01-02-service-util/src/main/java/cn/dankal/test/common/service/aspect/RequestLogAspect.java
View file @
f08ada1e
...
...
@@ -19,6 +19,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.Serializable
;
import
java.util.HashMap
;
import
java.util.Map
;
...
...
@@ -78,7 +79,16 @@ public class RequestLogAspect {
.
setRequestParams
(
getRequestParamsByProceedingJoinPoint
(
proceedingJoinPoint
))
.
setResult
(
result
)
.
setTimeCost
(
System
.
currentTimeMillis
()
-
startTime
);
// 参考 <<阿里巴巴Java开发手册>>(嵩山版) 日志输出不应直接使用 JSON 工具将其转化成 String see: 二 异常日志 ==> (三) 日志规约: 10
// 先谈谈为啥需要选择 序列化成 JSON String: 方便我们借助 JSON 工具更优雅的查看日志记录信息
// 为此 加入 try-catch 避免 日志序列化导致服务不可用
try
{
LOGGER
.
info
(
"Request-Info : {}"
,
JSON
.
toJSONString
(
requestInfo
));
}
catch
(
Exception
e
)
{
// 序列化失败
// 使用 toString 代替
LOGGER
.
info
(
"use toString method --> Request-Info : {}"
,
requestInfo
.
toString
());
}
return
result
;
}
...
...
@@ -106,7 +116,16 @@ public class RequestLogAspect {
joinPoint
.
getSignature
().
getName
()))
.
setRequestParams
(
getRequestParamsByJoinPoint
(
joinPoint
))
.
setThrowable
(
throwable
);
// 参考 <<阿里巴巴Java开发手册>>(嵩山版) 日志输出不应直接使用 JSON 工具将其转化成 String see: 二 异常日志 ==> (三) 日志规约: 10
// 先谈谈为啥需要选择 序列化成 JSON String: 方便我们借助 JSON 工具更优雅的查看日志记录信息
// 为此 加入 try-catch 避免 日志序列化导致服务不可用
try
{
LOGGER
.
error
(
"Error-Request-Info : {}"
,
JSON
.
toJSONString
(
requestErrorInfo
));
}
catch
(
Exception
e
)
{
// 序列化失败
// 使用 toString 代替
LOGGER
.
info
(
"use toString method --> Request-Info : {}"
,
requestErrorInfo
.
toString
());
}
}
/**
...
...
@@ -169,8 +188,8 @@ public class RequestLogAspect {
*/
@Data
@Accessors
(
chain
=
true
)
static
class
RequestInfo
{
static
class
RequestInfo
implements
Serializable
{
private
static
final
long
serialVersionUID
=
-
8109854803896597581L
;
/**
* 请求 ip
*/
...
...
@@ -207,7 +226,8 @@ public class RequestLogAspect {
*/
@Data
@Accessors
(
chain
=
true
)
static
class
RequestErrorInfo
{
static
class
RequestErrorInfo
implements
Serializable
{
private
static
final
long
serialVersionUID
=
8966927431771824888L
;
/**
* 请求 ip
*/
...
...
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