日志的目的:
为了方便日后的运维,快速定位BUG,错误信息等等
日志算是一个救命稻草
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.8</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.jmc</groupId> <artifactId>tco</artifactId> <version>0.0.1-SNAPSHOT</version> <name>tco</name> <description>tco project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </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> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.18</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.13</version> </dependency> <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
application.yml的日志配置信息
logging: file: name: # 配置日志文件名称 log-info.log path: # 配置日志文件所在路径 ./logs level: # 设置日志输出级别 root : info # 根类下 info输出级别 # 指定项目路径下级别 com.jmc.tco : debug org.springframework : warn logback: rollingpolicy: max-file-size: 50MB # 设置单个日志文件大小上限 max-history: 30 # 设置日志保存时长 (天)
有Lombok支持,可以直接对类打这个【@Slf4j】注解即可
import lombok.extern.slf4j.Slf4j;
代替写入的方法:
private static final Logger logger = LoggerFactory.getLogger(需要日志输出的类.class);
关于输出问题,没有设置任何的路径,只有文件名称
用Jar包运行发现,日志文件会输出在jar包所在的目录下面,同理可以推算出目录也是同目录下创建