java 代码
- Spring Open Sourse Research(Log4jConfigListener Parts)
- /**
- * Bootstrap listener for custom Log4J initialization in a web environment.
- * Delegates to Log4jWebConfigurer (see its javadoc for configuration details).
- *
- * WARNING: Assumes an expanded WAR file, both for loading the configuration
- * file and for writing the log files. If you want to keep your WAR unexpanded or
- * don’t need application-specific log files within the WAR directory, don’t use
- * Log4J setup within the application (thus, don’t use Log4jConfigListener or
- * Log4jConfigServlet). Instead, use a global, VM-wide Log4J setup (for example,
- * in JBoss) or JDK 1.4’s java.util.logging (which is global too).
- *
- *
- This listener should be registered before ContextLoaderListener in web.xml,
- * when using custom Log4J initialization.
- *
- *
- For Servlet 2.2 containers and Servlet 2.3 ones that do not
- * initalize listeners before servlets, use Log4jConfigServlet.
- * See the ContextLoaderServlet javadoc for details.
- *
- * @author Juergen Hoeller
- * @since 13.03.2003
- * @see Log4jWebConfigurer
- * @see Log4jConfigServlet
- * @see org.springframework.web.context.ContextLoaderListener
- * @see org.springframework.web.context.ContextLoaderServlet
- * @see WebAppRootListener
- */
- public class Log4jConfigListener implements ServletContextListener {
- public void contextInitialized(ServletContextEvent event) {
- Log4jWebConfigurer.initLogging(event.getServletContext());
- }
- public void contextDestroyed(ServletContextEvent event) {
- Log4jWebConfigurer.shutdownLogging(event.getServletContext());
- }
- }
- Log4jWebConfigurer.java
- public abstract class Log4jWebConfigurer {
- /** Parameter specifying the location of the Log4J config file */
- public static final String CONFIG_LOCATION_PARAM = “log4jConfigLocation”;
- /** Parameter specifying the refresh interval for checking the Log4J config file */
- public static final String REFRESH_INTERVAL_PARAM = “log4jRefreshInterval”;
- /** Parameter specifying whether to expose the web app root system property */
- public static final String EXPOSE_WEB_APP_ROOT_PARAM = “log4jExposeWebAppRoot”;
- /**
- * Initialize Log4J, including setting the web app root system property.
- * @param servletContext the current ServletContext
- * @see WebUtils#setWebAppRootSystemProperty
- */
- public static void initLogging(ServletContext servletContext) {
- // Expose the web app root system property.
- if (exposeWebAppRoot(servletContext)) {
- WebUtils.setWebAppRootSystemProperty(servletContext);
- }
- // Only perform custom Log4J initialization in case of a config file.
- //CONFIG_LOCATION_PARAM 为log4j.properties的存放路径
- String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
- if (location != null) {
- // Perform actual Log4J initialization; else rely on Log4J’s default initialization.
- try {
- // Return a URL (e.g. “classpath:” or “file:”) as-is;
- // consider a plain file path as relative to the web application root directory.
- if (!ResourceUtils.isUrl(location)) {
- // Resolve system property placeholders before resolving real path.
- location = SystemPropertyUtils.resolvePlaceholders(location);
- location = WebUtils.getRealPath(servletContext, location);
- }
- // Write log message to server log.
- //初始化log
- servletContext.log(“Initializing Log4J from [“ + location + “]”);
- // Check whether refresh interval was specified.
- // REFRESH_INTERVAL_PARAM equals the refresh interval for checking the Log4J config file
- String intervalString = servletContext.getInitParameter(REFRESH_INTERVAL_PARAM);
- if (intervalString != null) {
- // Initialize with refresh interval, i.e. with Log4J’s watchdog thread,
- // checking the file in the background.
- try {
- long refreshInterval = Long.parseLong(intervalString);
- Log4jConfigurer.initLogging(location, refreshInterval);
- }
- catch (NumberFormatException ex) {
- throw new IllegalArgumentException(“Invalid ‘log4jRefreshInterval’ parameter: “ + ex.getMessage());
- }
- }
- else {
- // Initialize without refresh check, i.e. without Log4J’s watchdog thread.
- Log4jConfigurer.initLogging(location);
- }
- }
- catch (FileNotFoundException ex) {
- throw new IllegalArgumentException(“Invalid ‘log4jConfigLocation’ parameter: “ + ex.getMessage());
- }
- }
- }
- /**
- * Return whether to expose the web app root system property,
- * checking the corresponding ServletContext init parameter.
- * @see #EXPOSE_WEB_APP_ROOT_PARAM
- */
- //返回是否暴露web app root system property
- //get the “log4jExposeWebAppRoot” para from web.xml
- private static boolean exposeWebAppRoot(ServletContext servletContext) {
- String exposeWebAppRootParam = servletContext.getInitParameter(EXPOSE_WEB_APP_ROOT_PARAM);
- return (exposeWebAppRootParam == null || Boolean.valueOf(exposeWebAppRootParam).booleanValue());
- }
- }
- WebUtils.java
- public abstract class WebUtils {
- /**
- * Standard Servlet 2.3+ spec request attributes for include URI and paths.
- *
- If included via a RequestDispatcher, the current resource will see the
- * originating request. Its own URI and paths are exposed as request attributes.
- */
- public static final String INCLUDE_REQUEST_URI_ATTRIBUTE = “javax.servlet.include.request_uri”;
- public static final String INCLUDE_CONTEXT_PATH_ATTRIBUTE = “javax.servlet.include.context_path”;
- public static final String INCLUDE_SERVLET_PATH_ATTRIBUTE = “javax.servlet.include.servlet_path”;
- public static final String INCLUDE_PATH_INFO_ATTRIBUTE = “javax.servlet.include.path_info”;
- public static final String INCLUDE_QUERY_STRING_ATTRIBUTE = “javax.servlet.include.query_string”;
- /**
- * Standard Servlet 2.4+ spec request attributes for forward URI and paths.
- *
- If forwarded to via a RequestDispatcher, the current resource will see its
- * own URI and paths. The originating URI and paths are exposed as request attributes.
- */
- public static final String FORWARD_REQUEST_URI_ATTRIBUTE = “javax.servlet.forward.request_uri”;
- public static final String FORWARD_CONTEXT_PATH_ATTRIBUTE = “javax.servlet.forward.context_path”;
- public static final String FORWARD_SERVLET_PATH_ATTRIBUTE = “javax.servlet.forward.servlet_path”;
- public static final String FORWARD_PATH_INFO_ATTRIBUTE = “javax.servlet.forward.path_info”;
- public static final String FORWARD_QUERY_STRING_ATTRIBUTE = “javax.servlet.forward.query_string”;
- /**
- * Prefix of the charset clause in a content type String: “;charset=”
- */
- public static final String CONTENT_TYPE_CHARSET_PREFIX = “;charset=”;
- /**
- * Default character encoding to use when request.getCharacterEncoding
- * returns null, according to the Servlet spec.
- * @see ServletRequest#getCharacterEncoding
- */
- public static final String DEFAULT_CHARACTER_ENCODING = “ISO-8859-1”;
- /**
- * Standard Servlet spec context attribute that specifies a temporary
- * directory for the current web application, of type java.io.File.
- */
- public static final String TEMP_DIR_CONTEXT_ATTRIBUTE = “javax.servlet.context.tempdir”;
- /**
- * HTML escape parameter at the servlet context level
- * (i.e. a context-param in web.xml): “defaultHtmlEscape”.
- */
- public static final String HTML_ESCAPE_CONTEXT_PARAM = “defaultHtmlEscape”;
- /**
- * Web app root key parameter at the servlet context level
- * (i.e. a context-param in web.xml): “webAppRootKey”.
- */
- public static final String WEB_APP_ROOT_KEY_PARAM = “webAppRootKey”;
- /** Default web app root key: “webapp.root” */
- public static final String DEFAULT_WEB_APP_ROOT_KEY = “webapp.root”;
- /** Name suffixes in case of image buttons */
- public static final String[] SUBMIT_IMAGE_SUFFIXES = {“.x”, “.y”};
- /** Key for the mutex session attribute */
- public static final String SESSION_MUTEX_ATTRIBUTE = WebUtils.class.getName() + “.MUTEX”;
- /**
- * Set a system property to the web application root directory.
- * The key of the system property can be defined with the “webAppRootKey”
- * context-param in web.xml. Default is “webapp.root”.
- *
- Can be used for tools that support substition with System.getProperty
- * values, like Log4J’s “${key}” syntax within log file locations.
- * @param servletContext the servlet context of the web application
- * @throws IllegalStateException if the system property is already set,
- * or if the WAR file is not expanded
- * @see #WEB_APP_ROOT_KEY_PARAM
- * @see #DEFAULT_WEB_APP_ROOT_KEY
- * @see WebAppRootListener
- * @see Log4jWebConfigurer
- */
- /**把当前的项目的路径放到System属性参数中,供以后调用,其中key值为web.xml
- webAppRootKey
- fms.root
- */
- public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException {
- Assert.notNull(servletContext, “ServletContext must not be null”);
- String root = servletContext.getRealPath(“/”);// 项目路径
- if (root == null) {
- throw new IllegalStateException(
- “Cannot set web app root system property when WAR file is not expanded”);
- }
- String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);
- String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);
- String oldValue = System.getProperty(key);
- if (oldValue != null && !StringUtils.pathEquals(oldValue, root)) {
- throw new IllegalStateException(
- “Web app root system property already set to different value: ‘” +
- key + “‘ = [“ + oldValue + “] instead of [“ + root + “] – “ +
- “Choose unique values for the ‘webAppRootKey’ context-param in your web.xml files!”);
- }
- System.setProperty(key, root);
- servletContext.log(“Set web app root system property: ‘” + key + “‘ = [“ + root + “]”);
- }
- }
In a word ,if you want to use the Log4jConfigListener ,
there are several context params need to config in web.xml as follows:
1,webAppRootKey,项目名称
2,log4jExposeWebAppRoot, whether to expose the webAppRootKey to the System,是否将webAppRootKey.value这个属性暴露给System,
也就是否可以用System.getProperty(webAppRootKey.value)获得项目名称
3,log4jRefreshInterval,Parameter specifying the refresh interval for checking the Log4J config file
4,log4jConfigLocation,Parameter specifying the place where you can find log4j.properties.