Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
ec716f99
Commit
ec716f99
authored
Jun 03, 2019
by
zhengjie
Browse files
优化 druid配置,日志异步保存
parent
3696c2fb
Changes
5
Hide whitespace changes
Inline
Side-by-side
eladmin-logging/src/main/java/me/zhengjie/aspect/LogAspect.java
View file @
ec716f99
...
...
@@ -4,6 +4,8 @@ import lombok.extern.slf4j.Slf4j;
import
me.zhengjie.domain.Log
;
import
me.zhengjie.exception.BadRequestException
;
import
me.zhengjie.service.LogService
;
import
me.zhengjie.utils.RequestHolder
;
import
me.zhengjie.utils.SecurityUtils
;
import
me.zhengjie.utils.ThrowableUtil
;
import
org.aspectj.lang.JoinPoint
;
import
org.aspectj.lang.ProceedingJoinPoint
;
...
...
@@ -14,6 +16,8 @@ import org.aspectj.lang.annotation.Pointcut;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
javax.servlet.http.HttpServletRequest
;
/**
* @author jie
* @date 2018-11-24
...
...
@@ -47,7 +51,7 @@ public class LogAspect {
currentTime
=
System
.
currentTimeMillis
();
result
=
joinPoint
.
proceed
();
Log
log
=
new
Log
(
"INFO"
,
System
.
currentTimeMillis
()
-
currentTime
);
logService
.
save
(
joinPoint
,
log
);
logService
.
save
(
getUsername
(),
RequestHolder
.
getHttpServletRequest
(),
joinPoint
,
log
);
return
result
;
}
...
...
@@ -61,6 +65,14 @@ public class LogAspect {
public
void
logAfterThrowing
(
JoinPoint
joinPoint
,
Throwable
e
)
{
Log
log
=
new
Log
(
"ERROR"
,
System
.
currentTimeMillis
()
-
currentTime
);
log
.
setExceptionDetail
(
ThrowableUtil
.
getStackTrace
(
e
));
logService
.
save
((
ProceedingJoinPoint
)
joinPoint
,
log
);
logService
.
save
(
getUsername
(),
RequestHolder
.
getHttpServletRequest
(),
(
ProceedingJoinPoint
)
joinPoint
,
log
);
}
public
String
getUsername
()
{
try
{
return
SecurityUtils
.
getUsername
();
}
catch
(
Exception
e
){
return
""
;
}
}
}
eladmin-logging/src/main/java/me/zhengjie/service/LogService.java
View file @
ec716f99
...
...
@@ -4,6 +4,8 @@ import me.zhengjie.domain.Log;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.springframework.scheduling.annotation.Async
;
import
javax.servlet.http.HttpServletRequest
;
/**
* @author jie
* @date 2018-11-24
...
...
@@ -16,7 +18,7 @@ public interface LogService {
* @param log
*/
@Async
void
save
(
ProceedingJoinPoint
joinPoint
,
Log
log
);
void
save
(
String
username
,
HttpServletRequest
request
,
ProceedingJoinPoint
joinPoint
,
Log
log
);
/**
* 查询异常详情
...
...
eladmin-logging/src/main/java/me/zhengjie/service/impl/LogServiceImpl.java
View file @
ec716f99
...
...
@@ -5,7 +5,6 @@ import cn.hutool.json.JSONObject;
import
me.zhengjie.domain.Log
;
import
me.zhengjie.repository.LogRepository
;
import
me.zhengjie.service.LogService
;
import
me.zhengjie.utils.RequestHolder
;
import
me.zhengjie.utils.SecurityUtils
;
import
me.zhengjie.utils.StringUtils
;
import
org.aspectj.lang.ProceedingJoinPoint
;
...
...
@@ -32,10 +31,8 @@ public class LogServiceImpl implements LogService {
@Override
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
void
save
(
ProceedingJoinPoint
joinPoint
,
Log
log
){
public
void
save
(
String
username
,
HttpServletRequest
request
,
ProceedingJoinPoint
joinPoint
,
Log
log
){
// 获取request
HttpServletRequest
request
=
RequestHolder
.
getHttpServletRequest
();
MethodSignature
signature
=
(
MethodSignature
)
joinPoint
.
getSignature
();
Method
method
=
signature
.
getMethod
();
me
.
zhengjie
.
aop
.
log
.
Log
aopLog
=
method
.
getAnnotation
(
me
.
zhengjie
.
aop
.
log
.
Log
.
class
);
...
...
@@ -53,9 +50,6 @@ public class LogServiceImpl implements LogService {
Object
[]
argValues
=
joinPoint
.
getArgs
();
//参数名称
String
[]
argNames
=
((
MethodSignature
)
joinPoint
.
getSignature
()).
getParameterNames
();
// 用户名
String
username
=
""
;
if
(
argValues
!=
null
){
for
(
int
i
=
0
;
i
<
argValues
.
length
;
i
++)
{
params
+=
" "
+
argNames
[
i
]
+
": "
+
argValues
[
i
];
...
...
@@ -65,9 +59,7 @@ public class LogServiceImpl implements LogService {
// 获取IP地址
log
.
setRequestIp
(
StringUtils
.
getIP
(
request
));
if
(!
LOGINPATH
.
equals
(
signature
.
getName
())){
username
=
SecurityUtils
.
getUsername
();
}
else
{
if
(
LOGINPATH
.
equals
(
signature
.
getName
())){
try
{
JSONObject
jsonObject
=
new
JSONObject
(
argValues
[
0
]);
username
=
jsonObject
.
get
(
"username"
).
toString
();
...
...
eladmin-system/src/main/resources/config/application-dev.yml
View file @
ec716f99
...
...
@@ -23,6 +23,8 @@ spring:
test-while-idle
:
true
test-on-borrow
:
false
test-on-return
:
false
validation-query
:
select
1
# 配置监控统计拦截的filters
filters
:
stat
stat-view-servlet
:
...
...
eladmin-system/src/main/resources/config/application-prod.yml
View file @
ec716f99
...
...
@@ -23,6 +23,7 @@ spring:
test-while-idle
:
true
test-on-borrow
:
false
test-on-return
:
false
validation-query
:
select
1
# 配置监控统计拦截的filters
filters
:
stat
...
...
Write
Preview
Supports
Markdown
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