Skip to main content

Changing Log Levels in Embedded GlassFish

Posted by bhavanishankar on December 14, 2010 at 11:59 PM PST

This blog shows how to programatically change the log levels of a particular logger in Embedded GlassFish.

I did a search on the internet for information on how to change the log levels of a particular logger in Embedded GlassFish. The only information I could find was http://docs.sun.com/app/docs/doc/821-1208/gjlfe?l=en&a=view which requires changing $JAVA_HOME/jre/lib/logging.properties. But I am not comfortable changing it at the JDK level.

So, I wrote the following piece of code. May be the Embedded GlassFish users will find it useful:


import org.glassfish.embeddable.*;

// Create Embedded GlassFish
GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish();

// Set the log levels. For example, set 'deployment' and 'server' log levels to FINEST
Logger.getLogger(
"").getHandlers()[0].setLevel(Level.FINEST);
Logger.getLogger(
"javax.enterprise.system.tools.deployment").setLevel(Level.FINEST);
Logger.getLogger(
"javax.enterprise.system").setLevel(Level.FINEST);

// Start Embedded GlassFish and deploy an application.
// You will see all the FINEST logs printed on the console.
glassfish.start();
glassfish.getDeployer().deploy(
new File("sample.war"));

// Dispose Embedded GlassFish
glassfish.dispose();

 

The above code uses GlassFish 3.1 Embedded APIs and should be run using latest Embedded GlassFish JAR (3.1-SNAPSHOT is here).

Another alternative non programmatic way is to create a custom logging configuration file (customlogging.properties) with the following contents:

handlers= java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = FINEST
javax.enterprise.system.tools.deployment.level = FINEST
javax.enterprise.system.level = FINEST

and pass it while invoking your application (say MyEmbeddedGlassFish), like this:

java -Djava.util.logging.config.file=customlogging.properties MyEmbeddedGlassFish

(Refer http://docs.sun.com/app/docs/doc/820-7692/abluj?l=en&a=view for the logger names)

Related Topics >>