〇、参考资料
1、springboot整合log4j全过程详解
https://blog.csdn.net/m0_60845963/article/details/123307232
2、Spring Boot 全局排除 spring-boot-starter-logging 依赖
https://blog.csdn.net/weixin_40690761/article/details/117511699
一、项目概述
1、项目框架
2、技术栈
Spring Boot+log4j+SpringBootTest
二、依赖管理
1、导入Spring依赖
<?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,则spring boot所有的版本就可以忽略(version标签)
继承自一个spring-boot-starter-parent,引入spring-boot-starter-xxx时就不需要显示的指定具体的版本号
parent的基本功能有:
1、定义了Java的编译版本
2、使用UTF-8格式编码
3、parent继承自spring-boot-dependencies,这个里面定义了依赖的版本
4、执行打包操作的配置
5、自动化的资源过滤
6、自动化的插件配置
参考地址:https://blog.csdn.net/Guesshat/article/details/113805914
parent依赖简要分析(包括查看源码):https://www.jianshu.com/p/55959a625469
-->
<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.boulderaitech</groupId>
<artifactId>kettle_processor</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--kettle内部需要的日志记录框架?-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!--spring boot的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</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.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--全局排除spring-boot-starter-logging内的所有依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--引入spring-boot的log4j依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</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>
参考:https://blog.csdn.net/weixin_40690761/article/details/117511699
三、配置
1、配置application.properties
# Spring Boot端口
server.port=9088
# spring数据源配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.password=qaz123
spring.datasource.username=root
spring.datasource.url=jdbc:mysql://192.168.40.111:3306/visualization?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.thymeleaf.prefix=classpath:/templates/
# 配置swagger
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
# log4j的配置
logging.config=classpath:log4j.properties
2、配置log4j.properties
# 日志输出优先级???
log4j.rootLogger=info,error,info,warn,stdout
##############打印到平台的日志##################
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#输出的信息格式
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}-[%p:%X{traceID}]-(%C:%L) %m%n
##############WARN打印的文件的日志##################
log4j.appender.warn=org.apache.log4j.DailyRollingFileAppender
log4j.appender.tracking.Encoding=utf-8
log4j.appender.warn.layout=org.apache.log4j.PatternLayout
log4j.appender.warn.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}-[%p]-(%C:%L) %m%n
#输出文件位置【注意:以项目为根目录】
log4j.appender.warn.File=logs/warn/warn.log
log4j.appender.warn.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.warn.Threshold=WARN
##############ERROR打印的文件的日志##################
log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}-[%p]-(%C:%L) %m%n
log4j.appender.error.File=logs/error/error.log
log4j.appender.error.DatePattern='.'yyyy-MM-dd-HH
log4j.appender.error.Threshold=ERROR
##############INFO打印的文件的日志##################cn.com.bluemoon.bd.service.its.deg.common.Log4jMyAppender
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}-[%p:%X{traceID}]-(%C:%L) %m%n
#这个相对路径是从当前配置文件的路径开始的
log4j.appender.info.File=logs/info/info.log
#DataPattern规定每小时产生一个日志文档,当前小时的文日志会放在名字为info的文档里,以前的日志放在lofo.yyyy-MM-dd-HH的文档里。
log4j.appender.info.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.info.ImmediateFlush=true
四、测试
1、编写测试类
package com.boulderaitech.demo;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class Log2FileTests {
Logger log = Logger.getLogger(Log2FileTests.class);
@Test
public void testLog() {
//记录debug级别的信息
log.debug(">> this is a debug message-2022年12月2日");
//记录info级别的信息
log.info(">>this is an info message");
//记录error级别的信息
log.error(">>this is an error message");
//记录warn级别的信息
log.warn("hello");
}
}
验证:
2、项目启动测试
待更新:log4j/commons-logging/slf4j各种日志框架比较、日志级别、配置介绍(带日期的输出)
标签:info,Spring,boot,appender,Boot,spring,org,log4j From: https://www.cnblogs.com/liujinhui/p/16943238.html