Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
file-service
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
Packages
Packages
Container Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
SuperHive
back-end
file-service
Commits
6f362df5
Commit
6f362df5
authored
May 26, 2020
by
huangcb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化日志输出
parent
9f1fe97d
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
159 additions
and
67 deletions
+159
-67
pom.xml
pom.xml
+6
-0
CommonConstants.java
...om/esv/freight/file/common/constants/CommonConstants.java
+69
-0
RestLogFilter.java
...ava/com/esv/freight/file/common/filter/RestLogFilter.java
+69
-59
ReqUtils.java
src/main/java/com/esv/freight/file/common/util/ReqUtils.java
+7
-6
BaseTestController.java
src/test/java/com/esv/freight/file/BaseTestController.java
+6
-0
UploadControllerTest.java
...ght/file/module/file/controller/UploadControllerTest.java
+2
-2
No files found.
pom.xml
View file @
6f362df5
...
@@ -36,6 +36,12 @@
...
@@ -36,6 +36,12 @@
<groupId>
org.springframework.boot
</groupId>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-test
</artifactId>
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
<scope>
test
</scope>
<exclusions>
<exclusion>
<groupId>
com.vaadin.external.google
</groupId>
<artifactId>
android-json
</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>
junit
</groupId>
<groupId>
junit
</groupId>
...
...
src/main/java/com/esv/freight/file/common/constants/CommonConstants.java
0 → 100644
View file @
6f362df5
package
com
.
esv
.
freight
.
file
.
common
.
constants
;
/**
* @description: 全局常量
* @project: freight-file-service
* @name: com.esv.freight.file.common.constants.CommonConstants
* @author: 黄朝斌
* @email: huangchaobin@esvtek.com
* @createTime: 2020/05/26 15:21
* @version:1.0
*/
public
class
CommonConstants
{
/**
* 访问端来源:1-浏览器端、2-Android端、3-iOS端、4-后台服务端
**/
public
static
final
String
REQ_SOURCE_TYPE_KEY
=
"Source-Type"
;
public
static
final
String
REQ_SOURCE_TYPE_WEB
=
"1"
;
public
static
final
String
REQ_SOURCE_TYPE_ANDROID
=
"2"
;
public
static
final
String
REQ_SOURCE_TYPE_IOS
=
"3"
;
public
static
final
String
REQ_SOURCE_TYPE_SERVICE
=
"4"
;
/**
* Feign调用返回参数
**/
public
static
final
String
FEIGN_RESULT_CODE
=
"code"
;
public
static
final
String
FEIGN_RESULT_MESSAGE
=
"message"
;
public
static
final
String
FEIGN_RESULT_DATA
=
"data"
;
public
static
final
int
FEIGN_RESULT_SUCCESS
=
200
;
/**
* 字符串"null"
**/
public
static
final
String
NULL_STRING
=
"null"
;
/**
* 字符串"unknown"
**/
public
static
final
String
UNKNOWN_STRING
=
"unknown"
;
/**
* log日志输出的最大长度及截取输出的长度
**/
public
static
final
int
LOG_MAX_LENGTH
=
5000
;
public
static
final
int
LOG_CUT_LENGTH
=
1000
;
/**
* Spring Boot Admin监控url前缀
**/
public
static
final
String
SPRING_BOOT_ADMIN_PREFIX_URL
=
"/actuator"
;
/**
* 默认字符编码
**/
public
static
final
String
DEFAULT_CHARACTER_ENCODING
=
"utf-8"
;
/**
* Http请求方式
**/
public
static
final
String
HTTP_REQUEST_METHOD_GET
=
"GET"
;
public
static
final
String
HTTP_REQUEST_METHOD_POST
=
"POST"
;
/**
* Http请求头
**/
public
static
final
String
HTTP_HEADER_X_FORWARDED_FOR
=
"x-forwarded-for"
;
public
static
final
String
HTTP_HEADER_PROXY_CLIENT_IP
=
"Proxy-Client-IP"
;
public
static
final
String
HTTP_HEADER_WL_PROXY_CLIENT_IP
=
"WL-Proxy-Client-IP"
;
}
src/main/java/com/esv/freight/file/common/filter/RestLogFilter.java
View file @
6f362df5
package
com
.
esv
.
freight
.
file
.
common
.
filter
;
package
com
.
esv
.
freight
.
file
.
common
.
filter
;
import
com.alibaba.fastjson.JSONObject
;
import
com.esv.freight.file.common.constants.CommonConstants
;
import
com.esv.freight.file.common.util.ReqUtils
;
import
com.esv.freight.file.common.wrapper.RestRequestWrapper
;
import
com.esv.freight.file.common.wrapper.RestRequestWrapper
;
import
com.esv.freight.file.common.wrapper.RestResponseWrapper
;
import
com.esv.freight.file.common.wrapper.RestResponseWrapper
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
javax.servlet.*
;
import
javax.servlet.*
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.BufferedReader
;
import
java.io.*
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.nio.charset.Charset
;
import
java.nio.charset.Charset
;
import
java.util.Enumeration
;
import
java.util.Enumeration
;
import
java.util.HashMap
;
import
java.util.HashMap
;
...
@@ -34,40 +33,31 @@ public class RestLogFilter implements Filter {
...
@@ -34,40 +33,31 @@ public class RestLogFilter implements Filter {
}
}
@Override
public
void
destroy
()
{
}
@Override
@Override
public
void
doFilter
(
ServletRequest
servletRequest
,
ServletResponse
servletResponse
,
FilterChain
filterChain
)
throws
IOException
,
ServletException
{
public
void
doFilter
(
ServletRequest
servletRequest
,
ServletResponse
servletResponse
,
FilterChain
filterChain
)
throws
IOException
,
ServletException
{
RestRequestWrapper
requestWrapper
=
new
RestRequestWrapper
((
HttpServletRequest
)
servletRequest
);
RestRequestWrapper
requestWrapper
=
new
RestRequestWrapper
((
HttpServletRequest
)
servletRequest
);
RestResponseWrapper
responseWrapper
=
new
RestResponseWrapper
((
HttpServletResponse
)
servletResponse
);
RestResponseWrapper
responseWrapper
=
new
RestResponseWrapper
((
HttpServletResponse
)
servletResponse
);
String
reqContentType
=
StringUtils
.
trimToEmpty
(
requestWrapper
.
getContentType
()).
toLowerCase
();
// 日志输出请求体
if
(
reqContentType
.
contains
(
"multipart/form-data"
))
{
this
.
logReq
(
requestWrapper
);
log
.
info
(
"multipart/form-data request"
);
}
else
{
// 日志输出请求
logReq
(
requestWrapper
);
}
filterChain
.
doFilter
(
requestWrapper
,
responseWrapper
);
String
resContentType
=
StringUtils
.
trimToEmpty
(
responseWrapper
.
getContentType
());
// 日志输出请求头
if
(
resContentType
.
contains
(
"text"
)
||
resContentType
.
contains
(
"xml"
)
this
.
logReqHeader
(
requestWrapper
);
||
resContentType
.
contains
(
"json"
))
{
// 日志输出返回
try
{
logRes
(
requestWrapper
,
responseWrapper
);
}
catch
(
Exception
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
}
}
else
{
log
.
info
(
"Response ContentType: {}"
,
resContentType
);
}
}
@Override
filterChain
.
doFilter
(
requestWrapper
,
responseWrapper
);
public
void
destroy
()
{
// 日志输出返回体
this
.
logRes
(
requestWrapper
,
responseWrapper
);
}
}
/**
* 获取请求返回体
**/
private
String
getPostBodyStr
(
HttpServletRequest
request
)
{
private
String
getPostBodyStr
(
HttpServletRequest
request
)
{
String
bodyStr
=
null
;
String
bodyStr
=
null
;
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
...
@@ -75,7 +65,7 @@ public class RestLogFilter implements Filter {
...
@@ -75,7 +65,7 @@ public class RestLogFilter implements Filter {
BufferedReader
reader
=
null
;
BufferedReader
reader
=
null
;
try
{
try
{
inputStream
=
request
.
getInputStream
();
inputStream
=
request
.
getInputStream
();
reader
=
new
BufferedReader
(
new
InputStreamReader
(
inputStream
,
Charset
.
forName
(
"UTF-8"
)));
reader
=
new
BufferedReader
(
new
InputStreamReader
(
inputStream
,
Charset
.
forName
(
CommonConstants
.
DEFAULT_CHARACTER_ENCODING
)));
String
line
=
""
;
String
line
=
""
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
while
((
line
=
reader
.
readLine
())
!=
null
)
{
sb
.
append
(
line
);
sb
.
append
(
line
);
...
@@ -114,52 +104,72 @@ public class RestLogFilter implements Filter {
...
@@ -114,52 +104,72 @@ public class RestLogFilter implements Filter {
return
bodyStr
;
return
bodyStr
;
}
}
/**
* 日志输出请求体
**/
private
void
logReq
(
RestRequestWrapper
requestWrapper
)
{
private
void
logReq
(
RestRequestWrapper
requestWrapper
)
{
String
url
=
requestWrapper
.
getRequestURI
();
String
url
=
requestWrapper
.
getRequestURI
();
String
method
=
requestWrapper
.
getMethod
();
String
method
=
requestWrapper
.
getMethod
();
if
(
"GET"
.
equalsIgnoreCase
(
method
))
{
String
reqBody
=
""
;
// 获取get、post请求体
if
(
CommonConstants
.
HTTP_REQUEST_METHOD_GET
.
equalsIgnoreCase
(
method
))
{
Enumeration
em
=
requestWrapper
.
getParameterNames
();
Enumeration
em
=
requestWrapper
.
getParameterNames
();
String
reqParams
=
""
;
while
(
em
.
hasMoreElements
())
{
while
(
em
.
hasMoreElements
())
{
String
k
=
em
.
nextElement
().
toString
();
String
k
=
em
.
nextElement
().
toString
();
String
v
=
requestWrapper
.
getParameter
(
k
);
String
v
=
requestWrapper
.
getParameter
(
k
);
reqParams
+=
"&"
+
k
+
"="
+
v
;
reqBody
+=
"&"
+
k
+
"="
+
v
;
}
reqParams
=
reqParams
.
replaceFirst
(
"&"
,
""
);
if
(
url
.
startsWith
(
"/actuator"
))
{
log
.
debug
(
"[IP={}]收到{}请求,url:{},params:{}"
,
getHttpClientIp
(
requestWrapper
),
method
,
url
,
reqParams
);
}
else
{
log
.
info
(
"[IP={}]收到{}请求,url:{},params:{}"
,
getHttpClientIp
(
requestWrapper
),
method
,
url
,
reqParams
);
}
}
}
else
if
(
"POST"
.
equalsIgnoreCase
(
method
))
{
reqBody
=
reqBody
.
replaceFirst
(
"&"
,
""
);
log
.
info
(
"[IP={}]收到{}请求,url:{},body:{}"
,
getHttpClientIp
(
requestWrapper
),
method
,
url
,
getPostBodyStr
(
requestWrapper
));
}
else
if
(
CommonConstants
.
HTTP_REQUEST_METHOD_POST
.
equalsIgnoreCase
(
method
))
{
}
else
{
reqBody
=
this
.
getPostBodyStr
(
requestWrapper
);
}
// 请求体日志截取
if
(
CommonConstants
.
LOG_MAX_LENGTH
<
reqBody
.
length
())
{
reqBody
=
reqBody
.
substring
(
0
,
CommonConstants
.
LOG_CUT_LENGTH
)
+
"……"
;
}
}
}
private
void
logRes
(
RestRequestWrapper
requestWrapper
,
RestResponseWrapper
responseWrapper
)
throws
Exception
{
// 日志输出请求体
byte
[]
bytes
=
responseWrapper
.
getBody
();
if
(
url
.
startsWith
(
CommonConstants
.
SPRING_BOOT_ADMIN_PREFIX_URL
))
{
String
resStr
=
new
String
(
bytes
,
"utf-8"
);
log
.
debug
(
"[IP={}]收到{}请求,url:{},params:{}"
,
ReqUtils
.
getHttpClientIp
(
requestWrapper
),
method
,
url
,
reqBody
);
String
url
=
requestWrapper
.
getRequestURI
();
if
(
url
.
startsWith
(
"/actuator"
))
{
log
.
debug
(
"请求响应:{}"
,
resStr
);
}
else
{
}
else
{
log
.
info
(
"
请求响应:{}"
,
resStr
);
log
.
info
(
"
[IP={}]收到{}请求,url:{},body:{}"
,
ReqUtils
.
getHttpClientIp
(
requestWrapper
),
method
,
url
,
reqBody
);
}
}
}
}
private
String
getHttpClientIp
(
HttpServletRequest
req
){
/**
String
ip
=
req
.
getHeader
(
"x-forwarded-for"
);
* 日志输出请求头
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
**/
ip
=
req
.
getHeader
(
"Proxy-Client-IP"
);
private
void
logReqHeader
(
RestRequestWrapper
request
)
{
JSONObject
headerJson
=
new
JSONObject
();
Enumeration
headerNames
=
request
.
getHeaderNames
();
while
(
headerNames
.
hasMoreElements
())
{
String
key
=
(
String
)
headerNames
.
nextElement
();
headerJson
.
put
(
key
,
request
.
getHeader
(
key
));
}
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
log
.
info
(
"请求头:{}"
,
headerJson
.
toJSONString
());
ip
=
req
.
getHeader
(
"WL-Proxy-Client-IP"
);
}
/**
* 日志输出返回体
**/
private
void
logRes
(
RestRequestWrapper
requestWrapper
,
RestResponseWrapper
responseWrapper
)
{
byte
[]
bytes
=
responseWrapper
.
getBody
();
String
resBody
=
null
;
try
{
resBody
=
new
String
(
bytes
,
CommonConstants
.
DEFAULT_CHARACTER_ENCODING
);
}
catch
(
UnsupportedEncodingException
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
}
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
String
url
=
requestWrapper
.
getRequestURI
();
ip
=
req
.
getRemoteAddr
();
if
(
CommonConstants
.
LOG_MAX_LENGTH
<
resBody
.
length
())
{
resBody
=
resBody
.
substring
(
0
,
CommonConstants
.
LOG_CUT_LENGTH
)
+
"……"
;
}
if
(
url
.
startsWith
(
CommonConstants
.
SPRING_BOOT_ADMIN_PREFIX_URL
))
{
log
.
debug
(
"请求响应:{}"
,
resBody
);
}
else
{
log
.
info
(
"请求响应:{}"
,
resBody
);
}
}
return
ip
;
}
}
}
}
\ No newline at end of file
src/main/java/com/esv/freight/file/common/util/ReqUtils.java
View file @
6f362df5
package
com
.
esv
.
freight
.
file
.
common
.
util
;
package
com
.
esv
.
freight
.
file
.
common
.
util
;
import
com.esv.freight.file.common.constants.CommonConstants
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
...
@@ -26,14 +27,14 @@ public class ReqUtils {
...
@@ -26,14 +27,14 @@ public class ReqUtils {
* @return
* @return
*/
*/
public
static
String
getHttpClientIp
(
HttpServletRequest
req
){
public
static
String
getHttpClientIp
(
HttpServletRequest
req
){
String
ip
=
req
.
getHeader
(
"x-forwarded-for"
);
String
ip
=
req
.
getHeader
(
CommonConstants
.
HTTP_HEADER_X_FORWARDED_FOR
);
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
if
(
ip
==
null
||
ip
.
length
()
==
0
||
CommonConstants
.
UNKNOWN_STRING
.
equalsIgnoreCase
(
ip
))
{
ip
=
req
.
getHeader
(
"Proxy-Client-IP"
);
ip
=
req
.
getHeader
(
CommonConstants
.
HTTP_HEADER_PROXY_CLIENT_IP
);
}
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
if
(
ip
==
null
||
ip
.
length
()
==
0
||
CommonConstants
.
UNKNOWN_STRING
.
equalsIgnoreCase
(
ip
))
{
ip
=
req
.
getHeader
(
"WL-Proxy-Client-IP"
);
ip
=
req
.
getHeader
(
CommonConstants
.
HTTP_HEADER_WL_PROXY_CLIENT_IP
);
}
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
"unknown"
.
equalsIgnoreCase
(
ip
))
{
if
(
ip
==
null
||
ip
.
length
()
==
0
||
CommonConstants
.
UNKNOWN_STRING
.
equalsIgnoreCase
(
ip
))
{
ip
=
req
.
getRemoteAddr
();
ip
=
req
.
getRemoteAddr
();
}
}
return
ip
;
return
ip
;
...
...
src/test/java/com/esv/freight/file/BaseTestController.java
View file @
6f362df5
...
@@ -27,14 +27,20 @@ public class BaseTestController {
...
@@ -27,14 +27,20 @@ public class BaseTestController {
MockMvc
mockMvc
;
MockMvc
mockMvc
;
private
static
Long
TEST_START_TIME
;
private
static
Long
TEST_END_TIME
;
@Before
@Before
public
void
before
()
{
public
void
before
()
{
log
.
info
(
"=================================== Test Start ==================================="
);
log
.
info
(
"=================================== Test Start ==================================="
);
mockMvc
=
MockMvcBuilders
.
webAppContextSetup
(
webApplicationContext
).
build
();
mockMvc
=
MockMvcBuilders
.
webAppContextSetup
(
webApplicationContext
).
build
();
TEST_START_TIME
=
System
.
currentTimeMillis
();
}
}
@After
@After
public
void
after
()
{
public
void
after
()
{
TEST_END_TIME
=
System
.
currentTimeMillis
();
log
.
info
(
"Test耗时:"
+
(
TEST_END_TIME
-
TEST_START_TIME
)
+
"毫秒"
);
log
.
info
(
"=================================== Test End ==================================="
);
log
.
info
(
"=================================== Test End ==================================="
);
}
}
...
...
src/test/java/com/esv/freight/file/module/file/controller/UploadControllerTest.java
View file @
6f362df5
...
@@ -106,12 +106,12 @@ public class UploadControllerTest extends BaseTestController {
...
@@ -106,12 +106,12 @@ public class UploadControllerTest extends BaseTestController {
JSONObject
reqJson
=
new
JSONObject
();
JSONObject
reqJson
=
new
JSONObject
();
// 构造数据
// 构造数据
String
filepath
=
"D:\\test\\线上合同-
货主
.docx"
;
String
filepath
=
"D:\\test\\线上合同-
司机
.docx"
;
File
file
=
new
File
(
filepath
);
File
file
=
new
File
(
filepath
);
InputStream
input
=
new
FileInputStream
(
file
);
InputStream
input
=
new
FileInputStream
(
file
);
reqJson
.
put
(
"fileType"
,
"word"
);
reqJson
.
put
(
"fileType"
,
"word"
);
reqJson
.
put
(
"fileData"
,
Base64
.
getEncoder
().
encodeToString
(
IOUtils
.
toByteArray
(
input
)));
reqJson
.
put
(
"fileData"
,
Base64
.
getEncoder
().
encodeToString
(
IOUtils
.
toByteArray
(
input
)));
reqJson
.
put
(
"fileName"
,
"线上合同-
货主
"
);
reqJson
.
put
(
"fileName"
,
"线上合同-
司机
"
);
MvcResult
mvcResult
=
this
.
getMockMvc
().
perform
(
MockMvcRequestBuilders
.
post
(
url
)
MvcResult
mvcResult
=
this
.
getMockMvc
().
perform
(
MockMvcRequestBuilders
.
post
(
url
)
.
contentType
(
MediaType
.
APPLICATION_JSON_UTF8_VALUE
)
.
contentType
(
MediaType
.
APPLICATION_JSON_UTF8_VALUE
)
...
...
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