webservice中使用log4j记录日志

前端之家收集整理的这篇文章主要介绍了webservice中使用log4j记录日志前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


这次项目只做了webservice,然后使用log4j记录日志,按照原来的方式放好了记录日志的类,放好了log4j.properties配置文件,启动项目,测试webservice,竟然没有正常记录日志,提示

log4j:WARN No appenders could be found for logger (SYSTEM_LOG).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.


试过很多方法都不行,后来从网上查到,要强制加载log4j.properties配置文件,试了一下果然可以了。


添加一个servlet:

public class Log4jConfig extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

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

	public void doGet(HttpServletRequest request,HttpServletResponse response)
			throws

			ServletException,IOException {
	}

	public void doPost(HttpServletRequest request,IOException {
	}

	public void init() throws ServletException {
		String prefix = getServletContext().getRealPath("/");
		String file = getInitParameter("log4j");
		if (file != null) {
			PropertyConfigurator.configure(prefix + file);
		}
	}
}


web.xml增加配置:

<!-- 强制加载log4j配置文件 -->
	<servlet>
		<servlet-name>log4jConfig</servlet-name>
		<servlet-class>config.Log4jConfig</servlet-class>
		<init-param>
			<param-name>log4j</param-name>
			<param-value>WEB-INF\classes\log4j.properties</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

参考:http://blog.sina.com.cn/s/blog_a1304cff0101c9bb.html,感谢作者的分享

以此记录

猜你在找的WebService相关文章