一、配置log4j.properties的内容:
log4j.rootLogger=debug,appender
#log4j.rootLogger=info,appender
#log4j.rootLogger=error,appender
log4j.appender.appender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.appender.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.appender.File=./log/logsersss.log
log4j.appender.appender.layout=org.apache.log4j.PatternLayout
log4j.appender.appender.layout.ConversionPattern=%r [%d{yy/MM/dd HH:mm:ss:SSS}][%t][%C-%M] %m%n
#%r:ms [%t] [%p] -%l -%m%n
二、加载jar包 : 一共3个jar包: commons-logging-1.0.4.jar,commons-logging-api-1.1.jar,log4j-1.2.15.jar
三、在java类里面使用:
serverlet里面使用,web.xml配置servlet,
public class LogServerlettest extends HttpServlet {
private static final Log log =LogFactory.getLog(LogServerlettest.class.getName());
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doGet.......");
log.error("doget......error");
log.warn("doget......warn");
log.info("doget......info");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
doGet(request, response);
}
}
四、效果
在文件里面显示
0 [13/12/20 14:49:52:086][http--81-0$18205044][com.cnten.logtest.LogServerlettest-doGet] doget......error
2 [13/12/20 14:49:52:088][http--81-0$18205044][com.cnten.logtest.LogServerlettest-doGet] doget......warn
3 [13/12/20 14:49:52:089][http--81-0$18205044][com.cnten.logtest.LogServerlettest-doGet] doget......info