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