Skip to content

Commit 6bda91c

Browse files
committed
第四篇 : SpringBoot日志
1 parent 1295695 commit 6bda91c

8 files changed

Lines changed: 216 additions & 0 deletions

File tree

springboot-logging/.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
46.5 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip

springboot-logging/pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.gf</groupId>
7+
<artifactId>springboot-logging</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>springboot-logging</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.5.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
</dependencies>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-maven-plugin</artifactId>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
49+
50+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.gf;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringbootLoggingApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringbootLoggingApplication.class, args);
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
logging.level.com.gf=trace
2+
3+
#logging.path 和 logging.path 是冲突设置 ,两者都设置时只有 logging.path 起作用
4+
#指定文件名,就在项目下生产springboot.log日志
5+
#logging.file=springboot.log
6+
7+
# 在当前磁盘路径下创建spring文件夹和里面的log文件夹;使用spring.log 作为默认文件
8+
logging.path=/Users/huanchu/Documents/spring/log
9+
10+
# 指定控制台输出的日志格式
11+
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} -%msg%n
12+
13+
# 指定文件中日志输出的格式
14+
logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} === %msg%n
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
scan:当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true。
4+
scanPeriod:设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒当scan为true时,此属性生效。默认的时间间隔为1分钟。
5+
debug:当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。
6+
-->
7+
<configuration scan="true" scanPeriod="10 seconds" debug="false">
8+
<!-- 定义日志的根目录 -->
9+
<property name="LOG_HOME" value="log" />
10+
<!-- 定义日志文件名称 -->
11+
<property name="appName" value="springboot-logging"></property>
12+
<!-- ch.qos.logback.core.ConsoleAppender 表示控制台输出 -->
13+
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
14+
<!--
15+
日志输出格式:
16+
%d表示日期时间,
17+
%thread表示线程名,
18+
%-5level:级别从左显示5个字符宽度
19+
%logger{50} 表示logger名字最长50个字符,否则按照句点分割。
20+
%msg:日志消息,
21+
%n是换行符
22+
-->
23+
<layout class="ch.qos.logback.classic.PatternLayout">
24+
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
25+
</layout>
26+
</appender>
27+
28+
<!-- 滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件 -->
29+
<appender name="appLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
30+
<!-- 指定日志文件的名称 -->
31+
<file>${LOG_HOME}/${appName}.log</file>
32+
<!--
33+
当发生滚动时,决定 RollingFileAppender 的行为,涉及文件移动和重命名
34+
TimeBasedRollingPolicy: 最常用的滚动策略,它根据时间来制定滚动策略,既负责滚动也负责出发滚动。
35+
-->
36+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
37+
<!--
38+
滚动时产生的文件的存放位置及文件名称 %d{yyyy-MM-dd}:按天进行日志滚动
39+
%i:当文件大小超过maxFileSize时,按照i进行文件滚动
40+
-->
41+
<fileNamePattern>${LOG_HOME}/${appName}-%d{yyyy-MM-dd}-%i.log</fileNamePattern>
42+
<!--
43+
可选节点,控制保留的归档文件的最大数量,超出数量就删除旧文件。假设设置每天滚动,
44+
且maxHistory是365,则只保存最近365天的文件,删除之前的旧文件。注意,删除旧文件是,
45+
那些为了归档而创建的目录也会被删除。
46+
-->
47+
<MaxHistory>365</MaxHistory>
48+
<!--
49+
当日志文件超过maxFileSize指定的大小是,根据上面提到的%i进行日志文件滚动 注意此处配置SizeBasedTriggeringPolicy是无法实现按文件大小进行滚动的,必须配置timeBasedFileNamingAndTriggeringPolicy
50+
-->
51+
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
52+
<maxFileSize>100MB</maxFileSize>
53+
</timeBasedFileNamingAndTriggeringPolicy>
54+
</rollingPolicy>
55+
<!-- 日志输出格式: -->
56+
<layout class="ch.qos.logback.classic.PatternLayout">
57+
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [ %thread ] - [ %-5level ] [ %logger{50} : %line ] - %msg%n</pattern>
58+
</layout>
59+
60+
</appender>
61+
62+
<!--
63+
logger主要用于存放日志对象,也可以定义日志类型、级别
64+
name:表示匹配的logger类型前缀,也就是包的前半部分
65+
level:要记录的日志级别,包括 TRACE < DEBUG < INFO < WARN < ERROR
66+
additivity:作用在于children-logger是否使用 rootLogger配置的appender进行输出,
67+
false:表示只用当前logger的appender-ref,true:
68+
表示当前logger的appender-ref和rootLogger的appender-ref都有效
69+
-->
70+
<!--hibernate logger-->
71+
<logger name="com.gf" level="debug" />
72+
<!-- Spring framework logger -->
73+
<!--<logger name="org.springframework" level="debug" additivity="false"></logger>-->
74+
75+
<!--
76+
root与logger是父子关系,没有特别定义则默认为root,任何一个类只会和一个logger对应,
77+
要么是定义的logger,要么是root,判断的关键在于找到这个logger,然后判断这个logger的appender和level。
78+
-->
79+
<root level="info">
80+
<appender-ref ref="stdout" />
81+
<appender-ref ref="appLogAppender" />
82+
</root>
83+
</configuration>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.gf;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.context.junit4.SpringRunner;
9+
10+
@RunWith(SpringRunner.class)
11+
@SpringBootTest
12+
public class SpringbootLoggingApplicationTests {
13+
14+
//日志记录器
15+
Logger logger = LoggerFactory.getLogger( this.getClass() );
16+
17+
@Test
18+
public void contextLoads() {
19+
20+
//日志级别(由高到底):trace < debug < info < warn < error
21+
//可以调整输出的日志级别,日志只会在这个级别以后的高级别生效
22+
logger.trace( "这是trace日志..." );
23+
logger.debug( "这是debug日志..." );
24+
//SpringBoot默认给我们使用的是info级别,没有指定级别就使用SpringBoot默认的规定
25+
logger.info( "这是info日志..." );
26+
logger.warn( "这是warn日志..." );
27+
logger.error( "这是error日志..." );
28+
29+
}
30+
31+
}

0 commit comments

Comments
 (0)