Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Tip - Logging with GlassFish

Kurt Berg
1 decade ago Apr 17, 2011 7:32pm

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

Last updated on Apr, 18th 2011
Marko Grönroos
1 decade ago Apr 17, 2011 9:56pm