关于log4f日志的使用(spring+maven)

首先在web.xml里面配置

                <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>

</dependency>

接着创建log4j.properties配置文件和Log4JInitServlet.java初始化文件

log4j.properties

 ### set log levels ###
##log4j.rootLogger = debug , stdout , D , E
#log4j.rootLogger = debug , stdout , D
log4j.rootLogger = debug, console , D , E


###  output to the console ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.Append = true
log4j.appender.console.Threshold = INFO 
#log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
log4j.appender.console.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n


### Output to the log file ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
###log4j.appender.D.File = ${springmvc.root}/WEB-INF/logs/log.log
log4j.appender.D.File = E:/log/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n


### Save exception information to separate file ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
###log4j.appender.E.File = ${springmvc.root}/WEB-INF/logs/error.log 
log4j.appender.E.File = E:/log/error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout

log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

注:

首先,log4f输出从低到高分为debug,info,warn,error,fatal这5个级别

log4j.rootLogger这个是总的概括,最多可以有4个参数,第一个参数(debug)表示全局最低输出(若这里设置了warn,则不管你下面的怎么设置,debug和info都不会输出,建议这里都设置成debug,细分的可以在下面输出)。第二个到第四个参数是下面的设置的参数名(比如console 即log4j.appender.console最后的console),只要保证与下面的一样即可,最多可以设置3个参数。

org.apache.log4j.ConsoleAppender表示控制台输出

org.apache.log4j.DailyRollingFileAppender 表示每天生产的文件,到第二天他会自动改名成log.log.2018-05-16

log4j.appender.E.File 表示日志输出的路径

log4j.appender.D.Append =true表示日志输出是追加,不是覆盖

log4j.appender.D.Threshold = DEBUG 表示这个类的最低输出级别

log4j.appender.E.layout = org.apache.log4j.PatternLayout

log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

表示输出的格式


Log4JInitServlet.java


package com.sunyard.insurance.Servlet;


import java.io.File;  
import java.io.IOException;  


import javax.servlet.ServletConfig;  
import javax.servlet.ServletContext;  
import javax.servlet.ServletException;  
import javax.servlet.annotation.WebServlet;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  


import org.apache.log4j.BasicConfigurator;  
import org.apache.log4j.PropertyConfigurator;  


/** 
 * Servlet implementation class Log4JInitServlet 
 */  
@WebServlet("/Log4JInitServlet")  
public class Log4JInitServlet extends HttpServlet {  
    private static final long serialVersionUID = 1L;  


    /** 
     * @see HttpServlet#HttpServlet() 
     */  
    public Log4JInitServlet() {  
        super();  
        // TODO Auto-generated constructor stub  
    }  


    /** 
     * @see Servlet#init(ServletConfig) 
     */  
    public void init(ServletConfig config) throws ServletException {  
        System.out.println("Log4JInitServlet 正在初始化 log4j日志设置信息");  
        String log4jLocation = config.getInitParameter("log4j-properties-location");  
        ServletContext sc = config.getServletContext();  
        if (log4jLocation == null) {  
            System.err.println("*** 没有 log4j-properties-location 初始化的文件, 所以使用 BasicConfigurator初始化");  
            BasicConfigurator.configure();  
        } else {  
            String webAppPath = sc.getRealPath("/");  
            String log4jProp = webAppPath + log4jLocation;  
            File yoMamaYesThisSaysYoMama = new File(log4jProp);  
            if (yoMamaYesThisSaysYoMama.exists()) {  
                System.out.println("使用: " + log4jProp+"初始化日志设置信息");  
                PropertyConfigurator.configure(log4jProp);  
            } else {  
                System.err.println("*** " + log4jProp + " 文件没有找到, 所以使用 BasicConfigurator初始化");  
                BasicConfigurator.configure();  
            }  
        }  
        super.init(config);  
    }  


    /** 
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
     */  
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
        // TODO Auto-generated method stub  
    }  
    /** 
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
     */  
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
        // TODO Auto-generated method stub  
    } 
}

接着配置web.xml

  <servlet>
    <servlet-name>Log4JInitServlet</servlet-name>
    <servlet-class>com.sunyard.insurance.Servlet.Log4JInitServlet</servlet-class>
    <init-param>
      <param-name>log4j-properties-location</param-name>
      <param-value>WEB-INF/classes/conf/log4j.properties</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>

  </servlet>





阅读更多
想对作者说点什么? 我来说一句

spring+maven整合

2015年04月30日 21.92MB 下载

没有更多推荐了,返回首页

不良信息举报

关于log4f日志的使用(spring+maven)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