Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
C
communication
Project
Project
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
耿迪迪
communication
Commits
c4b230cc
Commit
c4b230cc
authored
Jun 30, 2021
by
耿迪迪
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
日志写入本地文件 gengdidi
parent
458fd9fc
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
4 deletions
+50
-4
LogInterceptor.java
...hong/communication/common/interceptor/LogInterceptor.java
+45
-3
application.yml
src/main/resources/application.yml
+5
-1
No files found.
src/main/java/com/zehong/communication/common/interceptor/LogInterceptor.java
View file @
c4b230cc
...
@@ -20,6 +20,8 @@ import org.springframework.web.context.request.RequestContextHolder;
...
@@ -20,6 +20,8 @@ import org.springframework.web.context.request.RequestContextHolder;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.text.SimpleDateFormat
;
import
java.text.SimpleDateFormat
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.Date
;
import
java.util.Date
;
...
@@ -38,6 +40,12 @@ public class LogInterceptor {
...
@@ -38,6 +40,12 @@ public class LogInterceptor {
@Value
(
"${secretKey}"
)
@Value
(
"${secretKey}"
)
private
String
secretKey
;
private
String
secretKey
;
@Value
(
"${isWriteLocal}"
)
private
boolean
isWriteLocal
;
@Value
(
"${fileUrl}"
)
private
String
fileUrl
;
/**
/**
* 定义拦截器规则
* 定义拦截器规则
*/
*/
...
@@ -75,13 +83,25 @@ public class LogInterceptor {
...
@@ -75,13 +83,25 @@ public class LogInterceptor {
}
}
/**
/**
* 记录请求日志
* 根据配置记录请求日志
* @param params 请求参数
* @param result 请求结果
*/
private
void
insertRequestLogRecord
(
String
params
,
String
result
)
{
if
(
isWriteLocal
){
writeToLocalFile
(
params
,
result
);
}
else
{
writeToData
(
params
,
result
);
}
}
/**
* 将请求日志写入数据库
* @param params 请求参数
* @param params 请求参数
* @param result 请求结果
* @param result 请求结果
*/
*/
private
void
insertRequestLogRecord
(
String
params
,
String
result
){
private
void
writeToData
(
String
params
,
String
result
){
HttpServletRequest
request
=
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
()).
getRequest
();
HttpServletRequest
request
=
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
()).
getRequest
();
request
.
getParameter
(
"secretKey"
);
RequestLog
requestLog
=
new
RequestLog
();
RequestLog
requestLog
=
new
RequestLog
();
requestLog
.
setId
(
UUID
.
randomUUID
().
toString
().
replaceAll
(
"-"
,
""
));
requestLog
.
setId
(
UUID
.
randomUUID
().
toString
().
replaceAll
(
"-"
,
""
));
requestLog
.
setRequestMapping
(
request
.
getRequestURL
().
toString
());
requestLog
.
setRequestMapping
(
request
.
getRequestURL
().
toString
());
...
@@ -91,4 +111,26 @@ public class LogInterceptor {
...
@@ -91,4 +111,26 @@ public class LogInterceptor {
requestLog
.
setResult
(
JSONObject
.
toJSONString
(
result
));
requestLog
.
setResult
(
JSONObject
.
toJSONString
(
result
));
requestLogService
.
insert
(
requestLog
);
requestLogService
.
insert
(
requestLog
);
}
}
/**
* 将请求日志写入本地文件
* @param params 请求参数
* @param result 请求结果
*/
private
void
writeToLocalFile
(
String
params
,
String
result
)
{
try
{
HttpServletRequest
request
=
((
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
()).
getRequest
();
FileWriter
writer
=
new
FileWriter
(
fileUrl
,
true
);
writer
.
write
(
"\n time:"
+
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
).
format
(
new
Date
()));
writer
.
write
(
" requestMapping:"
+
request
.
getRequestURL
().
toString
());
writer
.
write
(
" params:"
+
params
);
writer
.
write
(
" postTime:"
+
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
).
format
(
new
Date
()));
writer
.
write
(
" result:"
+
JSONObject
.
toJSONString
(
result
));
writer
.
flush
();
writer
.
close
();
}
catch
(
IOException
e
)
{
logger
.
error
(
"写入本地文件出错:"
+
e
);
}
}
}
}
src/main/resources/application.yml
View file @
c4b230cc
...
@@ -48,3 +48,7 @@ iPWhite: 192.168.2.23
...
@@ -48,3 +48,7 @@ iPWhite: 192.168.2.23
#秘钥
#秘钥
secretKey
:
f225e66813e6d6a663daf919c1935dcb
secretKey
:
f225e66813e6d6a663daf919c1935dcb
#是否写入本地
isWriteLocal
:
false
fileUrl
:
E:\java\work\requireLog.text
\ No newline at end of file
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