From cf43f12a6555b19b0f357d113b5c164d9fd47c13 Mon Sep 17 00:00:00 2001 From: tianxia007 Date: Fri, 5 Jan 2018 12:11:15 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=91=E5=90=AC=E5=A4=96=E9=83=A8=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 107 ++++++++++-------- .../java/com/example/websocket/LogFile.java | 68 +++++++++++ .../websocket/WebsocketApplication.java | 30 +++-- src/main/resources/application.properties | 2 +- 4 files changed, 146 insertions(+), 61 deletions(-) create mode 100644 src/main/java/com/example/websocket/LogFile.java diff --git a/pom.xml b/pom.xml index 603f9a0..49df087 100644 --- a/pom.xml +++ b/pom.xml @@ -1,55 +1,62 @@ - 4.0.0 - - com.example - websocket - 0.0.1-SNAPSHOT - jar - - websocket - Demo project for Spring Boot - - - org.springframework.boot - spring-boot-starter-parent - 1.5.7.RELEASE - - - - - UTF-8 - UTF-8 - 1.8 - - - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-websocket - - - - org.springframework.boot - spring-boot-starter-test - test - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + com.example + websocket + 0.0.1-SNAPSHOT + jar + + websocket + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.5.7.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-websocket + + + + org.springframework.boot + spring-boot-starter-test + test + + + + com.xiaoleilu + hutool-all + 3.3.0 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + diff --git a/src/main/java/com/example/websocket/LogFile.java b/src/main/java/com/example/websocket/LogFile.java new file mode 100644 index 0000000..9930913 --- /dev/null +++ b/src/main/java/com/example/websocket/LogFile.java @@ -0,0 +1,68 @@ +package com.example.websocket; + +import com.xiaoleilu.hutool.io.FileUtil; +import com.xiaoleilu.hutool.io.watch.WatchMonitor; +import com.xiaoleilu.hutool.io.watch.Watcher; +import com.xiaoleilu.hutool.lang.Console; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.WatchEvent; +import java.sql.Date; +import java.text.DateFormat; + +/** + * Created by dw on 2018/01/05 + */ +public class LogFile { + public static void main(String[] args) { + addMessage(); + } + + public static void addMessage() { + File file = FileUtil.file("C:\\projects_openproject\\boot-websocket-log\\log\\spring.log"); + System.out.println(file.getAbsolutePath()); + WatchMonitor watchMonitor = WatchMonitor.create(file, WatchMonitor.ENTRY_MODIFY); + watchMonitor.setWatcher(new + + Watcher() { + @Override + public void onCreate(WatchEvent event, Path currentPath) { + Object obj = event.context(); + Console.log("创建:{}-> {}", currentPath, obj); + } + + @Override + public void onModify(WatchEvent event, Path currentPath) { + LoggerMessage loggerMessage = new LoggerMessage( + "修改:{}-> {}" + , null, + null, + null, + "debugg" + ); + LoggerQueue.getInstance().push(loggerMessage); + + Object obj = event.context(); + Console.log("修改:{}-> {}", currentPath, obj); + } + + @Override + public void onDelete(WatchEvent event, Path currentPath) { + Object obj = event.context(); + Console.log("删除:{}-> {}", currentPath, obj); + } + + @Override + public void onOverflow(WatchEvent event, Path currentPath) { + Object obj = event.context(); + Console.log("Overflow:{}-> {}", currentPath, obj); + } + }); + + //设置监听目录的最大深入,目录层级大于制定层级的变更将不被监听,默认只监听当前层级目录 + watchMonitor.setMaxDepth(3); + //启动监听 + watchMonitor.start(); + } +} diff --git a/src/main/java/com/example/websocket/WebsocketApplication.java b/src/main/java/com/example/websocket/WebsocketApplication.java index 114e270..857f40a 100644 --- a/src/main/java/com/example/websocket/WebsocketApplication.java +++ b/src/main/java/com/example/websocket/WebsocketApplication.java @@ -6,21 +6,31 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @EnableScheduling +@RestController public class WebsocketApplication { - private Logger logger = LoggerFactory.getLogger(WebsocketApplication.class); + private Logger logger = LoggerFactory.getLogger(WebsocketApplication.class); - public static void main(String[] args) { - SpringApplication.run(WebsocketApplication.class, args); - } + public static void main(String[] args) { + LogFile.main(args); + SpringApplication.run(WebsocketApplication.class, args); + } - int info = 1; + int info = 1; - @Scheduled(fixedRate = 1000) - public void outputLogger() { - logger.info("测试日志输出" + info++); - throw new RuntimeException(); - } + //@Scheduled(fixedRate = 1000) + public void outputLogger() { + logger.info("测试日志输出" + info++); + throw new RuntimeException(); + } + + @RequestMapping("/hello") + public String hello() { + logger.debug("访问了hello"); + return "hello!"; + } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index c18cecc..68a19b5 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,3 @@ server.port=8084 logging.level.com.example.websocket=debug -logging.path=/opt +logging.path=C:\\projects_openproject\\boot-websocket-log\\log -- Gitee