org.springframework.util
Class Log4jConfigurer

java.lang.Object
  extended byorg.springframework.util.Log4jConfigurer

public abstract class Log4jConfigurer
extends Object

Convenience class that features simple methods for custom Log4J configuration.

Only needed for non-default Log4J initialization, for example with a custom config location or a refresh interval. By default, Log4J will simply read its configuration from a "log4j.properties" file in the root of the class path.

For web environments, the analogous Log4jWebConfigurer class can be found in the web package, reading in its configuration from context-params in web.xml. In a J2EE web application, Log4J is usually set up via Log4jConfigListener or Log4jConfigServlet, delegating to Log4jWebConfigurer underneath.

Since:
13.03.2003
Author:
Juergen Hoeller
See Also:
Log4jWebConfigurer, Log4jConfigListener, Log4jConfigServlet

Field Summary
static String CLASSPATH_URL_PREFIX
          Pseudo URL prefix for loading from the class path: "classpath:"
static long DEFAULT_REFRESH_INTERVAL
          Deprecated. Either choose no config file refreshing (initLogging with plain location) or specify an explicit refresh interval. As of Spring 1.1.3, there is no default refresh interval; the default is no config file refreshing now. The rationale is to avoid Log4J's watchdog thread (which never terminates) unless explicitly required.
static String XML_FILE_EXTENSION
           
 
Constructor Summary
Log4jConfigurer()
           
 
Method Summary
static void initLogging(String location)
          Initialize Log4J from the given file location, with no config file refreshing.
static void initLogging(String location, long refreshInterval)
          Initialize Log4J from the given location, with the given refresh interval for the config file.
static void setWorkingDirSystemProperty(String key)
          Set the specified system property to the current working directory.
static void shutdownLogging()
          Shut down Log4J, properly releasing all file locks.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASSPATH_URL_PREFIX

public static final String CLASSPATH_URL_PREFIX
Pseudo URL prefix for loading from the class path: "classpath:"

See Also:
Constant Field Values

DEFAULT_REFRESH_INTERVAL

public static final long DEFAULT_REFRESH_INTERVAL
Deprecated. Either choose no config file refreshing (initLogging with plain location) or specify an explicit refresh interval. As of Spring 1.1.3, there is no default refresh interval; the default is no config file refreshing now. The rationale is to avoid Log4J's watchdog thread (which never terminates) unless explicitly required.

Default refresh interval, previously used for initLogging(location).

See Also:
initLogging(String), initLogging(String, long), Constant Field Values

XML_FILE_EXTENSION

public static final String XML_FILE_EXTENSION
See Also:
Constant Field Values
Constructor Detail

Log4jConfigurer

public Log4jConfigurer()
Method Detail

initLogging

public static void initLogging(String location)
                        throws FileNotFoundException
Initialize Log4J from the given file location, with no config file refreshing. Assumes an XML file in case of a ".xml" file extension, and a properties file else.

Parameters:
location - the location of the config file: either a "classpath:" location (e.g. "classpath:myLog4j.properties"), an absolute file URL (e.g. "file:C:/log4j.properties), or a plain absolute path in the file system (e.g. "C:/log4j.properties")
Throws:
FileNotFoundException - if the location specifies an invalid file path
See Also:
DEFAULT_REFRESH_INTERVAL

initLogging

public static void initLogging(String location,
                               long refreshInterval)
                        throws FileNotFoundException
Initialize Log4J from the given location, with the given refresh interval for the config file. Assumes an XML file in case of a ".xml" file extension, and a properties file else.

Log4J's watchdog thread will asynchronously check whether the timestamp of the config file has changed, using the given interval between checks. A refresh interval of 1000 milliseconds (one second), which allows to do on-demand log level changes with immediate effect, is not unfeasible.

WARNING: Log4J's watchdog thread does not terminate until VM shutdown; in particular, it does not terminate on LogManager shutdown. Therefore, it is recommended to not use config file refreshing in a production J2EE environment; the watchdog thread would not stop on application shutdown there.

Parameters:
location - the location of the config file: either a "classpath:" location (e.g. "classpath:myLog4j.properties"), an absolute file URL (e.g. "file:C:/log4j.properties), or a plain absolute path in the file system (e.g. "C:/log4j.properties")
refreshInterval - interval between config file refresh checks, in milliseconds
Throws:
FileNotFoundException - if the location specifies an invalid file path

shutdownLogging

public static void shutdownLogging()
Shut down Log4J, properly releasing all file locks.

This isn't strictly necessary, but recommended for shutting down Log4J in a scenario where the host VM stays alive (for example, when shutting down an application in a J2EE environment).


setWorkingDirSystemProperty

public static void setWorkingDirSystemProperty(String key)
Set the specified system property to the current working directory.

This can be used e.g. for test environments, for applications that leverage Log4jWebConfigurer's "webAppRootKey" support in a web environment.

Parameters:
key - system property key to use, as expected in Log4j configuration (for example: "demo.root", used as "${demo.root}/WEB-INF/demo.log")
See Also:
Log4jWebConfigurer


Copyright (C) 2003-2004 The Spring Framework Project.