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.


Comments:

Hi Naman,

but how about replacing the java.util.logging (JUL) by LOG4J completely. The above logging is just on top of the standard JUL logging of Glassfish.

Is it possible to replace the LogManager and what is the correct/recommended way to do this?

Thanks, Jörg

Posted by Jörg Thönnes on December 15, 2010 at 11:55 AM IST #

.

Posted by Jörg Thönnes on December 15, 2010 at 11:56 AM IST #

Hi Jörg,

this blog explains how to replace java.util.logging by SLF4J in Glassfish:

http://hwellmann.blogspot.com/2010/12/glassfish-logging-with-slf4j-part-2.html

The example uses logback as logging backend, but the same should work with log4j.

Regards,
Harald

Posted by Harald Wellmann on December 21, 2010 at 07:51 PM IST #

Would it be possible to post that sample myapp.war file you describe to this blog for others to use? Thanks!

Posted by Steve Friedberg on December 22, 2010 at 02:27 PM IST #

This is fine for when you only have 1 application deployed to your server.
If you have more than 1, you wouldn't want their log output to be written to the same log file, so I'm guessing that you would need to provide the log4j.properties file as part of the application EAR or WAR file.
How would that be configured up?

Posted by Chris on January 26, 2011 at 11:07 AM IST #

Posted sample war file at the end of blog. Use the same to test.

Posted by Naman Mehta on March 08, 2011 at 03:23 AM IST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Naman

Search

Categories
Archives
« April 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
   
       
Today