Use of log4j for detailed parsing

  • 2020-04-01 02:11:53
  • OfStack

1 Log4j configuration instructions

1.1 configuration files
Log4j can be set dynamically by a Java program. The obvious disadvantages of this approach are that if you need to change information such as the level of log output, you must modify the Java file and then recompile it, which is troublesome.

Log4j can also be set as a configuration file, and currently supports two formats of configuration files:

The & # 8226; The XML file
The & # 8226; Properties file (recommended)
Here is the full contents of a log4j configuration file:


log4j.rootCategory=INFO, stdout
log4j.rootLogger=info, stdout

### stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p - %m%n

### set package ###
log4j.logger.org.springframework=info
log4j.logger.org.apache.catalina=info
log4j.logger.org.apache.commons.digester.Digester=info
log4j.logger.org.apache.catalina.startup.TldConfig=info
log4j.logger.chb.test=debug

1.2 configure the root Logger
The root logger mainly defines the log level and output destination supported by log4j, and its syntax is:
RootLogger = [level], appenderName, appenderName,...
Where, level is the priority of logging, divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL, or custom levels.

It is recommended to use only four levels, with priority ranging from high to low: ERROR, WARN, INFO, DEBUG.
AppenderName specifies where the log information is output and can specify multiple output destinations at the same time.

1.3 configure an output destination Appender
The Appender mainly defines where the log information output is, and the main syntax is:


log4j.appender.appenderName = classInfo
log4j.appender.appenderName.option1 = value1
 ... 
log4j.appender.appenderName.optionN = valueN

Log4j provides several appenders:

The & # 8226; Org, apache log4j. ConsoleAppender (console),
The & # 8226; Org, apache log4j. FileAppender (documents),
The & # 8226; Org, apache log4j. DailyRollingFileAppender (every day to create a log file),
The & # 8226; Org, apache log4j. RollingFileAppender (file size reached specified size to create a new file)
The & # 8226; Org, apache log4j. WriterAppender (the log information to flow format to send to any designated place)
Take ConsoleAppender as an example, for example:


log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out

1.4 configure the Layout of the log information
Layout is responsible for formatting the output of the Appender, and its syntax is:

log4j.appender.appenderName.layout = classInfo
log4j.appender.appenderName.layout.option1 = value1
 ... 
log4j.appender.appenderName.layout.optionN = valueN

Among them, Log4j provides the following layouts:

The & # 8226; Org, apache log4j. HTMLLayout (layout) in HTML form,
The & # 8226; Org, apache log4j. PatternLayout (the flexibility to specify layout model),
The & # 8226; Org, apache log4j. SimpleLayout string (including the level of logging information and information)
The & # 8226; Org, apache log4j. TTCCLayout (contains the logs generated time, thread, categories, etc.)

1.5 set the package output level
The log output level of different packages can be set. The syntax is:
Log4j. Logger. PackageName = level
Where, the packageName is the actual packageName and the level is the log level, for example:


log4j.logger.org.springframework=info
log4j.logger.org.apache.catalina=info
log4j.logger.org.apache.commons.digester.Digester=info
log4j.logger.org.apache.catalina.startup.TldConfig=info
log4j.logger.chb.test=debug

2 Log4j is combined with J2ee

2.1 use the spring architecture
Spring is really good and does a lot of things for us. If the system USES the Spring framework, it is easy to integrate log4j, which is mainly divided into three steps, as follows:

2.1.1 define the log4j configuration file


log4j.rootCategory=INFO, stdout
log4j.rootLogger=info, stdout

### stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p - %m%n

### log to file ###
log4j.logger.org.springframework=info
log4j.logger.org.apache.catalina=info
log4j.logger.org.apache.commons.digester.Digester=info
log4j.logger.org.apache.catalina.startup.TldConfig=info
log4j.logger.chb.test=debug

2.1.2   Define listener
The listener needs to be defined in web.xml, mainly including: define log4j configuration file directory and log4j listener, as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <!-- by Spring loaded Log4j Profile location -->
    <context-param>
       <param-name>log4jConfigLocation</param-name>
       <param-value>/WEB-INF/log4j.properties</param-value>
    </context-param>

    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>
           /WEB-INF/classes/applicationContext*.xml
       </param-value>
    </context-param>

    <!--Spring log4j Config loader-->
    <listener>
       <listener-class>
           org.springframework.web.util.Log4jConfigListener
       </listener-class>
    </listener>

    <listener>
       <listener-class>
           org.springframework.web.context.ContextLoaderListener
       </listener-class>
    </listener>

    <servlet>
       <servlet-name>InitiaServlet</servlet-name>
    <servlet-class>chb.test.web.InitiaServlet</servlet-class>
       <load-on-startup>1</load-on-startup>
    </servlet>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

2.1.3   The test class

package com.dheaven.mip.web;

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

import org.apache.log4j.Logger;

public class InitiaServlet extends HttpServlet {

    protected Logger log = Logger.getLogger(InitiaServlet.class);

    private static final long serialVersionUID = 8550329576989690578L;

    
    public InitiaServlet() {
       super();
    }

    
    public void destroy() {
       super.destroy();
    }

    
    public void init() throws ServletException {
       log.debug(" The server is up, log4j Get to work ");
    }
}

2.2 do not use the spring architecture
If the system is not using spring, let's take a servlet as an example. It's very simple. Just follow the steps and stick to the code.

2.2.1 define the log4j configuration file
In the web-inf directory of the web project, the content is as follows:


log4j.rootCategory=INFO, stdout
log4j.rootLogger=info, stdout

### stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p - %m%n

### set package ###
log4j.logger.org.apache.catalina=info
log4j.logger.org.apache.commons.digester.Digester=info
log4j.logger.org.apache.catalina.startup.TldConfig=info
log4j.logger.com.dheaven=debug

2.2.2   Create the log4j initialization class

package com.dheaven.mip.web;

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

import org.apache.log4j.PropertyConfigurator;

public class InitLog4j extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public void init() throws ServletException {
       String prefix = getServletContext().getRealPath("/");
       prefix = prefix.replace("//", "/");
       String file = getInitParameter("log4j-init-file");
       // if the log4j-init-file is not set, then no point in trying
       if (file != null) {
           PropertyConfigurator.configure(prefix + file);
       }
    }
}

2.2.3   Define the initialization class in web.xml

    <servlet>
       <servlet-name>log4j-init</servlet-name>
       <servlet-class>chb.test.web.InitLog4j</servlet-class>

       <init-param>
           <param-name>log4j-init-file</param-name>
           <param-value>WEB-INF/log4j.properties</param-value>
       </init-param>

       <load-on-startup>1</load-on-startup>
    </servlet>

2.2.4 test class

package chb.test.web;

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

import org.apache.log4j.Logger;

public class InitiaServlet extends HttpServlet {

    protected Logger log = Logger.getLogger(InitiaServlet.class);

    private static final long serialVersionUID = 8550329576989690578L;

    
    public InitiaServlet() {
       super();
    }

    
    public void destroy() {
       super.destroy();
    }

    
    public void init() throws ServletException {
       log.debug(" The server is up, log4j Get to work ");
    }
}


Related articles: