forked from elunez/eladmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
郑杰
committed
Dec 25, 2018
1 parent
55d4824
commit 4765785
Showing
17 changed files
with
256 additions
and
856 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package me.zhengjie.monitor.config; | ||
|
||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.classic.spi.IThrowableProxy; | ||
import ch.qos.logback.core.filter.Filter; | ||
import ch.qos.logback.core.spi.FilterReply; | ||
import me.zhengjie.monitor.domain.LogMessage; | ||
|
||
import java.text.DateFormat; | ||
import java.util.Date; | ||
|
||
/** | ||
* 定义Logfilter拦截输出日志 | ||
* @author jie | ||
* @date 2018-12-24 | ||
*/ | ||
public class LogFilter extends Filter<ILoggingEvent>{ | ||
|
||
@Override | ||
public FilterReply decide(ILoggingEvent event) { | ||
String exception = ""; | ||
IThrowableProxy iThrowableProxy1 = event.getThrowableProxy(); | ||
if(iThrowableProxy1!=null){ | ||
exception = "<span class='excehtext'>"+iThrowableProxy1.getClassName()+" "+iThrowableProxy1.getMessage()+"</span></br>"; | ||
for(int i=0; i<iThrowableProxy1.getStackTraceElementProxyArray().length;i++){ | ||
exception += "<span class='excetext'>"+iThrowableProxy1.getStackTraceElementProxyArray()[i].toString()+"</span></br>"; | ||
} | ||
} | ||
LogMessage loggerMessage = new LogMessage( | ||
event.getMessage() | ||
, DateFormat.getDateTimeInstance().format(new Date(event.getTimeStamp())), | ||
event.getThreadName(), | ||
event.getLoggerName(), | ||
event.getLevel().levelStr, | ||
exception | ||
); | ||
LoggerQueue.getInstance().push(loggerMessage); | ||
return FilterReply.ACCEPT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package me.zhengjie.monitor.config; | ||
|
||
import me.zhengjie.monitor.domain.LogMessage; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
|
||
/** | ||
* 创建一个阻塞队列,作为日志系统输出的日志的一个临时载体 | ||
* @author jie | ||
* @date 2018-12-24 | ||
*/ | ||
public class LoggerQueue { | ||
|
||
//队列大小 | ||
public static final int QUEUE_MAX_SIZE = 10000; | ||
|
||
private static LoggerQueue alarmMessageQueue = new LoggerQueue(); | ||
//阻塞队列 | ||
private BlockingQueue blockingQueue = new LinkedBlockingQueue<>(QUEUE_MAX_SIZE); | ||
|
||
private LoggerQueue() { | ||
} | ||
|
||
public static LoggerQueue getInstance() { | ||
return alarmMessageQueue; | ||
} | ||
|
||
/** | ||
* 消息入队 | ||
* | ||
* @param log | ||
* @return | ||
*/ | ||
public boolean push(LogMessage log) { | ||
return this.blockingQueue.add(log);//队列满了就抛出异常,不阻塞 | ||
} | ||
|
||
/** | ||
* 消息出队 | ||
* | ||
* @return | ||
*/ | ||
public LogMessage poll() { | ||
LogMessage result = null; | ||
try { | ||
result = (LogMessage) this.blockingQueue.take(); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
return result; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/me/zhengjie/monitor/config/VisitsScheduling.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package me.zhengjie.monitor.config; | ||
|
||
import me.zhengjie.monitor.service.VisitsService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author jie | ||
* @date 2018-12-25 | ||
*/ | ||
@Component | ||
@Async | ||
public class VisitsScheduling { | ||
|
||
@Autowired | ||
private VisitsService visitsService; | ||
|
||
/** | ||
* 每天0:01运行 | ||
*/ | ||
@Scheduled(cron = "1 0 0 * * ?") | ||
public void save(){ | ||
visitsService.save(); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/me/zhengjie/monitor/config/WebSocketConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package me.zhengjie.monitor.config; | ||
|
||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import me.zhengjie.monitor.domain.LogMessage; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.messaging.simp.SimpMessagingTemplate; | ||
import org.springframework.web.socket.config.annotation.StompEndpointRegistry; | ||
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; | ||
import javax.annotation.PostConstruct; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
/** | ||
* 配置WebSocket消息代理端点,即stomp服务端 | ||
* @author jie | ||
* @date 2018-12-24 | ||
*/ | ||
@Slf4j | ||
@Configuration | ||
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { | ||
|
||
@Autowired | ||
private SimpMessagingTemplate messagingTemplate; | ||
|
||
@Override | ||
public void registerStompEndpoints(StompEndpointRegistry registry) { | ||
registry.addEndpoint("/websocket") | ||
.setAllowedOrigins("*") | ||
.withSockJS(); | ||
} | ||
|
||
/** | ||
* 推送日志到/topic/pullLogger | ||
*/ | ||
@PostConstruct | ||
public void pushLogger(){ | ||
ExecutorService executorService= Executors.newFixedThreadPool(2); | ||
Runnable runnable=new Runnable() { | ||
|
||
@Override | ||
public void run() { | ||
while (true) { | ||
try { | ||
LogMessage log = LoggerQueue.getInstance().poll(); | ||
if(log!=null){ | ||
if(log.getClassName().equals("jdbc.resultsettable")){ | ||
log.setBody("<br><pre>"+log.getBody()+"</pre>"); | ||
} | ||
if(messagingTemplate!=null){ | ||
messagingTemplate.convertAndSend("/topic/logMsg",log); | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
}; | ||
executorService.submit(runnable); | ||
executorService.submit(runnable); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package me.zhengjie.monitor.domain; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
/** | ||
* @author 郑杰 | ||
* @date 2018-12-24 | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
public class LogMessage { | ||
|
||
private String body; | ||
private String timestamp; | ||
private String threadName; | ||
private String className; | ||
private String level; | ||
private String exception; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.