一,配置好的log4j在tomcat控制台界面打印n多的DEBUG信息。
经过度娘,网上给出的两种解决方法:
1,找到tomcat下面的 conf 文件夹下面的 logging.properties 文件,修改java.util.logging.ConsoleHandler.level =FINE为 INFO。
(在我这不起作用,还是打印)
2,引入jar包问题导致。经查找,不仅引入了log4j的jar包,还引入了logback的jar包,而logback默认的级别是DEBUG所以会一直打印,去掉jar包引用,OK;
二,log4j打印mybaits sql语句的问题,
经网上查阅,添加如下信息可以打印(我这引用的alibaba的jar包)
<logger name="druid.sql.Connection" additivity="false">
<level value="DEBUG" />
</logger>
<logger name="druid.sql.Statement" additivity="false">
<level value="DEBUG" />
<appender-ref ref="CONSOLE" />
</logger>
注意:这句话必须加上。
三,附上log4j详细配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- appender 部分是可用的日志输出方式定义,可以定义多个 -->
<!--输出到控制台 -->
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss} %m (%F:%L) \n" />
</layout>
<!--限制输出级别 -->
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="DEBUG" />
<param name="LevelMin" value="DEBUG" />
</filter>
</appender>
<!-- 打印sql语句 -->
<logger name="druid.sql.Connection" additivity="false">
<level value="DEBUG" />
</logger>
<logger name="druid.sql.Statement" additivity="false">
<level value="DEBUG" />
<appender-ref ref="CONSOLE" />
</logger>
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%l %d{ISO8601}-- %p -- %m%n" />
</layout>
</appender>
<!-- DEBUG日志-->
<appender name="DEBUG" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/logs/debug.log" />
<param name="DatePattern" value="'daily.'yyyy-MM-dd-HH'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d - %c -%-4r [%t] %-5p %x - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="DEBUG" />
</filter>
</appender>
<!-- INFO日志-->
<appender name="INFO" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/logs/info.log" />
<param name="DatePattern" value="'daily.'yyyy-MM-dd-HH'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d - %c -%-4r [%t] %-5p %x - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>
<!-- WARN日志-->
<appender name="WARN" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/logs/warn.log" />
<param name="DatePattern" value="'daily.'yyyy-MM-dd-HH'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d - %c -%-4r [%t] %-5p %x - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>
<!-- ERROR日志-->
<appender name="ERROR" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/logs/error.log" />
<param name="DatePattern" value="'daily.'yyyy-MM-dd-HH'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d - %c -%-4r [%t] %-5p %x - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
<!-- category 部分定义了特定包或者类的输出级别和方式,可以有多个 -->
<category name="org.apache">
<priority value="warn" />
<appender-ref ref="WARN" />
</category>
<!-- root部分定义了log4j的默认输出级别和方式 -->
<root>
<priority value="INFO" />
<appender-ref ref="DEBUG" />
<appender-ref ref="INFO" />
<appender-ref ref="WARN" />
<appender-ref ref="ERROR" />
<appender-ref ref="STDOUT" />
<appender-ref ref="CONSOLE"/>
</root>
</log4j:configuration>