Skip to main content

To the Hell with the JDK Logging: II

Posted by schaefa on August 28, 2007 at 11:41 AM PDT

After several futile attempts to get my deployment units to log to Glassfish I finally figured out to use Log4J in Glassfish which is not really well documented there. So that is what I had to do:

  1. Place log4j.jar and log4j.properties file somewhere (I put it into <Glassfish Home>/lib/log)
  2. Add the fully qualified path to the log4j.jar into the classpath of Glassfish together with the directory that contains the log4j.properties file. You can do that in the web UI: Application Server/JVM Settings/Paths and the entries are separated by a return.
  3. Restart the server

Now you will find the log file relative to the <Glassfish Domain Directory>/config directory. Now I can read the log4j output like usual even though not in the same file as the Glassfish log statements.

Now some feedback to the responses I got:

  1. I could use SLF4J but this does not help for the classes provided by ServiceMix and that are the more important logging statements
  2. Well, Log4J was here first and so many projects and products were using Log4J way before JDK 1.4. And because logging is such a wide spread component so it the pain the incompatibility is causing.
  3. If Sun had provided a superior implementation that Log4J I could bite the bullet but they did not and so the whole idea of the JDK logging is ridiculous. And therefore I also don't want a JSR because that is making it even more painful.

Moral of the story is that if Sun could have swallowed their pride and either kept Log4J out of the JDK or just incorporated Log4J as is like they did with Corba we would not have this discussion. Now we have several frameworks on top of them that are trying to fix that and each of them fail one way or the other, what a mess. Thank you - Sun.

Comments

Glassfish v3 Instructions

Glassfish v3 users will notice that the System Classpath settings are not editable in the admin anymore.

An alternative that works in v3 has been posted 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:

-Dlog4j.configuration=[file:///$]{com.sun.aas.instanceRoot}/config/log4j.properties"

Log4J conf

Basically packaging your log4j.properties with your ejb.jar or other deployment packaging works as well Then you can have your log file in your domain log folder like this: log4j.rootLogger=DEBUG, FILE log4j.logger.my.company=DEBUG log4j.appender.FILE=org.apache.log4j.RollingFileAppender log4j.appender.FILE.File=${com.sun.aas.instanceRoot}/logs/fundd.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

I fought with this in a clustered Glasshfish 3.1.2.2 ...

I fought with this in a clustered Glasshfish 3.1.2.2 Narrowed it down to :

1) Place the log4j.jar file in each node/instance[x]/lib/ext directories
2) Place the config file either log4j.properties or log4j.xml in each node/instance[x]/config directory
3) Added the JVM Settings : JVM Options -Dlog4j.configuration=file:///${com.sun.aas.instanceRoot}/config/log4j.xml (I use xml and not properties file) to the clustr-config

I configured the file in log4j.xml to be :

Works great, when I go to Cluster - Domain Logs and gather logs for the cluster, they are included in the zip download.

Found a Gotcha, when you edit config under the server, this ...

Found a Gotcha, when you edit config under the server, this completely over writes the nodes so, these need to go under the domain directories as well as the node directories