daily pastebin goal
31%
SHARE
TWEET

Untitled

a guest Jan 19th, 2018 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.apache.log4j.BasicConfigurator;
  2. import org.apache.log4j.xml.DOMConfigurator;
  3.  
  4. @WebListener
  5. public class AppContextListener implements ServletContextListener {
  6.  
  7.     public void contextInitialized(ServletContextEvent servletContextEvent) {
  8.         ServletContext ctx = servletContextEvent.getServletContext();
  9.         //initialize DB Connection          
  10.         //..........................
  11.  
  12.         //initialize log4j
  13.         String log4jConfig = ctx.getInitParameter("log4j-config");
  14.         if(log4jConfig == null){
  15.             System.err.println("No log4j-config init param, initializing log4j with BasicConfigurator");
  16.             BasicConfigurator.configure();
  17.         }else {
  18.             String webAppPath = ctx.getRealPath("/");
  19.             String log4jProp = webAppPath + log4jConfig;
  20.             File log4jConfigFile = new File(log4jProp);
  21.             if (log4jConfigFile.exists()) {
  22.                 System.out.println("Initializing log4j with: " + log4jProp);
  23.                 DOMConfigurator.configure(log4jProp);
  24.             } else {
  25.                 System.err.println(log4jProp + " file not found, initializing log4j with BasicConfigurator");
  26.                 BasicConfigurator.configure();
  27.             }
  28.         }
  29.         System.out.println("log4j configured properly");
  30.     }
  31. }
RAW Paste Data
Top