本人使用spring boot搭建项目,跑是跑起来了,但是日志不输出到控制台,导致开发很不方便,到网上找了很多帖子也没有具体的一个解决方案,然后自己考虑了一个解决方案,欢迎一起交流,废话不多说直接上代码了:
方案1:
直接在web.xml中加入一下代码:
但是个人觉得这样的解决方案不是很好,我的项目不需要web.xml为了这一个配置去加一个文件是不合适的。
方案1:
直接在web.xml中加入一下代码:
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
但是个人觉得这样的解决方案不是很好,我的项目不需要web.xml为了这一个配置去加一个文件是不合适的。
方案2:
在启动类你里面这么写:
@Bean
public Log4jConfigListener log4jConfigListener( WebApplicationContext webApplicationContext){
ServletContext servletContext = webApplicationContext.getServletContext();
servletContext.setInitParameter("log4jConfigLocation","classpath:log4j.properties");
Log4jConfigListener log4jConfigListener = new Log4jConfigListener();
log4jConfigListener.contextInitialized(new ServletContextEvent(servletContext));
return log4jConfigListener;
}
这个就是进去看了下Log4jConfigListener的源码知道发现它是从initParameter这个属性去拿的文件地址,于是就这么尝试了下确实可行。