# Log4j
Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台、文件等地方;
我们也可以控制每一条日志的输出格式;通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。
最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。
以下是 Log4j 结合 slf4j 的配置使用过程
## Log4j 配置过程
1. 下载如下 jar 文件,jar 包下载网站: http://jareye.com/
* slf4j-api-1.7.25.jar
* slf4j-log4j12-1.7.25.jar
* log4j-1.2.17.jar
2. 在 Eclipse 的项目上右键定义一个名为 conf 的 source folder
3. 在 conf 文件夹中新建 log4j.properties 文件,内容如下
~~~
log4j.rootLogger=INFO, stdout , R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QNSL] %d{yyyyMMdd HH:mm:ss,SSS} %p [%t] %C.%M(%L) | %m%n
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=D:\\demo1.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[QNSL] %d{yyyyMMdd HH:mm:ss,SSS} %p [%t] %C.%M(%L) | %m%n
log4j.logger.cn=ERROR
~~~
4. 新建一个 Servlet,程序清单如下
~~~
package cn.controller;
import java.io.IOException;
import javax.servlet.ServletConfig;
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 Log4jServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException {
String prefix = this.getClass().getClassLoader().getResource("/").getPath();
String path = config.getInitParameter("log4j-path");
PropertyConfigurator.configure(prefix + path);
}
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
}
public void destroy() {
}
}
~~~
5. 在 web.xml 中登记 Log4jServlet
~~~
<servlet>
<servlet-name>log4j</servlet-name>
<servlet-class>cn.controller.Log4jServlet</servlet-class>
<init-param>
<param-name>log4j-path</param-name>
<param-value>log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
~~~
6. 使用 Log4j
~~~
package cn.controller.common;
import java.io.IOException;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/common/LoginServlet.do")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(LoginServlet.class);
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
logger.debug("Hello, Log4j");
request.getRequestDispatcher("/WEB-INF/pages/common/login.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
~~~