1、從官方網站下載 jakarta-log4j-1.2.17.tar.gz
http://logging.apache.org/log4j/1.2/download.html
2、在eclipse中將log4j-1.2.17.jar複製到 WebContent/WEB-INF/lib 下, 或新增引用
3、在src下新建File,命名為log4j.properties
4、在log4j.properties中寫入配置檔案
如:
log4j.rootLogger=debug, ServerDailyRollingFile, stdout
log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd
log4j.appender.ServerDailyRollingFile.File=notify-subscription.log
log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d - %m%n
log4j.appender.ServerDailyRollingFile.Append=true
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %p [%c] %m%n
5 使用
<%
Logger log = Logger.getLogger(this.getClass());
log.debug("資料還沒有插入到資料庫中,沒插入---");
%>
6 也可以定義靜態類,方便以後使用:
package net.aimin.dao;
import org.apache.log4j.Logger ;
public class Logs{
private static Logger logger = Logger.getLogger(Logs.class);
//debug方法,其他error、warn類似定義
public static void debug(String obj){
//如果更改日誌出工工具,只需修改該行程式碼即可。如:可改為 System.out.println(obj);
logger.debug(obj);
}
}
ref:
https://blog.csdn.net/yzccc/article/details/53485820
https://wuyechun.iteye.com/blog/1131264
https://blog.51cto.com/javaworker/392487