引用参考来源,请详细参考
https://dalin.blog.csdn.net/article/details/109475445
https://blog.csdn.net/guanshun1991/article/details/81773290
https://blog.csdn.net/mu_wind/article/details/99830829
https://blog.csdn.net/qq_27706119/article/details/104977666
1、新建项目
(1)
(2)
(3)
2、在pom.xml文件中添加相关依赖。
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
整体的pom.xml如下
<?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 https://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.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xachen</groupId>
<artifactId>chenslf4jlogback</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>chenslf4jlogback</name>
<description>Demo project for Test for Slf4j Logback</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
2、添加.xml。路径如下
src/main/resources/logback.xml
内容如下
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--日志文件主目录:这里${user.home}为当前服务器用户主目录-->
<property name="LOG_HOME" value="${user.home}/log"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
<charset>utf-8</charset>
</encoder>
<!-- <file>log/output.log</file>-->
<file>${LOG_HOME}/output.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<!-- <fileNamePattern>log/output.log.%i</fileNamePattern>-->
<fileNamePattern>${LOG_HOME}/output.log.%i</fileNamePattern>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>1MB</MaxFileSize>
</triggeringPolicy>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
3、配置文件application.yml中的设置
logging:
config: classpath:logback.xml
# level:
## com.xachen.chenslf4jlogback: trace
# com.xachen.chenslf4jlogback: error
logging.config 用来指定项目启动的时候,读取哪个配置文件,这里指定的日志配置文件是根路径下的 logback.xml 文件。关于日志的相关配置信息,都放在了 logback.xml 文件中。
logging.level 用来指定具体的 Mapper 中日志的输出级别,上面的配置表示 com.xachen.chenslf4jlogback包下的输出级别为 Trace。开发时设置成 trace 方便定位问题,在生产环境上,将这个日志级别再设置成 error 级别即可。
常用的日志级别按照从高到低依次为:ERROR、WARN、INFO、DEBUG。
4、测试
(1)在test中测试
package com.xachen.chenslf4jlogback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.UnsupportedEncodingException;
@SpringBootTest
class Chenslf4jlogbackApplicationTests {
static final Logger logger = LoggerFactory.getLogger(Chenslf4jlogbackApplicationTests.class);
@Test
void contextLoads() {
}
@Test
void chentesslf4jlogback()
{
logger.info("Start process {}...", Chenslf4jlogbackApplicationTests.class.getName());
try {
"".getBytes("invalidCharsetName");
} catch (UnsupportedEncodingException e) {
// TODO: 使用logger.error(String, Throwable)打印异常
}
logger.info("Process end.");
}
}
(2)在main中测试,注意可以使用lombok简洁的方式,用@Slf4j注解后,直接使用log
只要引用import lombok.extern.slf4j.Slf4j;
package com.xachen.chenslf4jlogback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@Slf4j
public class Chenslf4jlogbackApplication {
public static void main(String[] args) {
SpringApplication.run(Chenslf4jlogbackApplication.class, args);
log.info("chentest of lombok");
log.warn("now start");
log.debug("chen debug");
}
}
5、查看日志,
(1)windows下测试
去用户-->admin-->log下面查看
<!--日志文件主目录:这里${user.home}为当前服务器用户主目录-->
<property name="LOG_HOME" value="${user.home}/log"/>
(2)ubuntu下测试,修改下路径
<!--日志文件主目录:这里${user.home}为当前服务器用户主目录-->
<!-- <property name="LOG_HOME" value="${user.home}/log"/>-->
<!--日志文件主目录:ubuntu下测试目录-->
<property name="LOG_HOME" value="/chen/log_test_slf4j"/>
看到了日志
6、日志等级说明:
常用的日志级别按照从高到低依次为:ERROR、WARN、INFO、DEBUG。
上面的基础等级设定了info,所以debug的信息log.debug("chen debug");没有显示出来
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>