log4j1.2.17使用

网上找了一些资料没看明白,还是找到了一本书,书里有这一章,一步步敲下来。明白了。

PART 1 WEB应用里使用log4j

一、log4j1.2.17下载地址点击打开链接

二、myeclipse中新建WEB项目log4jtest, 并修改web.xml为:



  	
  
    index.jsp
  
  
      log4j config file
      sse.log4j.Log4jInitServlet
      
          configfile
          /WEB-INF/log4jconfig.properties
      
      1
  

三、新建/WEB-INF/log4jconfig.properties为:

log4j.rootLogger = DEBUG,file
log4j.logger.log4jTestLogger=WARN,console,file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd  HH:mm:ss,SSS}  [%c]-[%p]%m%n

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/log4jtest/WEB-INF/log.html
log4j.appender.file.MaxFileSize=500KB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.HTMLLayout

四、新建sse.log4j.Log4jInitServlet类为:

package sse.log4j;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.PropertyConfigurator;


public class Log4jInitServlet extends HttpServlet {
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		String path = getServletContext().getRealPath("/");
		String configfile = path + getInitParameter("configfile");
		PropertyConfigurator.configure(configfile);
	}
}
五、新建空文件/WEB-INF/log.html

整个包结构图如下:


六、新建log4japp.jsp,内容如下:

<%@ page language="java" import="org.apache.log4j.Logger" pageEncoding="GB2312"%>



  
    
    log4j application
    
	
	
	    
	
	
	

  
  
	<%
	Logger rootlogger = Logger.getRootLogger();
	Logger log4jlogger = Logger.getLogger("log4jTestLogger");
	out.println("rootlogger's LEVEL is "+rootlogger.getLevel()+"

"); out.println("log4jlogger's LEVEL is "+log4jlogger.getLevel()+"

"); log4jlogger.debug("This is DEBUG info from "+log4jlogger.getName()); log4jlogger.info("This is INFO info from "+log4jlogger.getName()); log4jlogger.warn("This is WARN info from "+log4jlogger.getName()); log4jlogger.error("This is ERROR info from "+log4jlogger.getName()); log4jlogger.fatal("This is FATAL info from "+log4jlogger.getName()); out.println("\nlog info has been printed!"); %>

七、启动tomcat,在浏览器中打入URL运行log4japp.jsp,可看到Console的部分输出(原因见参考资料)。


用浏览器打开log.html结果:


PART 2 普通java类里使用log4j

package Test;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Test {
     /*
      * 步骤:
      * 一、下载log4j.jar包,并导入
      * 二、配置log4j.properties文件
      */
     private Logger log = Logger.getLogger(Test.class);
     public Test() {
         //log4jconfig.properties文件和当前运行的class放在同一目录下
         String connectdir=Test.class.getResource("log4jconfig.properties").getPath();
         PropertyConfigurator.configure(connectdir);
         System.out.println(connectdir);
     }

     public void testprint() {
         log.debug("debug test");
         log.info("info test");
         log.warn("warn test");
         log.error("error test");
     }

     public static void main(String[] args) {
         Test test = new Test();
         test.testprint();
     }
}

log4j参考资料:

参考一

参考二

参考三

你可能感兴趣的:(log4j1.2.17使用)