Showing posts with label Log4J. Show all posts
Showing posts with label Log4J. Show all posts

Tuesday, 3 September 2013

Runtime Configuration of log4j properties

log4j is used widely to provide logging in applications. There are numerous articles about how to configure log4j.properties or log4j.xml to enable/disable logging at various levels and categories. Often it is necessary to enable the debug log level for a short duration to identify the cause of a production issue. Without runtime configuration, this would require stopping the application, changing the log configuration (after extracting the properties file from the war) and then redeploying.

This post describes how log4j can be configured outside of the application and also changed at runtime.

File Watchdog

Log4j provides runtime configuration through the DOMConfigurator.configureAndWatch for XML files, or the PropertyConfigurator.configureAndWatch for properties file. Both these methods take the absolute path for the configuration file and a refresh interval. This allows the configuration file to be located outside of a web application war file and allow administrators to change the logging levels at runtime.

The configureAndWatch API can be invoked from a custom servlet listener. For users of the Spring framework, there already exists the Log4jConfigListener that is a wrapper to the Log4j configureAndWatch API. This listener is configured in the web applications web.xml.

Spring Log4jConfigListener

By specifying the log4jConfigLocation to a file outside the web application, it allows different environments (dev, test, prod) to have different levels of logging enabled. The log4jRefreshInterval specifies how often the log4j.properties file should be checked for changes and reloaded. This allows runtime changes to the log4j configuration to be performed.

JMX

Log4j also has built-in support for JMX and two classes org.apache.log4j.jmx.LoggerDynamicMBean and org.apache.log4j.HierarchyDynamicMBean are used to expose and register loggers as MBeans. However, it still requires wrappers to work to add loggers that are not defined at start-up.


Wednesday, 6 July 2011

Configure Glassfish with Log4J

In order to configure GlassFish with Log4J, add a log4j.properties to the glassfish server.

1. Copy log4j.jar inside the GlassFish_Home/lib.
2. Put the Log4J configuration file inside GlassFish_Home/lib
3. Fire up GlassFish and open the admin web UI inside a browser (for example http://localhost:4848).
4. Click on 'Server(Admin Server)' -> Click on 'server-config' Configuration -> JVM Settings -> JVM Options.
5. Now Add new property
'-Dlog4j.configuration=file:///${com.sun.aas.instanceRoot}/lib/log4j.properties' based on as you have copied log4j.properties under lib folder in above Step 2. (basically specify the path of log4j.properties file)
6. Click on the Save button and restart glassfish.

Sample content of Log4J properties file (log4j.properties):
log4j.rootLogger=DEBUG

# enabling Logging for my application
log4j.logger.=DEBUG

# enabling Logging for the spring framework
log4j.logger.org.springframework=DEBUG

log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.MaxFileSize=100KB
log4j.appender.FILE.MaxBackupIndex=1
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{DATE} %-5p %c{1} : %m%n