While there are a lot of resources describing how to log using log4j with Glassfish, none of them describe how to configure the Glassfish instance that Netbeans uses while debugging web applications.

If you debug a Netbeans web application that uses log4j, you’ll see the following in the GlassFish Server output:

SEVERE: log4j:WARN No appenders could be found for logger (class).
SEVERE: log4j:WARN Please initialize the log4j system properly.
SEVERE: log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Instead, wouldn’t it be nice to have log4j logs appear in the Netbeans Output window?  To accomplish this, you need to configure the Glassfish server that was installed by Netbeans, which is slightly different from a standalone server.

Configure Glassfish

If you don’t already have Glassfish installed and working with Netbeans, you can either re-install using one of the bundles that includes Glassfish or you can add it manually.  Let’s assume Glassfish is installed in $GLASSFISH_HOME.  Then:

  1. Copy log4j-X.X.X.jar to $GLASSFISH_HOME/lib
  2. While Glassfish is running (start it from Netbeans), browse to the server’s admin pages at http://localhost:4848.
  3. Click server-config -> JVM Settings -> JVM Options
  4. Add a new option:  -Dlog4j.configuration=file:///${com.sun.aas.instanceRoot}/config/log4j.properties
  5. Save the settings and restart Glassfish using the Netbeans IDE
  6. Watch the Glassfish Output window in the IDE for any errors

This is rather similar to installing log4j on a stand-alone server.

Create log4j.properties

There are plenty of tutorials about log4j.properties, but to make the log output appear in the Netbeans console you must include a Console appender that writes to System.out.  Here’s a very basic config:

log4j.rootLogger=DEBUG, stdout
 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 log4j.appender.stdout.Target=System.out
 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
 log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %5p - %m%n

I use the DEBUG logging level but you can change it to suit your needs.  This setup actually outputs logs to the Glassfish server log, which is then displayed by Netbeans.  If you use this same log4j.properties file on a stand-alone server, logs will appear in ${com.sun.aas.instanceRoot}/logs/server.log, which is typically equivalent to $GLASSFISH_HOME/domains/domain1/logs/server.log

Install log4j.properties for Netbeans

On a stand-alone server this file would go in $GLASSFISH_HOME/domains/domain1/config, but Netbeans  sets a value for ${com.sun.aas.instanceRoot} that is different from typical installs.  To find out the actual value, use the Netbeans IDE:

  1. Select Tools -> Servers -> GlassFish
  2. Right-click on GlassFish and choose Properties
  3. Observe the value of the Domains Folder – this is equivalent to ${com.sun.aas.instanceRoot} and probably points somewhere within your home/user directory.

Copy log4j.properties into this directory.

Restart the Application

From the Netbeans IDE, run or debug your application.  Log messages will now appear in the GlassFish Server Output window.  Anytime you make changes to log4j.properties, you’ll need to restart the Glassfish server before the changes take effect.

When it comes time to deploy your application, you can use this same method and properties file (maybe change the logging level!) to write your application logs to ${com.sun.aas.instanceRoot}/logs/server.log.  The only difference between Netbeans and a stand-alone Glassfish server setup is the path to ${com.sun.ass.instanceRoot} and $GLASSFISH_HOME.

Tagged with →  
Share →