Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Q
qa-platform
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
QA
qa-platform
Commits
1942b475
Commit
1942b475
authored
Mar 01, 2021
by
黎博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改日志切面
parent
cd469d5a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
44 additions
and
6 deletions
+44
-6
HttpLogAspect.java
src/main/java/cn/qg/qaplatform/aspect/HttpLogAspect.java
+44
-6
No files found.
src/main/java/cn/qg/qaplatform/aspect/HttpLogAspect.java
View file @
1942b475
...
@@ -2,6 +2,7 @@ package cn.qg.qaplatform.aspect;
...
@@ -2,6 +2,7 @@ package cn.qg.qaplatform.aspect;
import
cn.qg.qaplatform.utils.JsonTransUtils
;
import
cn.qg.qaplatform.utils.JsonTransUtils
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
org.apache.commons.io.IOUtils
;
import
org.aspectj.lang.JoinPoint
;
import
org.aspectj.lang.JoinPoint
;
import
org.aspectj.lang.annotation.AfterReturning
;
import
org.aspectj.lang.annotation.AfterReturning
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Aspect
;
...
@@ -18,8 +19,10 @@ import org.springframework.web.context.request.ServletRequestAttributes;
...
@@ -18,8 +19,10 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
javax.servlet.http.HttpServletResponse
;
import
java.io.IOException
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Parameter
;
import
java.lang.reflect.Parameter
;
import
java.nio.charset.Charset
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
...
@@ -33,7 +36,7 @@ import java.util.Map;
...
@@ -33,7 +36,7 @@ import java.util.Map;
public
class
HttpLogAspect
{
public
class
HttpLogAspect
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
HttpLogAspect
.
class
);
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
HttpLogAspect
.
class
);
@Pointcut
(
"execution(public * cn.qg.qaplatform.
zdata
.controller..*.*(..))"
)
@Pointcut
(
"execution(public * cn.qg.qaplatform.
*
.controller..*.*(..))"
)
public
void
httpRequestLog
()
{}
public
void
httpRequestLog
()
{}
/**
/**
...
@@ -46,11 +49,12 @@ public class HttpLogAspect {
...
@@ -46,11 +49,12 @@ public class HttpLogAspect {
public
void
doBefore
(
JoinPoint
joinPoint
)
throws
JsonProcessingException
{
public
void
doBefore
(
JoinPoint
joinPoint
)
throws
JsonProcessingException
{
ServletRequestAttributes
attributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
ServletRequestAttributes
attributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
HttpServletRequest
request
=
attributes
.
getRequest
();
HttpServletRequest
request
=
attributes
.
getRequest
();
logger
.
info
(
"url:{}"
,
request
.
getRequestURL
());
// logger.info("url:{}", request.getRequestURL());
logger
.
info
(
"method:{}"
,
request
.
getMethod
());
// logger.info("method:{}", request.getMethod());
logger
.
info
(
"ip:{}"
,
request
.
getRemoteAddr
());
// logger.info("ip:{}", request.getRemoteAddr());
logger
.
info
(
"class_method={}"
,
joinPoint
.
getSignature
().
getDeclaringTypeName
()
+
"."
+
joinPoint
.
getSignature
().
getName
());
// logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
logger
.
info
(
"args:{}"
,
joinPoint
.
getArgs
());
// logger.info("args:{}", joinPoint.getArgs());
logger
.
info
(
"请求发起ip地址: {}, 请求url: {}, 请求参数:{}"
,
request
.
getRemoteAddr
(),
request
.
getRequestURI
(),
getQuery
(
request
));
}
}
/**
/**
...
@@ -98,4 +102,38 @@ public class HttpLogAspect {
...
@@ -98,4 +102,38 @@ public class HttpLogAspect {
return
argList
;
return
argList
;
}
}
}
}
private
String
getQuery
(
HttpServletRequest
request
)
{
if
(
isPostPutRequest
(
request
))
{
return
getParameters
(
request
)
+
getRequestBody
(
request
);
}
else
{
return
getParameters
(
request
);
}
}
private
boolean
isPostPutRequest
(
HttpServletRequest
request
)
{
return
"POST"
.
equalsIgnoreCase
(
request
.
getMethod
())
||
"PUT"
.
equalsIgnoreCase
(
request
.
getMethod
());
}
private
String
getParameters
(
HttpServletRequest
request
)
{
StringBuilder
sb
=
new
StringBuilder
();
Map
<
String
,
String
[]>
params
=
request
.
getParameterMap
();
for
(
String
key
:
params
.
keySet
())
{
sb
.
append
(
'['
)
.
append
(
key
)
.
append
(
'='
)
.
append
(
request
.
getParameter
(
key
))
.
append
(
"] "
);
}
return
sb
.
toString
();
}
private
String
getRequestBody
(
HttpServletRequest
request
)
{
try
{
return
IOUtils
.
toString
(
request
.
getInputStream
(),
String
.
valueOf
(
Charset
.
defaultCharset
()));
}
catch
(
IOException
e
)
{
return
""
;
}
}
}
}
\ 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