log4j1.2.17使用

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

PART 1 WEB应用里使用log4j

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

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
      <servlet-name>log4j config file</servlet-name>
      <servlet-class>sse.log4j.Log4jInitServlet</servlet-class>
      <init-param>
          <param-name>configfile</param-name>
          <param-value>/WEB-INF/log4jconfig.properties</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
</web-app>

三、新建/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

整个包结构图如下:

log4j1.2.17使用_第1张图片

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

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>log4j application</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  <body bgcolor="#FFFFFF">
	<%
	Logger rootlogger = Logger.getRootLogger();
	Logger log4jlogger = Logger.getLogger("log4jTestLogger");
	out.println("rootlogger's LEVEL is "+rootlogger.getLevel()+"<p>");
	out.println("log4jlogger's LEVEL is "+log4jlogger.getLevel()+"<p>");
	
	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!");
	
	 %>
  </body>
</html>
七、启动tomcat,在浏览器中打入URL运行log4japp.jsp,可看到Console的部分输出(原因见参考资料)。

log4j1.2.17使用_第2张图片

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

log4j1.2.17使用_第3张图片


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使用)