Log4j如何打印线程名称(log4j在javaWeb项目中的使用)(1)

一、log4j的配置文件

我们要使用log4j必须要有log4j的配置文件,前面一篇文章提到,log4j的配置文件格式有两种,一种是xml的,另一种是properties的,我更倾向于使用后者,这里也使用后者。配置文件如下,

====== [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%== F:log4j.appender.D.Append = === %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

从上边的配置文件可以看到对整个项目来说,日志界别为info,单独对com.mucfc包配置了error级别,输出目的地有两种:stdout(控制台)、D(文件,输出日志最低级别为debug),也就是说如果在类中 有logger.debug()、logger.info()、logger.warn()、logger.error()这样的方法都会输出,详见下边。

二、初始化log4j配置文件

上边第一步我们配置了log4j配置文件,下边就是初始化log4j。我们这里建的是普通的javaWeb项目,且没有用任何的框架(如,spring,和spring的结合放在下篇中说明),那么我们要如何在项目刚启动就加载配置文件呢?我们知道在web项目中有Filter、listener他们都会在项目启动时进行初始化,Filter是过滤的在这里不适合,listener是监听,这里可以使用listener,另外还有一个用的比较多的,那就是servlet,这个东西我们在开发中用的很多,且可以指定初始化顺序,这里便使用servlet,且指定初始化顺序为1,

package com.mucfc; import java.io.File;import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.Servletconfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; 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; public class Log4JInitServlet extends HttpServlet { private static final long serialVersionUID = 1L; public Log4JInitServlet() { super(); } public void init(ServletConfig config) throws ServletException { System.out.println("Log4JInitServlet 正在初始化 log4j日志设置信息"); String log4jLocation = config.getInitParameter("log4j-properties-location"); ServletContext sc = config.getServletContext(); String str= (String)sc.getInitParameter("test"); System.out.println("str:" str); 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); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }

从上面可以看出,此servlet会从servlet的初始化参数中读取log4j的路径,然后使用PropertyConfigurator.configure(log4jProp); 进行初始化,下面是web.xml,

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Log4j</display-name> <welcome-file-list> <welcome-file> index.html </welcome-file> </welcome-file-list> <servlet> <servlet-name>Log4JTestServlet</servlet-name> <servlet-class>com.mucfc.Log4JTestServlet</servlet-class> </servlet> <servlet> <servlet-name>Log4JInitServlet</servlet-name> <servlet-class>com.mucfc.Log4JInitServlet</servlet-class> <init-param> <param-name>log4j-properties-location</param-name> <param-value>log4j.properties</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> </web-app>

使用了log4j-properties-location参数名,配置的值为log4j.properties,我把配置文件放在了web-cotent下,即根路径下

三、测试

我这里测试使用的是servlet,在请求servlet时调用log4j的日志输出功能,

package com.test;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.Logger; public class MyFirstTest extends HttpServlet { private static final long serialVersionUID = 1L; private Logger logger=Logger.getLogger(this.getClass()); public MyFirstTest() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { logger.info("this is my info message"); logger.debug("this is my debug message"); logger.warn("this is my warn message"); logger.error("this is my error message"); } }

首先得到一个日志对象logger,使用的是,Logger ,然后调用了logger=Logger.getLogger(this.getClass())debug()、info()、warn()、error()方法,

对日志进行了打印,根据配置文件,会打印出相应日志,我们这里整个项目的日志级别定义的为info,则,info()、warn()、error()这三个方法的内容会打印在控制台。

综上所述就是在普通的javaWeb项目中使用log4j。

,