Log4j配置 实例说明

所需文件:1、jar包 : commons-logging.jar 、log4j-1.2.8.jar 、taglibs-log.jar
                    2、配置文件:commons-logging.properties 、log4j.properties
                    3、标签文件:taglibs-log.tag


log4j.properties文件内容:

Log4j配置

#ConversionPattern参数的格式含义
#格式名 含义
#
%c 输出日志信息所属的类的全名
#
%d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy-MM-dd HH:mm:ss },输出类似:2002-10-18- 221028
#
%f 输出日志信息所属的类的类名
#
%l 输出日志事件的发生位置,即输出日志信息的语句处于它所在的类的第几行
#
%m 输出代码中指定的信息,如log(message)中的message
#
%n 输出一个回车换行符,Windows平台为“/r/n”,Unix平台为“/n”
#
%p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL。如果是调用debug()输出的,则为DEBUG,依此类推
#
%r 输出自应用启动到输出该日志信息所耗费的毫秒数
#
%t 输出产生该日志事件的线程名

#日志级别为debug,有stdout,R两个输出端
log4j.rootLogger=debug, stdout, R
#定义stdout的输出端(输出到屏幕)
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#定义stdout的输出的格式
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

#定义stdout的输出端(输出到指定的地方)
log4j.appender.R=org.apache.log4j.RollingFileAppender
#.........
log4j.appender.R.layout=org.apache.log4j.PatternLayout
#.........
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.R.File=F://logger.log
#设置输出文件的最大值
log4j.appender.R.MaxFileSize= 100KB
#设置最大备份个数
log4j.appender.R.MaxBackupIndex=1


新建log4j配置文件载入servlet类:

web.xml文件:

package  Logger;

import  java.io.IOException;

import  javax.servlet.ServletException;
import  javax.servlet.http.HttpServlet;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

import  org.apache.log4j.PropertyConfigurator;

public   class  initLogger  extends  HttpServlet {

    
/**
     * 
     
*/
    
private   static   final   long  serialVersionUID  =   - 3200421233590654178L ;

    
/**
     * 
     
*/

    
/**
     * Constructor of the object.
     
*/
    
public  initLogger() {
        
super ();
    }

    
/**
     * Destruction of the servlet. <br>
     
*/
    
public   void  destroy() {
        
super .destroy();  //  Just puts "destroy" string in log
        
//  Put your code here
    }

    
/**
     * The doGet method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to get.
     * 
     * 
@param  request
     *            the request send by the client to the server
     * 
@param  response
     *            the response send by the server to the client
     * 
@throws  ServletException
     *             if an error occurred
     * 
@throws  IOException
     *             if an error occurred
     
*/
    
public   void  doGet(HttpServletRequest request, HttpServletResponse response)
            
throws  ServletException, IOException {

    }

    
/**
     * The doPost method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to
     * post.
     * 
     * 
@param  request
     *            the request send by the client to the server
     * 
@param  response
     *            the response send by the server to the client
     * 
@throws  ServletException
     *             if an error occurred
     * 
@throws  IOException
     *             if an error occurred
     
*/
    
public   void  doPost(HttpServletRequest request, HttpServletResponse response)
            
throws  ServletException, IOException {
        
this .doGet(request, response);
    }

    
/**
     * Initialization of the servlet. <br>
     * 
     * 
@throws  ServletException
     *             if an error occurs
     
*/
    
public   void  init()  throws  ServletException {
        
//  Put your code here
        
        String prefix 
=   this .getServletContext().getRealPath( " / " );
//         String file = this.getServletConfig().getInitParameter("log4j");
        String file = this .getServletContext().getInitParameter( " log " );
        
        System.out.println(
" 载入log4j配置文件 "   +  prefix  +  file);
        
if  (file  !=   null ) {
            PropertyConfigurator.configure(prefix 
+  file);
        }

    }

}


  <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <!-- 成功 -->
  <context-param>
   <param-name>log</param-name>
   <param-value>/WEB-INF/log4j.properties</param-value>
  </context-param>
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <!--没有成功 
    <init-param>
      <param-name>log4j</param-name>
      <param-value>/WEB-INF/log4j.properties</param-value>
    </init-param>
    -->
    <init-param>
      <param-name>debug</param-name>
      <param-value>3</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>3</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>Show</servlet-name>
    <servlet-class>Test.Show</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>initLogger</display-name>
    <servlet-name>initLogger</servlet-name>
    <servlet-class>Logger.initLogger</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Show</servlet-name>
    <url-pattern>/servlet/Show</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>initLogger</servlet-name>
    <url-pattern>/servlet/initLogger</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

测试代码:

package  Test;

import  java.sql.Connection;
import  java.sql.DriverManager;
import  java.sql.SQLException;
import  java.util.ArrayList;
import  java.util.List;

import  org.apache.log4j.Logger;

public   class  SQLConn {
    
public   static  Connection conn  =   null ;
    
public   static  Logger logger = Logger.getRootLogger();
    
    
public  SQLConn() {
        
try  {
             Class.forName(
" com.microsoft.jdbc.sqlserver.SQLServerDriver " );
             Connection con 
=  DriverManager
             .getConnection(
             
" jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=snow " ,
             
" sa " " 123 " );
             
this .conn  =  con;

        } 
catch  (Exception ex) {
            ex.printStackTrace();
        }
        logger.debug(
" init " );
    }

    
public  List getAll(){
        
//  System.out.println("ok");

        List
< PlateBean >  list  =   new  ArrayList < PlateBean > ();
        
try  {

            java.sql.PreparedStatement p
= conn.prepareStatement( " select * from plate  " );
            java.sql.ResultSet rs
= p.executeQuery();
            
while  (rs.next()) {
                PlateBean bean 
=   new  PlateBean();
                bean.setPlateId(rs.getInt(
1 ));
                bean.setDadId(rs.getInt(
2 ));
                bean.setPlateName(rs.getString(
3 ));
                bean.setPlateInstr(rs.getString(
4 ));
                list.add(bean);
            }
        } 
catch  (Exception ex) {
            ex.printStackTrace();
        }
        logger.debug(
" into " );
        
        
return  list;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值