springboot + log4j2 控制台日志打印 sql 问题

本文介绍如何在SpringBoot项目中配置Log4j2替代默认的Logback,实现更灵活的日志管理,包括排除不必要的依赖,配置log4j2.xml以控制日志输出级别,以及如何在MyBatis中使用Log4j2打印SQL语句。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

springboot 自带 日志打印  logback  如果使用log4j2 可能回抛出

Logging system failed to initialize using configuration from 'classpath:log4j2.xml'
java.lang.IllegalStateException: Logback configuration error detected: 

解决办法也是百度来的 但是忘了是那个网址了 只让你 忽略一个

配置xml 时候    spring-boot-starter-web 也要忽略jar包  mybatis-spring-boot-starter 也要忽略jar包

说还有 加载顺序问题 如果在头忽略就可以下面不写  我没配置成功

pom.xml  部分代码如下

<!-- spring boot的相关启动 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<!-- 去掉默认的,不然log4j2没效果 -->
			<!--去除springboot对logback的依赖 -->
			<exclusions>
				<exclusion>
					<artifactId>logback-access</artifactId>
					<groupId>ch.qos.logback</groupId>
				</exclusion>
				<exclusion>
					<artifactId>logback-core</artifactId>
					<groupId>ch.qos.logback</groupId>
				</exclusion>
				<exclusion>
					<artifactId>logback-classic</artifactId>
					<groupId>ch.qos.logback</groupId>
				</exclusion>
			</exclusions>
		</dependency>

		<!-- 热部署 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
			<scope>true</scope>
		</dependency>
		<!-- spring boot整合mybatis -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<!-- 打印日志   log4j2  需要   是排除多余的jar包 springboot 默认使用logback     log4j引入时候需要排除    -->
			  <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
			<version>1.3.0</version>
		</dependency>
		
		
		<!-- log4j -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-log4j2</artifactId>
		</dependency>

配置log4j2  控制台打印sql语句  

src/main/resource 下创建 (log4j2.xml 配置 不是全的 我就是想看个sql 不想要那么多配置  配置了好多都失败了 也只配置了properties文件  不是好使 现在不清楚到底那个是好使了 就全都配置)

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO" monitorInterval="1800">
    <appenders>
        <Console name="consolePrint" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
    </appenders>
 
    <loggers>
        <!-- 将业务dao接口填写进去,并用控制台输出即可 -->
        <logger name="example.moudels.*.mapper" level="DEBUG" additivity="false">
            <appender-ref ref="consolePrint"/>
        </logger>
 
        <root level="info">
            <appender-ref ref="consolePrint" />
        </root>
    </loggers>
</Configuration>

application.properties文件添加

logging.config=classpath:log4j2.xml

 #levle后面的是你的mapper 接口的包名是dao层的dao接口 不是mapper.xml文件他俩可以放到一起的但是有的单独配置出来
logging.level.example.moudels.*.mapper=debug 
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值