1:需要的jar
log4j-1.2.14.jar
tomcat-juli-adapters.jar
tomcat-juli.jar
2:src下添加log4j.properties
<span style="font-size:18px;">log4j.rootLogger=debug, stdout, log, errorlog #log4j.addivity.org.apache=true ###Console ### log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} [ %p ] [ %C{1}.java :%L(%M)] %m%n ### Log ### log4j.appender.log = org.apache.log4j.DailyRollingFileAppender log4j.appender.log.File = c:/logs/osd/log.log log4j.appender.log.Append = true log4j.appender.log.Threshold = DEBUG log4j.appender.log.DatePattern='.'yyyy-MM-dd log4j.appender.log.layout = org.apache.log4j.PatternLayout log4j.appender.log.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %p ] [ %C{1}.java :%L(%M)] %m%n ### Error ### log4j.appender.errorlog = org.apache.log4j.DailyRollingFileAppender log4j.appender.errorlog.File = c:/logs/osd/errorlog.log log4j.appender.errorlog.Append = true log4j.appender.errorlog.Threshold = ERROR log4j.appender.errorlog.DatePattern='.'yyyy-MM-dd log4j.appender.errorlog.layout = org.apache.log4j.PatternLayout log4j.appender.errorlog.layout.ConversionPattern =%-d{yyyy-MM-dd HH:mm:ss} [ %p ] [ %C{1}.java :%L(%M)] %m%n </span>
<span style="font-size:18px;"> </span>
<span style="font-size:18px;background-color: rgb(102, 255, 255);">3:使用方法</span>
<span style="font-size:18px;"></span><pre name="code" class="java">public class BaseApi { private static Logger logger = Logger.getLogger(BaseApi.class); public BaseApi() throws Throwable { logger.debug(getClass().getSimpleName() + " has created!"); } }
<span style="font-size:18px;">(1)log4j.rootLogger=debug 表示日志输出的最低级别</span>
<span style="font-size:18px;"> </span>
<span style="font-size:18px;">(2)如果使用Hibernate框架的话,还需要屏蔽Hibernate带的日志,否则很难跟踪日志。</span>
<span style="font-size:18px;">第一步: <property name="hibernate.show_sql" value="false" /> </span>
<span style="font-size: 18px;">第二步:Log4j配置文件中加入</span>
<pre name="code" class="html"># Control logging for other open source packages log4j.logger.com.opensymphony.oscache=ERROR log4j.logger.net.sf.navigator=ERROR log4j.logger.net.sf.acegisecurity=WARN log4j.logger.net.sf.acegisecurity.intercept.event.LoggerListener=WARN log4j.logger.org.apache.commons=ERROR log4j.logger.org.apache.struts=WARN log4j.logger.org.displaytag=ERROR log4j.logger.org.springframework=WARN log4j.logger.com.ibatis.db=WARN log4j.logger.org.apache.velocity=WARN # Don't show debug logs for WebTest log4j.logger.com.canoo.webtest=WARN # All hibernate log output of "info" level or higher goes to stdout. # For more verbose logging, change the "info" to "debug" on the last line. log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN log4j.logger.org.hibernate=WARN # Changing the log level to DEBUG will result in Hibernate generated # SQL to be logged. log4j.logger.org.hibernate.SQL=ERROR # Changing the log level to DEBUG will result in the PreparedStatement # bound variable values to be logged. log4j.logger.org.hibernate.type=ERROR
原文地址:http://blog.csdn.net/u013628152/article/details/44751611