一、九九表。
环境设置:需要将log4j-1.2.8.jar放入CLASSPATH变量中。
Hello.java文件的内容如下:
import org.apache.log4j.*;
public class Hello{
static Logger logger = Logger.getLogger(Hello.class);
public static void main(String[] args) {
int i,j;
// BasicConfigurator.configure();
PropertyConfigurator.configure(args[0]);
logger.info("Entering application.");
for(i=1;i<10;i++){
logger.debug(""+i);
for (j=1;j<=i;j++){
logger.warn(""+j);
System.out.print(i*j);
System.out.print("/t");
}
System.out.println("");
}
logger.info("Exiting application.");
}
log4j.rootLogger=WARN, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
#log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Print the date in ISO 8601 format
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN
二、servlet
环境设置:将log4j-1.2.8.jar及servlet.jar放入环境变量CLASSPATH中,并且要将log4j-1.2.8复制到$TOMCAT_HOME/common/lib目录下。
假定,有一个布署到tomcat的应用叫myweb。
servlet程序Log4jInit.java的位置在$TOMCAT_HOME/webapps/myweb/WEB-INF/classes/com/hedong/learning/log4j/目录下,内容如下:
package com.hedong.learning.log4j;
import org.apache.log4j.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Log4jInit extends HttpServlet {
public void init() {
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("log4j");
// if the log4j-init-file is not set, then no point in trying
System.out.println("................log4j start");
if(file != null) {
PropertyConfigurator.configure(prefix+file);
}
}
public void doGet(HttpServletRequest req, HttpServletResponse res) {
}
}
javac Log4jInit.java
...........
log4j-init
com.hedong.learning.log4j.Log4jInit
log4j
WEB-INF/log4j.properties
1
log4j.rootLogger=INFO, A1 , R
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=$TOMCAT_HOME/webapps/dbweb/logs/log4j.log<----将$TOMCAT_HOME换成tomcat的安装目录
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import="org.apache.log4j.*" %>
<%
Logger logger = Logger.getLogger("test.jsp");
logger.info("befor say hi");
%>Hi
<%
logger.info("after say hi");
%>