Configure log4j in the web app

xiaoxiao2021-03-06  108

Log4j is our common logger. When you use it in a web environment, you need to consider where the initialization code is placed, and the initialization code has been placed in the ActionServlet in Struts (yourselfloaded in the init section), but this always doesn't think Very elegant, and there is no versatility. After reading some of the posts, I found that using an AutoLoad's servlet to complete the most appropriate work, first can be separated, secondly this servlet can be universal, the method is simple, just overloading the Server's init method, rough code As follows: public void init () throws servletexception {string prefix = this.getServletContext (). GetRealPath ("/"); string path = this.getinitParameter ("log4j-path"); // If there is no configuration parameter, use the default path IF ((NULL == PATH) || (Path.Length () == 0)) {path = defiX path; // Complete the initialization PropertyConfigurator.configure (PATH);} then register this servlet For automatic start, Web.xml should have the following paragraph, the blue part indicates the servlet self-start log4jinit com.servlet.log4jinit 1

转载请注明原文地址:https://www.9cbs.com/read-95769.html

New Post(0)