From abb1780eab861ba6513162612223ad2871e56634 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Sun, 30 Sep 2018 23:26:01 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20spring-boot-demo-logback=20?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-boot-demo-logback/.gitignore | 25 +++ spring-boot-demo-logback/README.md | 184 ++++++++++++++++++ spring-boot-demo-logback/pom.xml | 55 ++++++ .../SpringBootDemoLogbackApplication.java | 40 ++++ .../src/main/resources/application.yml | 4 + .../src/main/resources/logback-spring.xml | 77 ++++++++ ...SpringBootDemoLogbackApplicationTests.java | 16 ++ 7 files changed, 401 insertions(+) create mode 100644 spring-boot-demo-logback/.gitignore create mode 100644 spring-boot-demo-logback/README.md create mode 100644 spring-boot-demo-logback/pom.xml create mode 100644 spring-boot-demo-logback/src/main/java/com/xkcoding/logback/SpringBootDemoLogbackApplication.java create mode 100644 spring-boot-demo-logback/src/main/resources/application.yml create mode 100644 spring-boot-demo-logback/src/main/resources/logback-spring.xml create mode 100644 spring-boot-demo-logback/src/test/java/com/xkcoding/logback/SpringBootDemoLogbackApplicationTests.java diff --git a/spring-boot-demo-logback/.gitignore b/spring-boot-demo-logback/.gitignore new file mode 100644 index 0000000..82eca33 --- /dev/null +++ b/spring-boot-demo-logback/.gitignore @@ -0,0 +1,25 @@ +/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/spring-boot-demo-logback/README.md b/spring-boot-demo-logback/README.md new file mode 100644 index 0000000..d5d0e6b --- /dev/null +++ b/spring-boot-demo-logback/README.md @@ -0,0 +1,184 @@ +# spring-boot-demo-logback + +> 此 demo 主要演示了如何使用 logback 记录程序运行过程中的日志,以及如何配置 logback,可以同时生成控制台日志和文件日志记录,文件日志以日期和大小进行拆分生成。 + +## pom.xml + +```xml + + + 4.0.0 + + com.xkcoding + spring-boot-demo-logback + 0.0.1-SNAPSHOT + jar + + spring-boot-demo-logback + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 2.0.5.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + + + + + spring-boot-demo-logback + + + org.springframework.boot + spring-boot-maven-plugin + + + + + +``` + +## SpringBootDemoLogbackApplication.java + +```java +/** + *

+ * 启动类 + *

+ * + * @package: com.xkcoding.logback + * @description: 启动类 + * @author: yangkai.shen + * @date: Created in 2018/9/30 11:16 PM + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@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/spring-boot-demo-logback/pom.xml b/spring-boot-demo-logback/pom.xml new file mode 100644 index 0000000..a4f9eb8 --- /dev/null +++ b/spring-boot-demo-logback/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + + com.xkcoding + spring-boot-demo-logback + 0.0.1-SNAPSHOT + jar + + spring-boot-demo-logback + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 2.0.5.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.projectlombok + lombok + + + + + spring-boot-demo-logback + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot-demo-logback/src/main/java/com/xkcoding/logback/SpringBootDemoLogbackApplication.java b/spring-boot-demo-logback/src/main/java/com/xkcoding/logback/SpringBootDemoLogbackApplication.java new file mode 100644 index 0000000..43ed673 --- /dev/null +++ b/spring-boot-demo-logback/src/main/java/com/xkcoding/logback/SpringBootDemoLogbackApplication.java @@ -0,0 +1,40 @@ +package com.xkcoding.logback; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ConfigurableApplicationContext; + +/** + *

+ * 启动类 + *

+ * + * @package: com.xkcoding.logback + * @description: 启动类 + * @author: yangkai.shen + * @date: Created in 2018/9/30 11:16 PM + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@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); + } + } +} diff --git a/spring-boot-demo-logback/src/main/resources/application.yml b/spring-boot-demo-logback/src/main/resources/application.yml new file mode 100644 index 0000000..af5002c --- /dev/null +++ b/spring-boot-demo-logback/src/main/resources/application.yml @@ -0,0 +1,4 @@ +server: + port: 8080 + servlet: + context-path: /demo diff --git a/spring-boot-demo-logback/src/main/resources/logback-spring.xml b/spring-boot-demo-logback/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..2c43a56 --- /dev/null +++ b/spring-boot-demo-logback/src/main/resources/logback-spring.xml @@ -0,0 +1,77 @@ + + + + + + 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 + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-demo-logback/src/test/java/com/xkcoding/logback/SpringBootDemoLogbackApplicationTests.java b/spring-boot-demo-logback/src/test/java/com/xkcoding/logback/SpringBootDemoLogbackApplicationTests.java new file mode 100644 index 0000000..b387f30 --- /dev/null +++ b/spring-boot-demo-logback/src/test/java/com/xkcoding/logback/SpringBootDemoLogbackApplicationTests.java @@ -0,0 +1,16 @@ +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() { + } + +}