Use log4j record logs in WebService

This time the project only made webservice, and then used log4j to record logs, and placed the logging class in the original way, placed the log4j.properties configuration file, started the project, and tested the webservice. There is no normal log record, prompt:

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.


I tried many methods but it didn’t work. Later, I found out on the Internet that the log4j.properties configuration file must be forced to load. I tried it and it worked.

Add a 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 

Reference: http://blog.sina.com.cn/s/blog_a1304cff0101c9bb.html , Thanks to the author for sharing

For this record

Leave a Comment

Your email address will not be published.