Wednesday Dec 15, 2010

Configure Log4J for use in GlassFish 3.1

How do I configure Log4J for use in GlassFish 3.1?

By default GlassFish does not provide Log4J support and therefore most likely any Log4J statements end up in thin air. In order to make it work the Log4J archive must be made available to GlassFish alongside with a Log4J configuration file either as properties or XML.

Installation of Log4J

1. Copy log4j.jar inside the GlassFish_Home/lib.
2. Put the Log4J configuration file inside GlassFish_Home/domains/domain1/config.
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}/config/log4j.properties', as you have copied log4j.properties under config folder in above Step 2.
6. Click on the Save button.
7. Restart the GlassFish.
8. Deploy sample app and check if the Log4J statements are now available.

Sample content of Log4J properties file (log4j.properties):

log4j.rootLogger=DEBUG
log4j.logger.myapp=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 

Sample App to test Log4J (HelloServlet.java):

package myapp;

import org.apache.log4j.\*;
//other required imports

public class HelloServlet extends HttpServlet {

	private Logger logger = Logger.getLogger(getClass().getName());

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

		System.out.println("Servlet processing do get..");
		BasicConfigurator.configure();
		logger.debug("Logging Log4 Debug Statement...");
		logger.info("Logging Log4 Info Statement...");
	}
}

Now, when you run above sample you can find the log4J logging statement under server.log file as below:

[#|2010-12-14T21:18:41.436+0530|SEVERE|GlassFish3.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=17;_ThreadName=Thread-1;|Servlet processing do get..|#]

[#|2010-12-14T21:18:41.459+0530|INFO|GlassFish3.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=17;_ThreadName=Thread-1;|0 [http-thread-pool-8080(1)] DEBUG myapp.HelloServlet  - Logging Log4 Debug Statement...
|#]

[#|2010-12-14T21:18:41.460+0530|INFO|GlassFish3.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=17;_ThreadName=Thread-1;|6 [http-thread-pool-8080(1)] INFO myapp.HelloServlet - Logging Log4 Info Statement...
|#]

Sometimes user needs that all log4J statements should be part of some other file apart from server.log. How could I do that?

If you need to get log4J statemetn in different file(log4j.log) configured Log4J properties file (log4j.properties) as below:

log4j.rootLogger=DEBUG, FILE
log4j.logger.myapp=DEBUG
log4j.appender.FILE=org.apache.log4j.RollingFileAppender 
log4j.appender.FILE.File=/space/gfv3/v3setup/GlassFish3/GlassFish/domains/domain1/logs/log4j.log
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 
Here, rootLogger is configured with FILE option and added new configuration log4j.appender.FILE.File in properties file.

Now, when you run sample new log4j.log file is created as you had configured above and all log4J logging statement goes under log4j.log file as below:

 14 Dec 2010 21:18:41,454 DEBUG HelloServlet : Logging Log4 Debug Statement...
 14 Dec 2010 21:18:41,460 INFO  HelloServlet : Logging Log4 Info Statement...

Download sample App from Here. Deploy same on GlassFish 3.1 and Access App by launching browser to http://localhost:8080/hello.


About

Naman

Search

Categories
Archives
« July 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today