diff --git a/demo-log/demo-log-logback/README.md b/demo-log/demo-log-logback/README.md new file mode 100644 index 0000000..34cdac3 --- /dev/null +++ b/demo-log/demo-log-logback/README.md @@ -0,0 +1,152 @@ +## spring-boot-demo-logback + +> 此 demo 主要演示了如何使用 logback 记录程序运行过程中的日志,以及如何配置 logback,可以同时生成控制台日志和文件日志记录,文件日志以日期和大小进行拆分生成。 + +### 1.开发步骤 +#### 1.1.添加依赖 + +```xml + + + + org.springframework.boot + spring-boot-starter-logging + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.projectlombok + lombok + true + + +``` + +#### 1.2.编写日志配置文件 + +> 在 `classpath` 下创建名为 `logback-spring.xml` 的配置文件 + +```xml + + + + // 引入 spring 内置的配置文件,获取一些变量值 + + + + INFO + + + ${CONSOLE_LOG_PATTERN} + UTF-8 + + + + + + + + ERROR + + DENY + + ACCEPT + + + + + + + logs/demo-log/demo-log-logback/info.created_on_%d{yyyy-MM-dd}.part_%i.log + + 90 + + + + + 2MB + + + + + + + ${FILE_LOG_PATTERN} + UTF-8 + + + + + + + Error + + + + + + + logs/demo-log/demo-log-logback/error.created_on_%d{yyyy-MM-dd}.part_%i.log + + 90 + + + 2MB + + + + ${FILE_ERROR_PATTERN} + UTF-8 + + + + + + + + + +``` + +#### 1.3.添加日志打印 +```java +@Slf4j +@SpringBootApplication +public class LogbackApplication { + + public static void main(String[] args) { + ConfigurableApplicationContext context = SpringApplication.run(LogbackApplication.class, args); + int length = context.getBeanDefinitionNames().length; + log.trace("Spring boot启动初始化了 {} 个 Bean", length); + log.debug("Spring boot启动初始化了 {} 个 Bean", length); + log.info("Spring boot启动初始化了 {} 个 Bean", length); + log.warn("Spring boot启动初始化了 {} 个 Bean", length); + log.error("Spring boot启动初始化了 {} 个 Bean", length); + try { + int i = 0; + int j = 1 / i; + } catch (Exception e) { + log.error("【SpringBootDemoLogbackApplication】启动异常:", e); + } + } +} +``` + +### 2.测试 + +运行 `LogbackApplication` + +可以在项目的根目录下生成日志文件 +- `logs/demo-log/demo-log-logback/info.created_on_xxxx.log` +- `logs/demo-log/demo-log-logback/error.created_on_xxxx.log` + +### 3.参考 + +- [Spring Boot 官方文档 Logging 配置](https://docs.spring.io/spring-boot/docs/3.0.0-M4/reference/htmlsingle/#howto.logging) diff --git a/demo-log/demo-log-logback/pom.xml b/demo-log/demo-log-logback/pom.xml new file mode 100644 index 0000000..b7abe35 --- /dev/null +++ b/demo-log/demo-log-logback/pom.xml @@ -0,0 +1,52 @@ + + + + com.xkcoding + demo-log + 1.0.0-SNAPSHOT + + + 4.0.0 + + demo-log-logback + 1.0.0-SNAPSHOT + jar + + demo-log-logback + Demo project for Spring Boot + + + 17 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + true + + + + + demo-log-logback + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/demo-logback/src/main/java/com/xkcoding/logback/SpringBootDemoLogbackApplication.java b/demo-log/demo-log-logback/src/main/java/com/xkcoding/logback/LogbackApplication.java similarity index 88% rename from demo-logback/src/main/java/com/xkcoding/logback/SpringBootDemoLogbackApplication.java rename to demo-log/demo-log-logback/src/main/java/com/xkcoding/logback/LogbackApplication.java index 217ee02..6f1b3eb 100644 --- a/demo-logback/src/main/java/com/xkcoding/logback/SpringBootDemoLogbackApplication.java +++ b/demo-log/demo-log-logback/src/main/java/com/xkcoding/logback/LogbackApplication.java @@ -11,14 +11,14 @@ import org.springframework.context.ConfigurableApplicationContext; *

