软件编程
位置:首页>> 软件编程>> java编程>> springboot 高版本后继续使用log4j的完美解决方法

springboot 高版本后继续使用log4j的完美解决方法

作者:吃瓜群众-江郎才尽  发布时间:2021-12-06 11:12:41 

标签:spring,boot,log4j

 springboot  高版本后不支持log4j了,很多人还是喜欢log4j风格的日志,我们自己来加载log4j,其实很容易。

第一步:我们手动加入我们想要的log4j jar,在项目里面随便建一个文件夹,将用到的jar丢进去,右键 add to build path

springboot 高版本后继续使用log4j的完美解决方法

第二步:

在main函数启动类所在的包或者其子包下写一个这样的类,用来加载log4j配置文件,是的,什么内容都没有。


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@ConfigurationProperties("classpath:log4j.properties")
public class Log4jConfigure {
}

这里可能会出现黄色警告提示你要在pom文件中,加入


  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-configuration-processor</artifactId>
   <optional>true</optional>
   </dependency>

如下依赖,你点击确定,他自动帮你加上了。

第二步:将log4j.properties文件丢到application.properties配置文件旁边就可以了,其他什么事情都不用做,

controller中用法和以前一模一样,


import com.dome.dao.UserMapper;
import com.entity.User;
@RestController
@RequestMapping({"/home"})
public class UserController {
private static Logger log = Logger.getLogger(UserController.class);
    log.debug("debug加载默认用户成功");
    log.info("加载默认用户成功");
    log.error("遇到错误,回滚成功")
}

接下来我们我们配置mybatis的日志输出设置为log4j

在application.properties旁边添加一个mybatis-config.xml文件,填入如下内容


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <settings>
 <setting name="logImpl" value="LOG4J"/>
</settings>
</configuration>

接着打开application.properties,添加如下一行信息,SQL语句就能输出到控制台了

mybatis.config-location=classpath:mybatis-config.xml

总结

以上所述是小编给大家介绍的springboot 高版本后继续使用log4j的完美解决方法网站的支持!

来源:http://blog.csdn.net/weixin_36666151/article/details/78080767

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com