Skip to content

Commit e772acb

Browse files
committed
logback
1 parent 290c435 commit e772acb

6 files changed

Lines changed: 176 additions & 1 deletion

File tree

log4j2/src/main/resources/log4j2-spring.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- status 指定打印日志的级别 -->
23
<Configuration status="INFO">
34

45
<!--变量配置-->
@@ -15,7 +16,7 @@
1516
<PatternLayout pattern="${LOG_PATTERN}"/>
1617
</Console>
1718

18-
<!-- 常规info -->
19+
<!-- 常规info ,每次大小超过size,则会以 filePattern 的格式重新生成一个文件,-->
1920
<RollingFile name="RollingFileInfo" fileName="${LOG_HOME}/info.log" filePattern="${LOG_HOME}/info-%d{MM-dd-yyyy}-%i.log">
2021
<!-- 只打印info级别日志 -->
2122
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>

logback/pom.xml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.1.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>org.ylc.note</groupId>
12+
<artifactId>logback</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>logback</name>
15+
<description>Demo project for Spring Boot use logback</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-test</artifactId>
30+
<scope>test</scope>
31+
<exclusions>
32+
<exclusion>
33+
<groupId>org.junit.vintage</groupId>
34+
<artifactId>junit-vintage-engine</artifactId>
35+
</exclusion>
36+
</exclusions>
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+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.ylc.note.logback;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class LogbackApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(LogbackApplication.class, args);
11+
}
12+
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration debug="false">
3+
<!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
4+
<property name="LOG_HOME" value="./log"/>
5+
6+
<!-- Console 输出设置 -->
7+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
8+
<withJansi>true</withJansi>
9+
<encoder>
10+
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %blue(%X{user}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{50}) - %cyan(%msg%n)
11+
</pattern>
12+
<charset>utf8</charset>
13+
</encoder>
14+
</appender>
15+
16+
<!-- 定时任务类设置单独日志文件 -->
17+
<appender name="SCHEDULE-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
18+
<!--没有File 文件不会按修改-->
19+
<File>${LOG_HOME}/schedule.log</File>
20+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
21+
<!--日志文件输出的文件名-->
22+
<FileNamePattern>${LOG_HOME}/schedule.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
23+
<!--日志文件保留天数-->
24+
<MaxHistory>7</MaxHistory>
25+
<!--日志文件最大的大小-->
26+
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
27+
<maxFileSize>5MB</maxFileSize>
28+
</timeBasedFileNamingAndTriggeringPolicy>
29+
</rollingPolicy>
30+
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
31+
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
32+
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %X{user} [%thread] %-5level %logger{50} - %msg%n</pattern>
33+
</encoder>
34+
<!-- 打印INFO及以上日志-->
35+
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
36+
<level>INFO</level>
37+
</filter>
38+
</appender>
39+
40+
<!-- 默认生成日志文件 -->
41+
<appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
42+
<!--没有File 文件不会按修改-->
43+
<File>${LOG_HOME}/info.log</File>
44+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
45+
<!--日志文件输出的文件名-->
46+
<FileNamePattern>${LOG_HOME}/info.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
47+
<!--日志文件保留天数-->
48+
<MaxHistory>7</MaxHistory>
49+
<!--日志文件最大的大小-->
50+
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
51+
<maxFileSize>5MB</maxFileSize>
52+
</timeBasedFileNamingAndTriggeringPolicy>
53+
</rollingPolicy>
54+
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
55+
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
56+
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %X{user} [%thread] %-5level %logger{50} - %msg%n</pattern>
57+
</encoder>
58+
<!-- 只打印INFO日志 -->
59+
<filter class="ch.qos.logback.classic.filter.LevelFilter">
60+
<level>INFO</level>
61+
<onMatch>ACCEPT</onMatch>
62+
<onMismatch>DENY</onMismatch>
63+
</filter>
64+
</appender>
65+
66+
<appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
67+
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
68+
<level>ERROR</level>
69+
</filter>
70+
<File>${LOG_HOME}/error.log</File>
71+
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
72+
<!--日志文件输出的文件名-->
73+
<FileNamePattern>${LOG_HOME}/error.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
74+
<!--日志文件保留天数-->
75+
<MaxHistory>30</MaxHistory>
76+
<!--日志文件最大的大小-->
77+
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
78+
<maxFileSize>2MB</maxFileSize>
79+
</timeBasedFileNamingAndTriggeringPolicy>
80+
</rollingPolicy>
81+
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
82+
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
83+
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %X{user} [%thread] %-5level %logger{50} - %msg%n</pattern>
84+
</encoder>
85+
</appender>
86+
87+
<!--这里的name和业务类中的getLogger中的字符串是一样的 additivity="false" 禁止这里的内容向上传递,否则会同时显示在默认日志中-->
88+
<logger name="schedule" level="INFO" additivity="false">
89+
<appender-ref ref="SCHEDULE-APPENDER"/>
90+
</logger>
91+
92+
<!-- 日志输出级别 -->
93+
<root level="INFO">
94+
<appender-ref ref="STDOUT"/>
95+
<appender-ref ref="INFO"/>
96+
<appender-ref ref="ERROR"/>
97+
</root>
98+
</configuration>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.ylc.note.logback;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
class LogbackApplicationTests {
8+
9+
@Test
10+
void contextLoads() {
11+
}
12+
13+
}

0 commit comments

Comments
 (0)