1 Log4j configuration instructions
1.1 Configuration file
Log4j can be dynamically set through a java program. The obvious disadvantage of this method is that if you need to modify the log output level and other information, you must modify the java file and then recompile, which is very troublesome;
Log4j can also be set through configuration files. Currently, it supports two formats of configuration files:
•xml file
•properties file (recommended)
The following is the complete content of a log4j configuration file:
Copy the code code 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.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. Its syntax is:
log4j.rootLogger = [ level ] , appenderName, appenderName, …
Among them, level is the priority of logging, which is divided into OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL or customized levels.
It is recommended to use only four levels. The priorities from high to low are ERROR, WARN, INFO, and DEBUG.
appenderName specifies where the log information is output, and multiple output destinations can be specified at the same time.
1.3 Configure the output destination Appender
Appender mainly defines where the log information is output. The main syntax is:
Copy the code code as follows:
log4j.appender.appenderName = classInfo
log4j.appender.appenderName.option1 = value1
…
log4j.appender.appenderName.optionN = valueN
The appenders provided by Log4j include the following:
•org.apache.log4j.ConsoleAppender(console),
•org.apache.log4j.FileAppender(file),
•org.apache.log4j.DailyRollingFileAppender (generates a log file every day),
•org.apache.log4j.RollingFileAppender (generates a new file when the file size reaches the specified size)
•org.apache.log4j.WriterAppender (send log information in streaming format to any specified place)
Take ConsoleAppender as an example, such as:
Copy the code code as follows:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
1.4 Configure the format layout of log information
Layout is responsible for formatting the output of the Appender. Its syntax is:
Copy the code code as follows:
log4j.appender.appenderName.layout = classInfo
log4j.appender.appenderName.layout.option1 = value1
…
log4j.appender.appenderName.layout.optionN = valueN
Among them, the layouts provided by Log4j include the following:
•org.apache.log4j.HTMLLayout (layout in HTML table format),
•org.apache.log4j.PatternLayout (can flexibly specify layout patterns),
•org.apache.log4j.SimpleLayout (contains the level and information string of the log information)
•org.apache.log4j.TTCCLayout (contains log generation time, thread, category, etc. information)
1.5 Set package output level
You can set the log output level of different packages. The syntax is:
log4j.logger.packageName=level
Among them, packageName is the actual package name, and level is the log level, for example:
Copy the code code as follows:
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 combined with J2ee
2.1 Using spring architecture
Spring is really good. It has done a lot of things for us. If the system uses the spring framework, it is very simple to integrate log4j. It is mainly divided into 3 steps, as follows:
2.1.1 Define log4j configuration file
Copy the code code 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
### 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 listeners
The listener needs to be defined in web.xml, which mainly includes: defining the log4j configuration file directory and log4j listener, as follows:
Copy the code code 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">
<!--The location of the Log4j configuration file loaded by Spring-->
<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 Test class
Copy the code code as follows:
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;
/**
* Constructor of the object.
*/
public InitiaServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
log.debug("The server has started and log4j has started working");
}
}
2.2 Not using spring architecture
If the system does not use spring, let's take servlet as an example. It is very simple. You can just follow the steps and only paste the code, no nonsense.
2.2.1 Define log4j configuration file
Place it in the WEB-INF directory of the web project, with the following content:
Copy the code code 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 log4j initialization class
Copy the code code as follows:
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 initialization class in Web.xml
Copy the code code as follows:
<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
Copy the code code as follows:
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;
/**
* Constructor of the object.
*/
public InitiaServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
log.debug("The server has started and log4j has started working");
}
}