Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
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
YZG
yzg-util
Commits
4fb19acc
Commit
4fb19acc
authored
Mar 18, 2025
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复异常提醒,从而正确的跟踪异常信息
parent
6ed0ffd1
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
163 additions
and
76 deletions
+163
-76
AspectFeign.java
.../src/main/java/com/yanzuoguang/cloud/aop/AspectFeign.java
+9
-4
AspectLogUrl.java
...src/main/java/com/yanzuoguang/cloud/aop/AspectLogUrl.java
+125
-50
AspectWeb.java
...ud/src/main/java/com/yanzuoguang/cloud/aop/AspectWeb.java
+13
-5
AspectLog.java
yzg-util-db/src/main/java/com/yanzuoguang/log/AspectLog.java
+5
-12
LogString.java
yzg-util-db/src/main/java/com/yanzuoguang/log/LogString.java
+11
-5
No files found.
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/AspectFeign.java
View file @
4fb19acc
...
...
@@ -2,6 +2,7 @@ package com.yanzuoguang.cloud.aop;
import
com.yanzuoguang.log.AspectLog
;
import
com.yanzuoguang.log.LogInfoVo
;
import
com.yanzuoguang.log.LogString
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
import
com.yanzuoguang.util.vo.ResponseResult
;
...
...
@@ -25,13 +26,16 @@ public class AspectFeign {
private
final
AspectLogUrl
aspectLogUrl
;
private
final
AspectLogBody
aspectLogBody
;
private
final
AspectLog
aspectLog
;
private
final
LogString
logString
;
public
AspectFeign
(
AspectLogUrl
aspectLogUrl
,
AspectLogBody
aspectLogBody
,
AspectLog
aspectLog
)
{
AspectLog
aspectLog
,
LogString
logString
)
{
this
.
aspectLogUrl
=
aspectLogUrl
;
this
.
aspectLogBody
=
aspectLogBody
;
this
.
aspectLog
=
aspectLog
;
this
.
logString
=
logString
;
}
/**
...
...
@@ -50,11 +54,12 @@ public class AspectFeign {
@Around
(
value
=
"feignAspect()"
)
public
Object
requestFeignAround
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
Class
<?>
declaringType
=
joinPoint
.
getSignature
().
getDeclaringType
();
String
url
=
aspectLogUrl
.
getWebMethod
Url
(
joinPoint
);
AspectLogUrl
.
WebUrlInfo
webMethodUrl
=
aspectLogUrl
.
getWebMethodBase
Url
(
joinPoint
);
Object
requestBody
=
aspectLogBody
.
getRequestBody
(
joinPoint
);
boolean
clear
=
aspectLog
.
requestLogInit
();
LogInfoVo
log
=
aspectLog
.
start
(
declaringType
,
TAG
,
url
,
requestBody
,
clear
);
String
bodyString
=
String
.
format
(
"地址:%s 内容:%s"
,
webMethodUrl
.
getRequestUrl
(),
logString
.
getBodyString
(
requestBody
));
LogInfoVo
log
=
aspectLog
.
start
(
declaringType
,
TAG
,
webMethodUrl
.
getConfigUrl
(),
bodyString
,
clear
);
Object
result
=
null
;
Exception
ex
=
null
;
...
...
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/AspectLogUrl.java
View file @
4fb19acc
...
...
@@ -26,89 +26,137 @@ import java.util.List;
*/
@Component
public
class
AspectLogUrl
{
/**
* /**
* 获取方法名称
*
* @return 方法路径
*/
public
String
getWebMethodUrl
(
ProceedingJoinPoint
joinPoint
)
{
String
url
=
StringHelper
.
EMPTY
;
public
WebUrlInfo
getWebMethodBaseUrl
(
ProceedingJoinPoint
joinPoint
)
{
Signature
signature
=
joinPoint
.
getSignature
();
Class
<?>
declaringType
=
signature
.
getDeclaringType
();
String
name
=
signature
.
getName
();
FeignClient
feignClient
=
declaringType
.
getAnnotation
(
FeignClient
.
class
);
// 类路径
RequestMapping
[]
classRequests
=
declaringType
.
getAnnotationsByType
(
RequestMapping
.
class
);
PostMapping
[]
classPosts
=
declaringType
.
getAnnotationsByType
(
PostMapping
.
class
);
GetMapping
[]
classGets
=
declaringType
.
getAnnotationsByType
(
GetMapping
.
class
);
WebUrlInfo
url
=
getWebMethodUrlHandle
(
signature
,
declaringType
);
String
baseUrl
=
String
.
format
(
"%s:%s"
,
declaringType
.
getSimpleName
(),
name
);
if
(
url
==
null
)
{
return
new
WebUrlInfo
(
baseUrl
,
baseUrl
);
}
return
new
WebUrlInfo
(
String
.
format
(
"%s:%s"
,
baseUrl
,
url
.
configUrl
),
String
.
format
(
"%s:%s"
,
baseUrl
,
url
.
requestUrl
));
}
List
<
Annotation
>
annotation
=
new
ArrayList
<>();
ArrayHelper
.
addList
(
annotation
,
classRequests
,
classPosts
,
classGets
);
/**
* 获取方法名称
*
* @return 方法路径
*/
public
WebUrlInfo
getWebMethodUrl
(
ProceedingJoinPoint
joinPoint
)
{
Signature
signature
=
joinPoint
.
getSignature
();
Class
<?>
declaringType
=
signature
.
getDeclaringType
();
return
getWebMethodUrlHandle
(
signature
,
declaringType
);
}
String
baseUrl
=
String
.
format
(
"%s:%s"
,
declaringType
.
getSimpleName
(),
name
);
if
(!
annotation
.
isEmpty
()
&&
signature
instanceof
MethodSignature
)
{
/**
* 获取地址最终处理方法
*
* @param signature 方法签名
* @param declaringType 类型
* @return 返回地址
*/
private
WebUrlInfo
getWebMethodUrlHandle
(
Signature
signature
,
Class
<?>
declaringType
)
{
FeignClient
feignClient
=
declaringType
.
getAnnotation
(
FeignClient
.
class
);
// 类路径
List
<
Annotation
>
classAnnotation
=
getClassRequestAnnotations
(
declaringType
);
if
(!
classAnnotation
.
isEmpty
()
&&
signature
instanceof
MethodSignature
)
{
MethodSignature
methodSignature
=
(
MethodSignature
)
signature
;
Method
targetMethod
=
methodSignature
.
getMethod
();
// 方法路径
RequestMapping
[]
requests
=
targetMethod
.
getAnnotationsByType
(
RequestMapping
.
class
);
PostMapping
[]
posts
=
targetMethod
.
getAnnotationsByType
(
PostMapping
.
class
);
GetMapping
[]
gets
=
targetMethod
.
getAnnotationsByType
(
GetMapping
.
class
);
url
=
getFeignOrRequestUrl
(
feignClient
,
classRequests
,
classPosts
,
classGets
,
requests
,
posts
,
gets
);
}
if
(
StringHelper
.
isEmpty
(
url
))
{
return
baseUrl
;
List
<
Annotation
>
methodAnnotation
=
getMethodRequestAnnotations
(
targetMethod
);
// 请求地址
return
getFeignOrRequestUrl
(
feignClient
,
classAnnotation
,
methodAnnotation
);
}
return
String
.
format
(
"%s:%s"
,
baseUrl
,
url
)
;
return
null
;
}
/**
* 获取feign请求地址
*
* @param feignClient
* @param classRequests
* @param classPosts
* @param classGets
* @param requests
* @param posts
* @param gets
* @return
* @param feignClient 客户端
* @param classAnnotation 类地址
* @param methodAnnotation 方法地址
* @return 最终返回地址
*/
private
String
getFeignOrRequestUrl
(
FeignClient
feignClient
,
RequestMapping
[]
classRequests
,
PostMapping
[]
classPosts
,
GetMapping
[]
classGets
,
RequestMapping
[]
requests
,
PostMapping
[]
posts
,
GetMapping
[]
gets
)
{
StringBuilder
sb
=
new
StringBuilder
();
private
WebUrlInfo
getFeignOrRequestUrl
(
FeignClient
feignClient
,
List
<
Annotation
>
classAnnotation
,
List
<
Annotation
>
methodAnnotation
)
{
// 配置地址
StringBuilder
sbConfig
=
new
StringBuilder
();
// HTTP请求地址
StringBuilder
sbRequest
=
new
StringBuilder
();
if
(
feignClient
!=
null
)
{
sb
.
append
(
feignClient
.
value
());
sb
.
append
(
":"
);
sbConfig
.
append
(
feignClient
.
value
());
sbConfig
.
append
(
":"
);
}
else
{
HttpServletRequest
request
=
CookiesHelper
.
getRequest
();
sbRequest
.
append
(
request
.
getRequestURI
());
if
(!
StringHelper
.
isEmpty
(
request
.
getQueryString
()))
{
sbRequest
.
append
(
"?"
);
sbRequest
.
append
(
request
.
getQueryString
());
}
}
appendUrl
(
sbConfig
,
classAnnotation
);
appendUrl
(
sbConfig
,
methodAnnotation
);
return
new
WebUrlInfo
(
sbConfig
.
toString
(),
sbRequest
.
toString
());
}
List
<
RequestMapping
>
reqList
=
ArrayHelper
.
mergeList
(
classRequests
,
requests
);
List
<
PostMapping
>
postList
=
ArrayHelper
.
mergeList
(
classPosts
,
posts
);
List
<
GetMapping
>
getList
=
ArrayHelper
.
mergeList
(
classGets
,
gets
);
for
(
RequestMapping
item
:
reqList
)
{
private
List
<
Annotation
>
getClassRequestAnnotations
(
Class
<?>
clazz
)
{
List
<
Annotation
>
annotation
=
new
ArrayList
<>();
ArrayHelper
.
addList
(
annotation
,
clazz
.
getAnnotationsByType
(
RequestMapping
.
class
),
clazz
.
getAnnotationsByType
(
PostMapping
.
class
),
clazz
.
getAnnotationsByType
(
GetMapping
.
class
)
);
return
annotation
;
}
private
List
<
Annotation
>
getMethodRequestAnnotations
(
Method
method
)
{
List
<
Annotation
>
annotation
=
new
ArrayList
<>();
ArrayHelper
.
addList
(
annotation
,
method
.
getAnnotationsByType
(
RequestMapping
.
class
),
method
.
getAnnotationsByType
(
PostMapping
.
class
),
method
.
getAnnotationsByType
(
GetMapping
.
class
)
);
return
annotation
;
}
private
void
appendUrl
(
StringBuilder
sb
,
List
<
Annotation
>
annotations
)
{
for
(
Annotation
annotation
:
annotations
)
{
String
url
=
StringHelper
.
EMPTY
;
if
(
annotation
instanceof
RequestMapping
)
{
RequestMapping
item
=
(
RequestMapping
)
annotation
;
if
(
item
.
value
().
length
>
0
)
{
sb
.
append
(
item
.
value
()[
0
])
;
url
=
item
.
value
()[
0
]
;
}
}
for
(
PostMapping
item
:
postList
)
{
}
else
if
(
annotation
instanceof
PostMapping
)
{
PostMapping
item
=
(
PostMapping
)
annotation
;
if
(
item
.
value
().
length
>
0
)
{
sb
.
append
(
item
.
value
()[
0
])
;
url
=
item
.
value
()[
0
]
;
}
}
for
(
GetMapping
item
:
getList
)
{
}
else
if
(
annotation
instanceof
GetMapping
)
{
PostMapping
item
=
(
PostMapping
)
annotation
;
if
(
item
.
value
().
length
>
0
)
{
sb
.
append
(
item
.
value
()[
0
])
;
url
=
item
.
value
()[
0
]
;
}
}
}
else
{
HttpServletRequest
request
=
CookiesHelper
.
getRequest
();
sb
.
append
(
request
.
getRequestURI
());
if
(!
StringHelper
.
isEmpty
(
request
.
getQueryString
()))
{
sb
.
append
(
"?"
);
sb
.
append
(
request
.
getQueryString
());
// 地址处理,源地址最后增加斜杠
if
(!
StringHelper
.
compare
(
sb
.
substring
(
sb
.
length
()
-
1
,
sb
.
length
()),
"/"
))
{
sb
.
append
(
"/"
);
}
// 地址处理,来源地址去掉最后的斜杠
url
=
StringHelper
.
trimStart
(
url
,
"/"
);
sb
.
append
(
url
);
}
return
sb
.
toString
();
}
/**
...
...
@@ -151,4 +199,31 @@ public class AspectLogUrl {
return
String
.
format
(
"%s:%s"
,
baseUrl
,
url
);
}
public
static
class
WebUrlInfo
{
/**
* 配置地址
*/
private
final
String
configUrl
;
/**
* 请求地址
*/
private
final
String
requestUrl
;
public
WebUrlInfo
(
String
configUrl
,
String
requestUrl
)
{
this
.
configUrl
=
configUrl
;
this
.
requestUrl
=
requestUrl
;
}
public
String
getConfigUrl
()
{
return
configUrl
;
}
public
String
getRequestUrl
()
{
if
(
StringHelper
.
isEmpty
(
requestUrl
))
{
return
configUrl
;
}
return
requestUrl
;
}
}
}
yzg-util-cloud/src/main/java/com/yanzuoguang/cloud/aop/AspectWeb.java
View file @
4fb19acc
...
...
@@ -3,6 +3,7 @@ package com.yanzuoguang.cloud.aop;
import
com.yanzuoguang.cloud.service.TokenServiceCall
;
import
com.yanzuoguang.log.AspectLog
;
import
com.yanzuoguang.log.LogInfoVo
;
import
com.yanzuoguang.log.LogString
;
import
com.yanzuoguang.token.TokenHelper
;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
import
com.yanzuoguang.util.helper.JsonHelper
;
...
...
@@ -40,18 +41,21 @@ public class AspectWeb {
private
final
AspectLogUrl
aspectLogUrl
;
private
final
AspectLogBody
aspectLogBody
;
private
final
AspectLog
aspectLog
;
private
final
LogString
logString
;
public
AspectWeb
(
TokenServiceCall
tokenServiceCall
,
Optional
<
WebAspectInit
>
webAspectInit
,
CloudConfig
cloudConfig
,
AspectLogUrl
aspectLogUrl
,
AspectLogBody
aspectLogBody
,
AspectLog
aspectLog
)
{
AspectLog
aspectLog
,
LogString
logString
)
{
this
.
tokenServiceCall
=
tokenServiceCall
;
this
.
cloudConfig
=
cloudConfig
;
this
.
aspectLogUrl
=
aspectLogUrl
;
this
.
aspectLogBody
=
aspectLogBody
;
this
.
aspectLog
=
aspectLog
;
this
.
logString
=
logString
;
if
(
webAspectInit
.
isPresent
())
{
this
.
webAspectInit
=
webAspectInit
.
get
();
}
else
{
...
...
@@ -77,7 +81,7 @@ public class AspectWeb {
@Around
(
value
=
"webAspect()"
)
public
Object
requestWebAround
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
Class
<?>
declaringType
=
joinPoint
.
getSignature
().
getDeclaringType
();
String
url
=
aspectLogUrl
.
getWebMethod
Url
(
joinPoint
);
AspectLogUrl
.
WebUrlInfo
webMethodUrl
=
aspectLogUrl
.
getWebMethodBase
Url
(
joinPoint
);
// 判断是否网关
boolean
isGateWay
=
cloudConfig
.
isGateWay
();
if
(
isGateWay
)
{
...
...
@@ -85,13 +89,15 @@ public class AspectWeb {
// 网关不进行任何拦截处理
return
executeMethod
(
joinPoint
);
}
catch
(
Exception
ex
)
{
System
.
err
.
println
(
"请求地址错误:"
+
url
);
System
.
err
.
println
(
"请求地址错误:"
+
webMethodUrl
.
getRequestUrl
()
);
throw
ex
;
}
}
Object
requestBody
=
aspectLogBody
.
getRequestBody
(
joinPoint
);
boolean
clear
=
aspectLog
.
requestLogInit
();
LogInfoVo
log
=
aspectLog
.
start
(
declaringType
,
TAG
,
UrlHelper
.
getPage
(
url
),
String
.
format
(
"请求地址:%s 请求内容:%s"
,
url
,
requestBody
),
clear
);
Object
requestBody
=
aspectLogBody
.
getRequestBody
(
joinPoint
);
String
bodyString
=
String
.
format
(
"地址:%s 内容:%s"
,
webMethodUrl
.
getRequestUrl
(),
logString
.
getBodyString
(
requestBody
));
LogInfoVo
log
=
aspectLog
.
start
(
declaringType
,
TAG
,
UrlHelper
.
getPage
(
webMethodUrl
.
getConfigUrl
()),
bodyString
,
clear
);
Exception
ex
=
null
;
boolean
isInit
=
false
;
...
...
@@ -164,4 +170,6 @@ public class AspectWeb {
private
Object
executeMethod
(
ProceedingJoinPoint
joinPoint
)
throws
Throwable
{
return
joinPoint
.
proceed
();
}
}
yzg-util-db/src/main/java/com/yanzuoguang/log/AspectLog.java
View file @
4fb19acc
...
...
@@ -47,20 +47,13 @@ public class AspectLog {
/**
* 记录请求日志
*
* @param tag
标记
* @param url
地址
* @param
requestBody
请求内容
* @param logFlag
是否打印日志
* @param tag 标记
* @param url 地址
* @param
body
请求内容
* @param logFlag 是否打印日志
* @return 继续记录日志的对象
*/
public
LogInfoVo
start
(
Class
<?>
cls
,
String
tag
,
String
url
,
Object
requestBody
,
boolean
logFlag
)
{
String
body
;
try
{
body
=
logString
.
getBodyString
(
requestBody
);
}
catch
(
Exception
ex
)
{
ExceptionHelper
.
PrintError
(
AspectLog
.
class
,
ex
);
body
=
StringHelper
.
EMPTY
;
}
public
LogInfoVo
start
(
Class
<?>
cls
,
String
tag
,
String
url
,
String
body
,
boolean
logFlag
)
{
// 声明日志对象
LogInfoVo
log
=
new
LogInfoVo
();
...
...
yzg-util-db/src/main/java/com/yanzuoguang/log/LogString.java
View file @
4fb19acc
package
com
.
yanzuoguang
.
log
;
import
com.yanzuoguang.util.exception.ExceptionHelper
;
import
com.yanzuoguang.util.helper.JsonHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
com.yanzuoguang.util.vo.CloudConfig
;
import
org.springframework.stereotype.Component
;
...
...
@@ -39,12 +41,16 @@ public class LogString {
*/
public
String
getBodyString
(
Object
body
)
{
String
ret
;
if
(
body
instanceof
String
)
{
ret
=
getMaxString
((
String
)
body
);
}
else
{
ret
=
getMaxString
(
JsonHelper
.
serialize
(
body
));
try
{
if
(
body
instanceof
String
)
{
ret
=
getMaxString
((
String
)
body
);
}
else
{
ret
=
getMaxString
(
JsonHelper
.
serialize
(
body
));
}
}
catch
(
Exception
e
)
{
ExceptionHelper
.
PrintError
(
AspectLog
.
class
,
e
);
ret
=
StringHelper
.
EMPTY
;
}
return
ret
;
}
}
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