Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dinglog-spring-boot-started
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
VCC
dinglog-spring-boot-started
Commits
82fb854e
Commit
82fb854e
authored
Mar 11, 2021
by
孙路路
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/master'
parents
29f3a17a
acfffb8e
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
214 additions
and
2 deletions
+214
-2
README.md
README.md
+5
-0
pom.xml
pom.xml
+1
-1
DingTalkHookLogErrorConfig.java
...quantgroup/dinglog/config/DingTalkHookLogErrorConfig.java
+57
-0
DingTalkLogbackSendMsg.java
...cn/quantgroup/dinglog/logback/DingTalkLogbackSendMsg.java
+8
-0
LogErrorSendMsgUtil.java
.../java/cn/quantgroup/dinglog/util/LogErrorSendMsgUtil.java
+141
-0
spring.factories
src/main/resources/META-INF/spring.factories
+2
-1
No files found.
README.md
View file @
82fb854e
# dinglog-spring-boot-started
1.
支持错误日志发送钉钉消息, 接入方式:
2.
支持错误日志发送到kill_bugs项目进行数据库存储,接入方式:
配置dinglog.hooke.logerror.enable=true;
dinglog.hooke.logerror.url=kill_bugs项目路径接收路径,以后补充
pom.xml
View file @
82fb854e
...
...
@@ -11,7 +11,7 @@
</parent>
<groupId>
cn.quantgroup.dinglog
</groupId>
<artifactId>
dinglog-spring-boot-started
</artifactId>
<version>
2021.0
1.12
.1
</version>
<version>
2021.0
3.08
.1
</version>
<name>
dinglog-spring-boot-started
</name>
<description>
dinglog project for Spring Boot
</description>
...
...
src/main/java/cn/quantgroup/dinglog/config/DingTalkHookLogErrorConfig.java
0 → 100644
View file @
82fb854e
package
cn
.
quantgroup
.
dinglog
.
config
;
import
cn.quantgroup.dinglog.properties.DingTalkWebHookProperties
;
import
cn.quantgroup.dinglog.service.DingTalkSendMsgService
;
import
cn.quantgroup.dinglog.service.impl.DingTalkSendMsgServiceImpl
;
import
cn.quantgroup.dinglog.util.LogErrorSendMsgUtil
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
/**
* @Describe:
* @Created by tangfeng 2020-07-07 19:56
*/
@Configuration
//@ConfigurationProperties(prefix = "dinglog.hooke.logerror")
public
class
DingTalkHookLogErrorConfig
{
private
static
String
url
;
private
static
Boolean
enable
=
false
;
/**
* 单线程发送,最大条数200条,超过废弃
*/
@Value
(
"${dinglog.hooke.logerror.enable:false}"
)
public
void
setDomain
(
String
enableString
){
if
(
enableString
==
null
){
return
;
}
if
(
"true"
.
equals
(
enableString
)){
enable
=
true
;
}
else
{
enable
=
false
;
}
}
@Value
(
"${dinglog.hooke.logerror.url:http://127.0.0.1}"
)
public
void
setUrl
(
String
url
){
DingTalkHookLogErrorConfig
.
url
=
url
;
}
public
static
Boolean
isEnable
(){
return
enable
;
}
public
static
void
setEnable
(
boolean
inEnable
){
enable
=
inEnable
;
}
public
static
String
getUrl
(){
return
url
;
}
}
src/main/java/cn/quantgroup/dinglog/logback/DingTalkLogbackSendMsg.java
View file @
82fb854e
...
...
@@ -6,6 +6,7 @@ import ch.qos.logback.classic.spi.LoggingEvent;
import
ch.qos.logback.classic.spi.StackTraceElementProxy
;
import
ch.qos.logback.core.spi.ContextAwareBase
;
import
cn.quantgroup.dinglog.util.DingTalkSendMsgUtil
;
import
cn.quantgroup.dinglog.util.LogErrorSendMsgUtil
;
import
org.apache.commons.lang3.StringUtils
;
/**
...
...
@@ -49,6 +50,7 @@ public class DingTalkLogbackSendMsg {
layoutLog
=
dingTalkLogbackParam
.
getMarkMsg
().
concat
(
"---"
).
concat
(
layoutLog
);
}
DingTalkSendMsgUtil
.
sendTextMsg
(
layoutLog
,
accessToken
);
LogErrorSendMsgUtil
.
sendMsg
(
layoutLog
);
return
;
}
...
...
@@ -68,6 +70,11 @@ public class DingTalkLogbackSendMsg {
for
(
int
i
=
0
;
i
<
stackTraceElementProxyArray
.
length
;
i
++)
{
StackTraceElementProxy
st
=
stackTraceElementProxyArray
[
i
];
if
(
StringUtils
.
isNotEmpty
(
dingTalkLogbackParam
.
getLogExStackTracePackage
())
&&
st
.
toString
().
contains
(
dingTalkLogbackParam
.
getLogExStackTracePackage
())
)
{
if
(
st
.
getStackTraceElement
()!=
null
&&
st
.
getStackTraceElement
().
getFileName
()!=
null
&&
st
.
getStackTraceElement
().
getFileName
().
contains
(
"generated"
)
&&
st
.
getStackTraceElement
().
getClassName
()!=
null
&&
st
.
getStackTraceElement
().
getClassName
().
contains
(
"BySpringCGLIB"
)){
continue
;
}
sbExMsg
.
append
(
st
.
toString
()).
append
(
"\n"
);
}
}
...
...
@@ -80,6 +87,7 @@ public class DingTalkLogbackSendMsg {
message
=
dingTalkLogbackParam
.
getMarkMsg
().
concat
(
"---"
).
concat
(
message
);
}
DingTalkSendMsgUtil
.
sendTextMsg
(
message
,
accessToken
);
LogErrorSendMsgUtil
.
sendMsg
(
message
);
}
catch
(
Exception
e
)
{
base
.
addInfo
(
"ding talk logback send msg error:{}"
,
e
);
}
...
...
src/main/java/cn/quantgroup/dinglog/util/LogErrorSendMsgUtil.java
0 → 100644
View file @
82fb854e
package
cn
.
quantgroup
.
dinglog
.
util
;
import
ch.qos.logback.classic.spi.IThrowableProxy
;
import
ch.qos.logback.classic.spi.StackTraceElementProxy
;
import
cn.quantgroup.dinglog.config.DingTalkHookLogErrorConfig
;
import
com.alibaba.fastjson.JSON
;
import
com.taobao.api.Constants
;
import
com.taobao.api.internal.util.HttpResponseData
;
import
com.taobao.api.internal.util.WebV2Utils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Value
;
import
java.io.IOException
;
import
java.time.LocalDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
java.util.concurrent.ArrayBlockingQueue
;
import
java.util.concurrent.ThreadFactory
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
java.util.concurrent.TimeUnit
;
/**
* @Describe:
* @Created by tangfeng 2020-07-07 19:58
*/
public
class
LogErrorSendMsgUtil
{
static
Logger
logger
=
LoggerFactory
.
getLogger
(
LogErrorSendMsgUtil
.
class
);
static
ThreadPoolExecutor
executor
=
new
ThreadPoolExecutor
(
1
,
1
,
5
l
,
TimeUnit
.
MINUTES
,
new
ArrayBlockingQueue
<>(
200
),
new
ThreadFactory
(){
public
Thread
newThread
(
Runnable
r
)
{
return
new
Thread
(
r
,
"钉钉log错误消息"
);
}},
new
ThreadPoolExecutor
.
DiscardOldestPolicy
());
public
static
void
sendMsg
(
String
msg
,
Throwable
e
){
if
(
msg
==
null
)
{
return
;
}
if
(!
DingTalkHookLogErrorConfig
.
isEnable
()){
return
;
}
if
(
e
!=
null
){
msg
=
msg
+
"\n"
+
getStackTraceElementMessage
(
e
);
}
sendMsg
(
msg
);
}
public
static
String
getStackTraceElementMessage
(
Throwable
ex
){
int
index
=
0
;
String
errorMessage
=
""
;
StackTraceElement
[]
stackTraceElements
=
ex
.
getStackTrace
();
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
ex
.
fillInStackTrace
()+
"\n"
);
for
(
StackTraceElement
stackTraceElement:
stackTraceElements
){
if
(
index
>
3
){
return
errorMessage
;
}
try
{
String
className
=
stackTraceElement
.
getClassName
();
String
fileName
=
stackTraceElement
.
getFileName
();
if
(
className
!=
null
&&
className
.
contains
(
"BySpringCGLIB"
)
&&
fileName
!=
null
&&
fileName
.
contains
(
"generated"
)){
continue
;
}
if
(
className
==
null
||
className
.
contains
(
"quantgroup"
)
||
className
.
contains
(
".qg."
)||
className
.
contains
(
".xyqb."
)||
className
.
contains
(
".lkb."
)||
className
.
contains
(
".liangkebang."
)
){
index
++;
String
les
[]
=
stackTraceElement
.
getClassName
().
split
(
"\\."
);
// if(les.length>3){
// sb.append("*.").append(les[les.length-3]).append(".").append(les[les.length-2]).append(".").append(les[les.length-1]);
// }else{
sb
.
append
(
stackTraceElement
.
getClassName
());
// }
sb
.
append
(
"."
+
stackTraceElement
.
getMethodName
()
+
"("
+
stackTraceElement
.
getFileName
()
+
":"
+
stackTraceElement
.
getLineNumber
()
+
") \n"
);
errorMessage
=
sb
.
toString
();
}
}
catch
(
Exception
e
){
}
}
return
errorMessage
;
}
public
static
void
sendMsg
(
String
msg
){
if
(!
DingTalkHookLogErrorConfig
.
isEnable
()){
return
;
}
executor
.
execute
(()->{
//组装参数,准备发送
// long time = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
String
error_time
=
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd HH:mm:ss"
).
format
(
LocalDateTime
.
now
());
Map
<
String
,
String
>
params
=
new
LinkedHashMap
<>();
params
.
put
(
"error_time"
,
error_time
);
String
project_name
=
System
.
getProperty
(
"@appId"
);
if
(
project_name
==
null
){
project_name
=
System
.
getProperty
(
"sun.java.command"
);
String
[]
strings
=
project_name
.
split
(
"\\."
);
project_name
=
strings
[
strings
.
length
-
1
];
}
params
.
put
(
"project_name"
,
project_name
);
params
.
put
(
"env"
,
System
.
getProperty
(
"env"
));
params
.
put
(
"msg"
,
msg
);
HttpResponseData
responseData
=
null
;
logger
.
debug
(
"发送错误信息 url={}, json={}"
,
DingTalkHookLogErrorConfig
.
getUrl
(),
JSON
.
toJSONString
(
params
));
try
{
//不关心返回结果
responseData
=
WebV2Utils
.
doPost
(
DingTalkHookLogErrorConfig
.
getUrl
(),
params
,
Constants
.
CHARSET_UTF8
,
1000
,
1000
);
}
catch
(
IOException
e
)
{
logger
.
error
(
"访问killbugs发送异常,msg={}"
,
e
.
getLocalizedMessage
());
}
if
(
responseData
.
getBody
()!=
null
&&
responseData
.
getBody
().
contains
(
"ok"
)){
logger
.
info
(
"项目名{},环境={},发送成功"
,
project_name
,
System
.
getProperty
(
"env"
));
}
else
{
logger
.
info
(
"项目名{},环境={}, 发送失败"
,
project_name
,
System
.
getProperty
(
"env"
));
}
});
}
public
static
void
main
(
String
[]
args
)
{
String
s
=
"cn.quantgroup.kdsp.operation.KdspOperationApplication"
;
String
[]
strings
=
s
.
split
(
"\\."
);
System
.
out
.
println
(
strings
[
2
]);
}
}
src/main/resources/META-INF/spring.factories
View file @
82fb854e
#-------starter自动装配,多个以逗号隔开---------
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.quantgroup.dinglog.demo.DemoConfig,\
cn.quantgroup.dinglog.config.DingTalkWebHookConfig
cn.quantgroup.dinglog.config.DingTalkWebHookConfig,\
cn.quantgroup.dinglog.config.DingTalkHookLogErrorConfig
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