Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
qq_41234584
spring-boot-demo
提交
d8935e69
S
spring-boot-demo
项目概览
qq_41234584
/
spring-boot-demo
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-boot-demo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
d8935e69
编写于
11月 17, 2020
作者:
cqmike0315
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
docs: 修改log-aop的README.md文档
上级
30017fa3
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
167 addition
and
72 deletion
+167
-72
demo-log-aop/README.md
demo-log-aop/README.md
+167
-72
未找到文件。
demo-log-aop/README.md
浏览文件 @
d8935e69
...
...
@@ -30,6 +30,11 @@
</properties>
<dependencies>
<dependency>
<groupId>
com.google.guava
</groupId>
<artifactId>
guava
</artifactId>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-web
</artifactId>
...
...
@@ -86,73 +91,149 @@
* </p>
*
* @author yangkai.shen
* @author chen qi
* @date Created in 2018-10-01 22:05
*/
@Aspect
@Component
@Slf4j
public
class
AopLog
{
private
static
final
String
START_TIME
=
"request-start"
;
/**
* 切入点
*/
@Pointcut
(
"execution(public * com.xkcoding.log.aop.controller.*Controller.*(..))"
)
public
void
log
()
{
}
/**
* 前置操作
*
* @param point 切入点
*/
@Before
(
"log()"
)
public
void
beforeLog
(
JoinPoint
point
)
{
ServletRequestAttributes
attributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
HttpServletRequest
request
=
Objects
.
requireNonNull
(
attributes
).
getRequest
();
log
.
info
(
"【请求 URL】:{}"
,
request
.
getRequestURL
());
log
.
info
(
"【请求 IP】:{}"
,
request
.
getRemoteAddr
());
log
.
info
(
"【请求类名】:{},【请求方法名】:{}"
,
point
.
getSignature
().
getDeclaringTypeName
(),
point
.
getSignature
().
getName
());
Map
<
String
,
String
[]>
parameterMap
=
request
.
getParameterMap
();
log
.
info
(
"【请求参数】:{},"
,
JSONUtil
.
toJsonStr
(
parameterMap
));
Long
start
=
System
.
currentTimeMillis
();
request
.
setAttribute
(
START_TIME
,
start
);
}
/**
* 环绕操作
*
* @param point 切入点
* @return 原方法返回值
* @throws Throwable 异常信息
*/
@Around
(
"log()"
)
public
Object
aroundLog
(
ProceedingJoinPoint
point
)
throws
Throwable
{
Object
result
=
point
.
proceed
();
log
.
info
(
"【返回值】:{}"
,
JSONUtil
.
toJsonStr
(
result
));
return
result
;
}
/**
* 后置操作
*/
@AfterReturning
(
"log()"
)
public
void
afterReturning
()
{
ServletRequestAttributes
attributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
HttpServletRequest
request
=
Objects
.
requireNonNull
(
attributes
).
getRequest
();
Long
start
=
(
Long
)
request
.
getAttribute
(
START_TIME
);
Long
end
=
System
.
currentTimeMillis
();
log
.
info
(
"【请求耗时】:{}毫秒"
,
end
-
start
);
String
header
=
request
.
getHeader
(
"User-Agent"
);
UserAgent
userAgent
=
UserAgent
.
parseUserAgentString
(
header
);
log
.
info
(
"【浏览器类型】:{},【操作系统】:{},【原始User-Agent】:{}"
,
userAgent
.
getBrowser
().
toString
(),
userAgent
.
getOperatingSystem
().
toString
(),
header
);
}
/**
* 切入点
*/
@Pointcut
(
"execution(public * com.xkcoding.log.aop.controller.*Controller.*(..))"
)
public
void
log
()
{
}
/**
* 环绕操作
*
* @param point 切入点
* @return 原方法返回值
* @throws Throwable 异常信息
*/
@Around
(
"log()"
)
public
Object
aroundLog
(
ProceedingJoinPoint
point
)
throws
Throwable
{
// 开始打印请求日志
ServletRequestAttributes
attributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
HttpServletRequest
request
=
Objects
.
requireNonNull
(
attributes
).
getRequest
();
// 打印请求相关参数
long
startTime
=
System
.
currentTimeMillis
();
Object
result
=
point
.
proceed
();
String
header
=
request
.
getHeader
(
"User-Agent"
);
UserAgent
userAgent
=
UserAgent
.
parseUserAgentString
(
header
);
final
Log
l
=
Log
.
builder
()
.
threadId
(
Long
.
toString
(
Thread
.
currentThread
().
getId
()))
.
threadName
(
Thread
.
currentThread
().
getName
())
.
ip
(
getIp
(
request
))
.
url
(
request
.
getRequestURL
().
toString
())
.
classMethod
(
String
.
format
(
"%s.%s"
,
point
.
getSignature
().
getDeclaringTypeName
(),
point
.
getSignature
().
getName
()))
.
httpMethod
(
request
.
getMethod
())
.
requestParams
(
getNameAndValue
(
point
))
.
result
(
result
)
.
timeCost
(
System
.
currentTimeMillis
()
-
startTime
)
.
userAgent
(
header
)
.
browser
(
userAgent
.
getBrowser
().
toString
())
.
os
(
userAgent
.
getOperatingSystem
().
toString
()).
build
();
log
.
info
(
"Request Log Info : {}"
,
JSONUtil
.
toJsonStr
(
l
));
return
result
;
}
/**
* 获取方法参数名和参数值
* @param joinPoint
* @return
*/
private
Map
<
String
,
Object
>
getNameAndValue
(
ProceedingJoinPoint
joinPoint
)
{
final
Signature
signature
=
joinPoint
.
getSignature
();
MethodSignature
methodSignature
=
(
MethodSignature
)
signature
;
final
String
[]
names
=
methodSignature
.
getParameterNames
();
final
Object
[]
args
=
joinPoint
.
getArgs
();
if
(
ArrayUtil
.
isEmpty
(
names
)
||
ArrayUtil
.
isEmpty
(
args
))
{
return
Collections
.
emptyMap
();
}
if
(
names
.
length
!=
args
.
length
)
{
log
.
warn
(
"{}方法参数名和参数值数量不一致"
,
methodSignature
.
getName
());
return
Collections
.
emptyMap
();
}
Map
<
String
,
Object
>
map
=
Maps
.
newHashMap
();
for
(
int
i
=
0
;
i
<
names
.
length
;
i
++)
{
map
.
put
(
names
[
i
],
args
[
i
]);
}
return
map
;
}
private
static
final
String
UNKNOWN
=
"unknown"
;
/**
* 获取ip地址
*/
public
static
String
getIp
(
HttpServletRequest
request
)
{
String
ip
=
request
.
getHeader
(
"x-forwarded-for"
);
if
(
ip
==
null
||
ip
.
length
()
==
0
||
UNKNOWN
.
equalsIgnoreCase
(
ip
))
{
ip
=
request
.
getHeader
(
"Proxy-Client-IP"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
UNKNOWN
.
equalsIgnoreCase
(
ip
))
{
ip
=
request
.
getHeader
(
"WL-Proxy-Client-IP"
);
}
if
(
ip
==
null
||
ip
.
length
()
==
0
||
UNKNOWN
.
equalsIgnoreCase
(
ip
))
{
ip
=
request
.
getRemoteAddr
();
}
String
comma
=
","
;
String
localhost
=
"127.0.0.1"
;
if
(
ip
.
contains
(
comma
))
{
ip
=
ip
.
split
(
","
)[
0
];
}
if
(
localhost
.
equals
(
ip
))
{
// 获取本机真正的ip地址
try
{
ip
=
InetAddress
.
getLocalHost
().
getHostAddress
();
}
catch
(
UnknownHostException
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
}
}
return
ip
;
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
static
class
Log
{
// 线程id
private
String
threadId
;
// 线程名称
private
String
threadName
;
// ip
private
String
ip
;
// url
private
String
url
;
// http方法 GET POST PUT DELETE PATCH
private
String
httpMethod
;
// 类方法
private
String
classMethod
;
// 请求参数
private
Object
requestParams
;
// 返回参数
private
Object
result
;
// 接口耗时
private
Long
timeCost
;
// 操作系统
private
String
os
;
// 浏览器
private
String
browser
;
// user-agent
private
String
userAgent
;
}
}
```
...
...
@@ -165,22 +246,36 @@ public class AopLog {
* </p>
*
* @author yangkai.shen
* @author chen qi
* @date Created in 2018-10-01 22:10
*/
@Slf4j
@RestController
public
class
TestController
{
/**
* 测试方法
*
* @param who 测试参数
* @return {@link Dict}
*/
@GetMapping
(
"/test"
)
public
Dict
test
(
String
who
)
{
return
Dict
.
create
().
set
(
"who"
,
StrUtil
.
isBlank
(
who
)
?
"me"
:
who
);
}
/**
* 测试方法
*
* @param who 测试参数
* @return {@link Dict}
*/
@GetMapping
(
"/test"
)
public
Dict
test
(
String
who
)
{
return
Dict
.
create
().
set
(
"who"
,
StrUtil
.
isBlank
(
who
)
?
"me"
:
who
);
}
/**
* 测试post json方法
* @param map 请求的json参数
* @return {@link Dict}
*/
@PostMapping
(
"/testJson"
)
public
Dict
testJson
(
@RequestBody
Map
<
String
,
Object
>
map
)
{
final
String
jsonStr
=
JSONUtil
.
toJsonStr
(
map
);
log
.
info
(
jsonStr
);
return
Dict
.
create
().
set
(
"json"
,
map
);
}
}
```
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录