出现场景:维护之前的SSM的项目,发现log4j.properties能正常加载,语法啥的,配置啥的都没有搞错,但是关于sql打印的就是不生效,没有任何的sql打印出来,前后端一起调试,后端不打印sql,一点都不方便,真是好气哦?,弄了好些天了,今天终于可以了。(log4j有很多的配置方法哦,我这里是properties配置文件通过web.xml的配置加载)
出现原因:就因为少了一句配置
log4j.logger.net.cxp.blog.dao=DEBUG
解决方案:在自己配置的dao路径下加上上面那句配置
log4j.logger.com.taYu.dao=DEBUG
log4j.logger.net.cxp.blog.dao=DEBUG
完整代码:
下面是我log4j.properties和web.xml的具体配置,仅供参考
log4j.properties
#LOG输出级别
log4j.rootLogger=INFO,Console,File
#日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活的指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c]-%m%n
#mybatis显示SQL语句日志配置
#将官网的为自己项目路径
#log4j.logger.org.mybatis=DEBUG
log4j.logger.com.taYu.dao=DEBUG
log4j.logger.net.cxp.blog.dao=DEBUG
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File=org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File=logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize=10MB
#输出所有日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm|\:ss}][%c]%m%n
#数据库连接和事务提交等打印
#log4j.logger.java.sql.Connection=debug
#log4j.logger.java.sql.Statement=debug
#log4j.logger.java.sql.PreparedStatement=debug
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>mybatis</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>1440</session-timeout>
</session-config>
<security-constraint>
<web-resource-collection>
<web-resource-name>DisableUnsecureHttpActions</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>PUT</http-method>
<http-method>HEAD</http-method>
<http-method>TRACE</http-method>
<http-method>OPTIONS</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>NotExistingRole</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<!-- 设置由Sprng载入的Log4j配置文件位置 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/classes/log4j.properties</param-value>
</context-param>
<!-- Spring刷新Log4j配置文件变动的间隔,单位为毫秒 -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>10000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml,classpath:spring-quartz.xml</param-value>
</context-param>
<filter>
<description>字符集过滤器</description>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<description>字符集编码</description>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet>
<description>spring mvc servlet</description>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>spring mvc 配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>xssFilter</filter-name>
<filter-class>com.taYu.filter.XssFilter</filter-class>
<init-param>
<param-name>SplitChar</param-name>
<param-value>@</param-value>
</init-param>
<init-param>
<param-name>FilterChar</param-name>
<param-value>>@<@'@"@\@#@(@)@%</param-value>
</init-param>
<init-param>
<param-name>ReplaceChar</param-name>
<param-value>>@<@‘@“@\@#@(@)@%</param-value>
</init-param>
</filter>
<filter>
<filter-name>systemContextFilter</filter-name>
<filter-class>com.taYu.filter.SystemContextFilter</filter-class>
<init-param>
<param-name>pageSize</param-name>
<param-value>15</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>systemContextFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>
吐槽:之前一直以为log4j.logger.net.cxp.blog.dao=DEBUG是别人项目的路径,后来才发现一直是这个鬼,就是它!气的我不行 维护老项目是真的心累呀
然后重新启动项目,点击前端按钮即可
说在最后的话:编写实属不易,若喜欢或者对你有帮助记得点赞+关注或者收藏哦~