1 16 17 package org.springframework.web.util; 18 19 import java.io.FileNotFoundException ; 20 21 import javax.servlet.ServletContext ; 22 23 import org.springframework.util.Log4jConfigurer; 24 import org.springframework.util.ResourceUtils; 25 import org.springframework.util.SystemPropertyUtils; 26 27 97 public abstract class Log4jWebConfigurer { 98 99 100 public static final String CONFIG_LOCATION_PARAM = "log4jConfigLocation"; 101 102 103 public static final String REFRESH_INTERVAL_PARAM = "log4jRefreshInterval"; 104 105 106 public static final String EXPOSE_WEB_APP_ROOT_PARAM = "log4jExposeWebAppRoot"; 107 108 109 114 public static void initLogging(ServletContext servletContext) { 115 if (exposeWebAppRoot(servletContext)) { 117 WebUtils.setWebAppRootSystemProperty(servletContext); 118 } 119 120 String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM); 122 if (location != null) { 123 try { 125 if (!ResourceUtils.isUrl(location)) { 128 location = SystemPropertyUtils.resolvePlaceholders(location); 130 location = WebUtils.getRealPath(servletContext, location); 131 } 132 133 servletContext.log("Initializing Log4J from [" + location + "]"); 135 136 String intervalString = servletContext.getInitParameter(REFRESH_INTERVAL_PARAM); 138 if (intervalString != null) { 139 try { 142 long refreshInterval = Long.parseLong(intervalString); 143 Log4jConfigurer.initLogging(location, refreshInterval); 144 } 145 catch (NumberFormatException ex) { 146 throw new IllegalArgumentException ("Invalid 'log4jRefreshInterval' parameter: " + ex.getMessage()); 147 } 148 } 149 else { 150 Log4jConfigurer.initLogging(location); 152 } 153 } 154 catch (FileNotFoundException ex) { 155 throw new IllegalArgumentException ("Invalid 'log4jConfigLocation' parameter: " + ex.getMessage()); 156 } 157 } 158 } 159 160 166 public static void shutdownLogging(ServletContext servletContext) { 167 servletContext.log("Shutting down Log4J"); 168 try { 169 Log4jConfigurer.shutdownLogging(); 170 } 171 finally { 172 if (exposeWebAppRoot(servletContext)) { 174 WebUtils.removeWebAppRootSystemProperty(servletContext); 175 } 176 } 177 } 178 179 184 private static boolean exposeWebAppRoot(ServletContext servletContext) { 185 String exposeWebAppRootParam = servletContext.getInitParameter(EXPOSE_WEB_APP_ROOT_PARAM); 186 return (exposeWebAppRootParam == null || Boolean.valueOf(exposeWebAppRootParam).booleanValue()); 187 } 188 189 } 190 | Popular Tags |