Tip - Logging with GlassFish

Hello all together,

this is actually not a question. It’s a post about how to realise logging with Vaadin and GlassFish.

I am new to Vaadin and GlassFish. So maybe some of you might think the following is quite obvious. Well it wasn’t for me. It took me a whole day to find out. But anyway, if the administrators think this is rubbish please delete this post.

Otherwise here we go:

I am using GlassFish 3.1 and Vaadin 6.5.4

GlassFish does not provide Log4J support by default. But there is an excellent
blog post
out there which describes how you can get Log4J to work in GlassFish.

Now if you have more than one application deployed to your domain you will get a pretty unclear log. To prevent this add a separate logger for each application to your
log4j.properties
file.

For example if this would be your
log4j.properties
file:


log4j.rootLogger=DEBUG, STDOUT, FILE

log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=[%d{ISO8601}]
%5p%6.6r[%t]
%x - %C.%M(%F:%L) - %m%n

log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=D:/_temp/logs/all_apps.log
log4j.appender.FILE.MaxFileSize=2000KB
log4j.appender.FILE.MaxBackupIndex=100
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[%d{ISO8601}]
%5p%6.6r[%t]
%x - %C.%M(%F:%L) - %m%n

And you have an application called
MySuperApp
, then add the following lines to the file:


log4j.logger.MYSUPERAPP=INFO, MYSUPERAPP_LOGGER
log4j.appender.MYSUPERAPP_LOGGER=org.apache.log4j.RollingFileAppender
log4j.appender.MYSUPERAPP_LOGGER.File=D:/_temp/logs/MySuperApp.log
log4j.appender.MYSUPERAPP_LOGGER.MaxFileSize=2000KB
log4j.appender.MYSUPERAPP_LOGGER.MaxBackupIndex=100
log4j.appender.MYSUPERAPP_LOGGER.layout=org.apache.log4j.PatternLayout
log4j.appender.MYSUPERAPP_LOGGER.layout.ConversionPattern=[%d{ISO8601}]
%5p%6.6r[%t]
%x - %C.%M(%F:%L) - %m%n

Put this in your MySuperApp classes:


...
private Logger appLogger = Logger.getLogger("MYSUPERAPP");
...
appLogger.info("Is this thing on?");

That’s it. Now log messages from MySuperApp classes will be directed to a separate file called
MySuperApp.log
.

Hope this can save some time for one or the other.

Regards

By the way, this
forum post
might be interesting too

Hi Kurt,

Excellent, thank you for sharing this. I too hope that it will help the quite many people who use Glassfish with Vaadin.