* * @author yangkai.shen - * @date Created in 2018-09-30 23:16 + * @date Created in 2022-08-25 17:03 */ -@SpringBootApplication @Slf4j -public class SpringBootDemoLogbackApplication { +@SpringBootApplication +public class LogbackApplication { public static void main(String[] args) { - ConfigurableApplicationContext context = SpringApplication.run(SpringBootDemoLogbackApplication.class, args); + ConfigurableApplicationContext context = SpringApplication.run(LogbackApplication.class, args); int length = context.getBeanDefinitionNames().length; log.trace("Spring boot启动初始化了 {} 个 Bean", length); log.debug("Spring boot启动初始化了 {} 个 Bean", length); diff --git a/demo-logback/src/main/resources/application.yml b/demo-log/demo-log-logback/src/main/resources/application.yml similarity index 100% rename from demo-logback/src/main/resources/application.yml rename to demo-log/demo-log-logback/src/main/resources/application.yml diff --git a/demo-log/demo-log-logback/src/main/resources/logback-spring.xml b/demo-log/demo-log-logback/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..2220dba --- /dev/null +++ b/demo-log/demo-log-logback/src/main/resources/logback-spring.xml @@ -0,0 +1,80 @@ + + + + // 引入 spring 内置的配置文件,获取一些变量值 + + + + INFO + + + ${CONSOLE_LOG_PATTERN} + UTF-8 + + + + + + + + ERROR + + DENY + + ACCEPT + + + + + + + logs/demo-log/demo-log-logback/info.created_on_%d{yyyy-MM-dd}.part_%i.log + + 90 + + + + + 2MB + + + + + + + ${FILE_LOG_PATTERN} + UTF-8 + + + + + + + Error + + + + + + + logs/demo-log/demo-log-logback/error.created_on_%d{yyyy-MM-dd}.part_%i.log + + 90 + + + 2MB + + + + ${FILE_ERROR_PATTERN} + UTF-8 + + + + + + + + + diff --git a/demo-log/demo-log-logback/src/test/java/com/xkcoding/logback/LogbackApplicationTests.java b/demo-log/demo-log-logback/src/test/java/com/xkcoding/logback/LogbackApplicationTests.java new file mode 100644 index 0000000..43636e6 --- /dev/null +++ b/demo-log/demo-log-logback/src/test/java/com/xkcoding/logback/LogbackApplicationTests.java @@ -0,0 +1,13 @@ +package com.xkcoding.logback; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class LogbackApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/demo-log/pom.xml b/demo-log/pom.xml index a3dafee..b42f2c8 100644 --- a/demo-log/pom.xml +++ b/demo-log/pom.xml @@ -18,4 +18,8 @@ 17 + + demo-log-logback + + diff --git a/demo-logback/.gitignore b/demo-logback/.gitignore deleted file mode 100644 index 82eca33..0000000 --- a/demo-logback/.gitignore +++ /dev/null @@ -1,25 +0,0 @@ -/target/ -!.mvn/wrapper/maven-wrapper.jar - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr - -### NetBeans ### -/nbproject/private/ -/build/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ \ No newline at end of file diff --git a/demo-logback/README.md b/demo-logback/README.md deleted file mode 100644 index d7d6988..0000000 --- a/demo-logback/README.md +++ /dev/null @@ -1,178 +0,0 @@ -# spring-boot-demo-logback - -> 此 demo 主要演示了如何使用 logback 记录程序运行过程中的日志,以及如何配置 logback,可以同时生成控制台日志和文件日志记录,文件日志以日期和大小进行拆分生成。 - -## pom.xml - -```xml - - - 4.0.0 - - spring-boot-demo-logback - 1.0.0-SNAPSHOT - jar - - spring-boot-demo-logback - Demo project for Spring Boot - - - com.xkcoding - spring-boot-demo - 1.0.0-SNAPSHOT - - - - UTF-8 - UTF-8 - 1.8 - - - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-test - test - - - - org.projectlombok - lombok - true - - - - - spring-boot-demo-logback - - - org.springframework.boot - spring-boot-maven-plugin - - - - - -``` - -## SpringBootDemoLogbackApplication.java - -```java -/** - *

- * 启动类 - *

- * - * @author yangkai.shen - * @date Created in 2018-09-30 23:16 - */ -@SpringBootApplication -@Slf4j -public class SpringBootDemoLogbackApplication { - - public static void main(String[] args) { - ConfigurableApplicationContext context = SpringApplication.run(SpringBootDemoLogbackApplication.class, args); - int length = context.getBeanDefinitionNames().length; - log.trace("Spring boot启动初始化了 {} 个 Bean", length); - log.debug("Spring boot启动初始化了 {} 个 Bean", length); - log.info("Spring boot启动初始化了 {} 个 Bean", length); - log.warn("Spring boot启动初始化了 {} 个 Bean", length); - log.error("Spring boot启动初始化了 {} 个 Bean", length); - try { - int i = 0; - int j = 1 / i; - } catch (Exception e) { - log.error("【SpringBootDemoLogbackApplication】启动异常:", e); - } - } -} -``` - -## logback-spring.xml - -```xml - - - - - - INFO - - - %date [%thread] %-5level [%logger{50}] %file:%line - %msg%n - UTF-8 - - - - - - - - ERROR - - DENY - - ACCEPT - - - - - - - logs/spring-boot-demo-logback/info.created_on_%d{yyyy-MM-dd}.part_%i.log - - 90 - - - - - 2MB - - - - - - - %date [%thread] %-5level [%logger{50}] %file:%line - %msg%n - UTF-8 - - - - - - - Error - - - - - - - logs/spring-boot-demo-logback/error.created_on_%d{yyyy-MM-dd}.part_%i.log - - 90 - - - 2MB - - - - %date [%thread] %-5level [%logger{50}] %file:%line - %msg%n - UTF-8 - - - - - - - - - -``` - diff --git a/demo-logback/pom.xml b/demo-logback/pom.xml deleted file mode 100644 index 174a1cd..0000000 --- a/demo-logback/pom.xml +++ /dev/null @@ -1,54 +0,0 @@ - - - 4.0.0 - - demo-logback - 1.0.0-SNAPSHOT - jar - - demo-logback - Demo project for Spring Boot - - - com.xkcoding - spring-boot-demo - 1.0.0-SNAPSHOT - - - - UTF-8 - UTF-8 - 1.8 - - - - - org.springframework.boot - spring-boot-starter-web - - - - org.springframework.boot - spring-boot-starter-test - test - - - - org.projectlombok - lombok - true - - - - - demo-logback - - - org.springframework.boot - spring-boot-maven-plugin - - - - - diff --git a/demo-logback/src/main/resources/logback-spring.xml b/demo-logback/src/main/resources/logback-spring.xml deleted file mode 100644 index dcd48fe..0000000 --- a/demo-logback/src/main/resources/logback-spring.xml +++ /dev/null @@ -1,79 +0,0 @@ - - - - - - - INFO - - - ${CONSOLE_LOG_PATTERN} - UTF-8 - - - - - - - - ERROR - - DENY - - ACCEPT - - - - - - - logs/demo-logback/info.created_on_%d{yyyy-MM-dd}.part_%i.log - - 90 - - - - - 2MB - - - - - - - ${FILE_LOG_PATTERN} - UTF-8 - - - - - - - Error - - - - - - - logs/demo-logback/error.created_on_%d{yyyy-MM-dd}.part_%i.log - - 90 - - - 2MB - - - - ${FILE_ERROR_PATTERN} - UTF-8 - - - - - - - - - diff --git a/demo-logback/src/test/java/com/xkcoding/logback/SpringBootDemoLogbackApplicationTests.java b/demo-logback/src/test/java/com/xkcoding/logback/SpringBootDemoLogbackApplicationTests.java deleted file mode 100644 index 53bbb7f..0000000 --- a/demo-logback/src/test/java/com/xkcoding/logback/SpringBootDemoLogbackApplicationTests.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.xkcoding.logback; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest -public class SpringBootDemoLogbackApplicationTests { - - @Test - public void contextLoads() { - } - -} diff --git a/pom.xml b/pom.xml index 5011f9a..624a26f 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,6 @@ demo-workflow demo-package demo-others -