SpringBoot+Slf4j+Log4j2+mybatis 日志整合

注:本次使用的是springboot4,具体版本为springboot4.0.6 GA (正式版)

Slf4j为外部调用提供了统一的日志接口,其内部实现取决于使用的日志框架。Slf4j是门面模式的优秀实践。

目的是要将这四部分进行整合,即希望日志都通过Log4j2来进行打印/记录。

1)引入依赖(不知道这几个依赖是否有多余的,这四个一起是能正常实现,少一个多一个没验证过):

复制代码
<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>2.0.18</version>
			<scope>compile</scope>
		</dependency>
		<!-- Source: https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j2-impl -->
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-slf4j2-impl</artifactId>
			<version>2.26.0</version>
			<scope>compile</scope>
			<exclusions>
				<exclusion>
					<groupId>org.apache.logging.log4j</groupId>
					<artifactId>log4j-api</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.apache.logging.log4j</groupId>
					<artifactId>log4j-core</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- Source: https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.26.0</version>
			<exclusions>
				<exclusion>
					<groupId>org.apache.logging.log4j</groupId>
					<artifactId>log4j-api</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- Source: https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>2.26.0</version>
			<scope>compile</scope>
		</dependency>

上面依赖包括:slf4j-api、log4j-slf4j2-impl、log4j-api、log4j-core

需要注意的是,要排除多余依赖,不一定要按下面写的来排除,上面四个依赖只要是唯一的就行。我的依赖排除如下:

log4j-slf4j2-impl依赖中,排除log4j-api和log4j-core;

log4j-core依赖中,排除log4j-api;

另外,spring-boot-starter依赖中,要排除spring-boot-starter-logging。

ps:还可以使用另一种依赖 spring-boot-starter-log4j2 代替,具体实操没有,但理论可行。

2)mybatis-config.xml配置-实现sql日志通过log4j2的打印

复制代码
    <settings>
        <setting name="logImpl" value="SLF4J"/>
    </settings>

3)log4j2.xml配置-精细化控制log4j2的日志使用

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
    <properties>
        <property name="LOG_HOME">D:/logs</property>
        <property name="FILE_NAME">mylog</property>
        <property name="log.sql.level">debug</property>
    </properties>


    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] LOG4J2 %-5level %l - %msg%n"/>
        </Console>

        <RollingRandomAccessFile name="RollingRandomAccessFile"
                                 fileName="C:\Users\aaa\Desktop\testlog/log4j2-config.xml.log"
                                 filePattern="C:\Users\aaa\Desktop\testlog/%d{yyyy-MM-dd}/log4j2-config.xml-%i.log">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] LOG4J2 %-5level %l - %msg%n"/>
        <Policies>
            <TimeBasedTriggeringPolicy interval="1"/>
            <SizeBasedTriggeringPolicy size="10 MB"/>
        </Policies>
        <DefaultRolloverStrategy max="20"/>
    </RollingRandomAccessFile>
</Appenders>

<Loggers>
    <Root level="info">
        <AppenderRef ref="Console"/>
        <AppenderRef ref="RollingRandomAccessFile"/>
    </Root>

    <Logger name="com.example.dao.EmpDao" level="${log.sql.level}" additivity="false">
        <AppenderRef ref="Console"/>
    </Logger>
</Loggers>
</Configuration>

经过以上三个步骤,即可实现log4j2日志框架的使用。

相关推荐
露天赏雪9 小时前
第3篇:Sentinel熔断降级实战:大模型接口抖动时的服务兜底方案
java·开发语言·人工智能·spring boot·spring·servlet·sentinel
蜗牛87516 小时前
为 Spring Boot 团队做一个 Elasticsearch 查询工作台:六种模式、语义补全与浏览器直连
spring boot
养军博客19 小时前
Spring Boot 3.x + Spring Security 6.x 整合 JWT 实战:jjwt 与 java-jwt 两种工具库详解
java·spring boot·springsecurity
txxzjmzlh1 天前
SpringBoot 统⼀功能处理
java·spring boot·后端
秋天的怀念_David2 天前
算法一千零一夜 · 第二十六夜 协议的母语
java·spring boot·算法
余槐i2 天前
MySQL 8.0 联合索引走错:为什么加了索引查询反而从 200ms 变成 8s
java·spring boot·mysql·docker·性能优化
CoderYanger2 天前
Java EE 进阶:8.MyBatis 操作数据库(入门)(第一弹)
java·数据库·spring·面试·职场和发展·java-ee·mybatis
nice_lcj5202 天前
【从零理解 Spring AI · 03】Spring AI 在 Spring 生态里的位置
java·spring boot·大模型·spring ai·ai应用开发
AI源码2 天前
旅游推荐管理系统
spring boot
aiks_yuan3 天前
Spring Boot SSE 流式输出变成一次性返回怎么办:大模型打字机效果失效的 5 个原因(Spring Boot 4.1.2 实测)
spring boot