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, HttpServletResponse response)
			throws

			ServletException, IOException {
	}

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


web.xml增加配置:


	
		log4jConfig
		config.Log4jConfig
		
			log4j
			WEB-INF\classes\log4j.properties
		
		1
	

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

以此记录

你可能感兴趣的:(Java,WebService)