1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.util;
  17. import java.io.FileNotFoundException;
  18. import javax.servlet.ServletContext;
  19. import org.springframework.util.Log4jConfigurer;
  20. /**
  21. * Convenience class that performs custom Log4J initialization for web environments,
  22. * supporting 2 init parameters at the servlet context level (i.e. context-param
  23. * in web.xml):
  24. * <ul>
  25. * <li>"log4jConfigLocation": name of the Log4J config file (relative to the web
  26. * application root directory, e.g. "WEB-INF/log4j.properties");
  27. * <li>"log4jRefreshInterval": interval between config file refresh* checks, in
  28. * milliseconds. If unspecified, a value of {@link Log4jConfigurer#DEFAULT_REFRESH_INTERVAL}
  29. * will be used.
  30. * </ul>
  31. *
  32. * <p>Note: initLogging should be called before any other Spring activity (when using
  33. * Log4J), to guarantee proper initialization before any Spring logging attempts.
  34. *
  35. * <p>Note: Sets the web app root system property, for "${key}" substitutions
  36. * within log file locations in the Log4J config file. The default system property
  37. * key is "webapp.root". Example, using context-param "webAppRootKey" = "demo.root":
  38. * log4j.appender.myfile.File=${demo.root}/WEB-INF/demo.log
  39. *
  40. * <p><b>WARNING</b>: Some containers like Tomcat do NOT keep system properties separate
  41. * per web app. You have to use unique "webAppRootKey" context-params per web app
  42. * then, to avoid clashes. Other containers like Resin do isolate each web app's
  43. * system properties: Here you can use the default key (i.e. no "webAppRootKey"
  44. * context-param at all) without worrying.
  45. *
  46. * <p><b>WARNING</b>: The WAR file containing the web application needs to be expanded
  47. * to allow for setting the web app root system property and for loading Log4J
  48. * configuration from a custom location. This is by default not the case when a
  49. * WAR file gets deployed to WebLogic, for example. Do not use this configurer
  50. * respectively Log4jConfigListener or Log4jConfigServlet in such an environment!
  51. *
  52. * @author Juergen Hoeller
  53. * @since 12.08.2003
  54. * @see org.springframework.util.Log4jConfigurer
  55. * @see Log4jConfigListener
  56. * @see Log4jConfigServlet
  57. */
  58. public abstract class Log4jWebConfigurer {
  59. /** Parameter specifying the location of the Log4J config file */
  60. public static final String CONFIG_LOCATION_PARAM = "log4jConfigLocation";
  61. /** Parameter specifying the refresh interval for checking the Log4J config file */
  62. public static final String REFRESH_INTERVAL_PARAM = "log4jRefreshInterval";
  63. public static void initLogging(ServletContext servletContext) {
  64. // set the web app root system property
  65. WebUtils.setWebAppRootSystemProperty(servletContext);
  66. // only perform custom Log4J initialization in case of a config file
  67. String location = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
  68. if (location != null) {
  69. // interpret location as relative to the web application root directory
  70. if (location.charAt(0) != '/') {
  71. location = "/" + location;
  72. }
  73. location = servletContext.getRealPath(location);
  74. // use default refresh interval if not specified
  75. long refreshInterval = Log4jConfigurer.DEFAULT_REFRESH_INTERVAL;
  76. String intervalString = servletContext.getInitParameter(REFRESH_INTERVAL_PARAM);
  77. if (intervalString != null) {
  78. refreshInterval = Long.parseLong(intervalString);
  79. }
  80. // write log message to server log
  81. servletContext.log("Initializing Log4J from [" + location + "]");
  82. // perform actual Log4J initialization
  83. try {
  84. Log4jConfigurer.initLogging(location, refreshInterval);
  85. }
  86. catch (FileNotFoundException ex) {
  87. throw new IllegalArgumentException("Invalid log4jConfigLocation parameter: " + ex.getMessage());
  88. }
  89. }
  90. }
  91. /**
  92. * Shutdown Log4J to release all file locks.
  93. */
  94. public static void shutdownLogging(ServletContext servletContext) {
  95. servletContext.log("Shutting down Log4J");
  96. Log4jConfigurer.shutdownLogging();
  97. }
  98. }