feat:增加logback配置
This commit is contained in:
parent
75019acd7a
commit
f48904dba5
29
spring-boot/log-demo/.gitignore
vendored
Normal file
29
spring-boot/log-demo/.gitignore
vendored
Normal file
@ -0,0 +1,29 @@
|
||||
HELP.md
|
||||
/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/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
/build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
43
spring-boot/log-demo/pom.xml
Normal file
43
spring-boot/log-demo/pom.xml
Normal file
@ -0,0 +1,43 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.1.3.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>log_demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>log_demo</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -0,0 +1,13 @@
|
||||
package com.example.log_demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class LogDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(LogDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.example.log_demo.log1;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/3/20 14:05
|
||||
*/
|
||||
@Service
|
||||
public class Log1Service {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(Log1Service.class);
|
||||
|
||||
public void errorLog(){
|
||||
logger.error("这是一条测试错误日志");
|
||||
}
|
||||
|
||||
|
||||
public void warnLog(){
|
||||
logger.warn("这是一条测试警告日志");
|
||||
}
|
||||
|
||||
public void infoLog(){
|
||||
logger.info("这是一条测试info日志");
|
||||
}
|
||||
|
||||
|
||||
public void debugLog(){
|
||||
logger.debug("这是一条测试debug日志");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.example.log_demo.log2;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 类功能简述:
|
||||
* 类功能详述:
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2019/3/20 14:05
|
||||
*/
|
||||
@Service
|
||||
public class Log2Service {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(Log2Service.class);
|
||||
|
||||
public void errorLog() {
|
||||
logger.error("这是一条测试错误日志");
|
||||
}
|
||||
|
||||
|
||||
public void warnLog() {
|
||||
logger.warn("这是一条测试警告日志");
|
||||
}
|
||||
|
||||
public void infoLog() {
|
||||
logger.info("这是一条测试info日志");
|
||||
}
|
||||
|
||||
|
||||
public void debugLog() {
|
||||
logger.debug("这是一条测试debug日志");
|
||||
}
|
||||
|
||||
}
|
11
spring-boot/log-demo/src/main/resources/application.yml
Normal file
11
spring-boot/log-demo/src/main/resources/application.yml
Normal file
@ -0,0 +1,11 @@
|
||||
#logging:
|
||||
## file: out.log
|
||||
# path: ./log/
|
||||
# file:
|
||||
# max-size: 10MB
|
||||
# level:
|
||||
# root: info
|
||||
# com.example.log_demo.log1: warn
|
||||
spring:
|
||||
profiles:
|
||||
active: dev
|
101
spring-boot/log-demo/src/main/resources/logback-spring.xml
Normal file
101
spring-boot/log-demo/src/main/resources/logback-spring.xml
Normal file
@ -0,0 +1,101 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration debug="false">
|
||||
<!--设置存储路径变量-->
|
||||
<property name="LOG_HOME" value="./log"/>
|
||||
|
||||
<!--控制台输出appender-->
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<!--设置输出格式-->
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
|
||||
<!--设置编码-->
|
||||
<charset>UTF-8</charset>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!--文件输出,时间窗口滚动-->
|
||||
<appender name="timeFileOutput" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<!--日志名,指定最新的文件名,其他文件名使用FileNamePattern -->
|
||||
<File>${LOG_HOME}/timeFile/out.log</File>
|
||||
<!--文件滚动模式-->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!--日志文件输出的文件名,使用gz压缩-->
|
||||
<FileNamePattern>${LOG_HOME}/timeFile/info.%d{yyyy-MM-dd}.%i.log.gz</FileNamePattern>
|
||||
<!--日志文件保留天数-->
|
||||
<MaxHistory>30</MaxHistory>
|
||||
<!--按大小分割同一天的-->
|
||||
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
|
||||
<maxFileSize>10MB</maxFileSize>
|
||||
</timeBasedFileNamingAndTriggeringPolicy>
|
||||
</rollingPolicy>
|
||||
|
||||
<!--输出格式-->
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
|
||||
<!--设置编码-->
|
||||
<charset>UTF-8</charset>
|
||||
</encoder>
|
||||
<!--在root指定的级别之上再次进行过滤,输出大于等于level,可通过onMatch和onMisMatch来确定只输出某个级别的 -->
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>INFO</level>
|
||||
<!--接受匹配-->
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<!--拒绝不匹配的-->
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<!--接受log1下的-->
|
||||
<appender name="log1FileOutput" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<!--日志名,指定最新的文件名,其他文件名使用FileNamePattern -->
|
||||
<File>${LOG_HOME}/log1/out.log</File>
|
||||
<!--文件滚动模式-->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!--日志文件输出的文件名,使用gz压缩-->
|
||||
<FileNamePattern>${LOG_HOME}/log1/info.%d{yyyy-MM-dd}.%i.log.gz</FileNamePattern>
|
||||
<!--日志文件保留天数-->
|
||||
<MaxHistory>30</MaxHistory>
|
||||
<!--按大小分割同一天的-->
|
||||
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
|
||||
<maxFileSize>10MB</maxFileSize>
|
||||
</timeBasedFileNamingAndTriggeringPolicy>
|
||||
</rollingPolicy>
|
||||
|
||||
<!--输出格式-->
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
|
||||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
|
||||
<!--设置编码-->
|
||||
<charset>UTF-8</charset>
|
||||
</encoder>
|
||||
<!--在root指定的级别之上再次进行过滤,输出大于等于level,可通过onMatch和onMisMatch来确定只输出某个级别的 -->
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>INFO</level>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<!--生产环节中打印到production-->
|
||||
<springProfile name="production">
|
||||
<!--给log1指定文件输出-->
|
||||
<logger name="com.example.log_demo.log1" level="warn" additivity="false">
|
||||
<appender-ref ref="log1FileOutput"/>
|
||||
</logger>
|
||||
</springProfile>
|
||||
|
||||
<!--dev/gg环境中打印到控制台-->
|
||||
<springProfile name="dev,gg">
|
||||
<!--给log1指定文件输出-->
|
||||
<logger name="com.example.log_demo.log1" level="warn" additivity="false">
|
||||
<appender-ref ref="console"/>
|
||||
</logger>
|
||||
</springProfile>
|
||||
|
||||
<!--指定基础的日志输出级别-->
|
||||
<root level="INFO">
|
||||
<!--appender将会添加到这个loger-->
|
||||
<appender-ref ref="console"/>
|
||||
<appender-ref ref="timeFileOutput"/>
|
||||
</root>
|
||||
</configuration>
|
@ -0,0 +1,34 @@
|
||||
package com.example.log_demo;
|
||||
|
||||
import com.example.log_demo.log1.Log1Service;
|
||||
import com.example.log_demo.log2.Log2Service;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class LogDemoApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private Log1Service log1Service;
|
||||
|
||||
@Autowired
|
||||
private Log2Service log2Service;
|
||||
|
||||
@Test
|
||||
public void log() {
|
||||
log1Service.debugLog();
|
||||
log1Service.infoLog();
|
||||
log1Service.warnLog();
|
||||
log1Service.errorLog();
|
||||
|
||||
|
||||
log2Service.debugLog();
|
||||
log2Service.infoLog();
|
||||
log2Service.warnLog();
|
||||
log2Service.errorLog();
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user