How do I configure Log4J for use in GlassFish?

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. The instruction steps here are designed not to rely on Glassfish default loading bye making it explicit.

Installation of Log4J

  1. Create a new directory inside of <Glassfish Home>/lib directory which is called logging for now
  2. Put log4j.jar inside the newly created directory logging
  3. Put the Log4J configuration file inside the logging directory as well
  4. Fire up Glassfish and open the admin web UI inside a browser (for example _*http://localhost:4848*_)
  5. Click on Application Server, then JVM Settings tab and finally the Path Settings tab
  6. In the System Classpath box enter in the first line fully qualified path to the Log4J archive, hit enter to go to the next line and then add the fully qualified path to the directory that contains the Log4J settings file
  7. Click on the Save button
  8. Restart the Glassfish domain
  9. Check if the Log4J statements are now available

Notes and Adjustments

Because the System Classpath contains fully qualified paths you can have the Log4J archive and the Log4J settings file anywhere you like. You may want to consider the following:

  1. You can use *$
    Unknown macro: {as.install*}

    to reference the Glassfish installation directory so that the installation directory can be relocated

  2. If you are using multiple installations it is probably best to place the Log4J settings inside the domain directory so that you can adjust it for each domain
  3. Or you could use a Glassfish property so that at least the output file (if used) is placed inside its own domain
  4. Log4J console output may be lost so you a file or other type of appender

Have fun - Andreas Schaefer



These instructions pertain to Glassfish v2.1 and older, since the System Classpath settings are no longer editable in Glassfish v3 admin.

I found an alternative that works in v3 here: nzpcmad.blogspot.com/2009/10/log4j-path-to-properties-file.html

The instructions are:

For Glassfish, we normally put the log4j.properties in the "domain / config" directory.

To enable Glassfish to find it, add the following to the Glassfish JVM settings. Easiest way is to use the Admin console i.e. Application Server / JVM Settings / JVM Options.

Add this line:

Java Opt
-Dlog4j.configuration=[file:///$]{com.sun.aas.instanceRoot}/config/log4j.properties
Posted by james_r at Jul 07, 2010 13:30