springboot⾼版本后继续使⽤log4j的完美解决⽅法
springboot ⾼版本后不⽀持log4j了,很多⼈还是喜欢log4j风格的⽇志,我们⾃⼰来加载log4j,其实很容易。
第⼀步:我们⼿动加⼊我们想要的log4j jar,在项⽬⾥⾯随便建⼀个⽂件夹,将⽤到的jar丢进去,右键 add to build path
第⼆步:
在main函数启动类所在的包或者其⼦包下写⼀个这样的类,⽤来加载log4j配置⽂件,是的,什么内容都没有。
import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.ComponentScan;@ComponentScan
@ConfigurationProperties(\"classpath:log4j.properties\")public class Log4jConfigure {}
这⾥可能会出现黄⾊警告提⽰你要在pom⽂件中,加⼊
org.springframework.boot
spring-boot-configuration-processor true
如下依赖,你点击确定,他⾃动帮你加上了。
第⼆步:将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⽂件,填⼊如下内容
PUBLIC \"-//mybatis.org//DTD Config 3.0//EN\"\"http://mybatis.org/dtd/mybatis-3-config.dtd\">
接着打开application.properties,添加如下⼀⾏信息,SQL语句就能输出到控制台了mybatis.config-location=classpath:mybatis-config.xml
总结
以上所述是⼩编给⼤家介绍的springboot ⾼版本后继续使⽤log4j的完美解决⽅法,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!