How to initialize log4j in vaadin applications

Hello!

I’m developing a Vaadin application and I test it using Jetty.

When starting the web server (
mvn jetty:run
), it is possible to specify a log4j config file.

I do this and the log file is actually created. However, the log file contains only log statements of Jetty, but not those generated by my application.

So, my question is - how and where should log4j be configured so that log statements are written into the log file?

Currently, I do this in the applicationInit method of my application class:

import eu.livotov.tpt.TPTApplication;
import eu.livotov.tpt.i18n.TM;

public class MyApplication extends TPTApplication {
	@Override
	public void applicationInit() 
	{
		BasicConfigurator.configure();
		...
	}

It doesn’t work. If I use
PropertyFileConfigurator
instead of
BasicConfigurator
, it doesn’t work, either.

Thanks in advance for any hint

Dmitri

Any thoughts on this? I’d like to get log4j going for my Vaadin app. Thanks.

Hi,

We use log4j in our apps, and it seems your problem is not really related to Vaadin but the way you have to initialize your logs in a webapp.
Just place a file log4j.properties in your classpath (src/main/resources in Eclipse for example) and declare your logger and appender.

like this for a console appender named stdout


log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%t]
: %m%n

then your logger and which appender it uses :

log4j.<your package of your vaadin webapp> = DEBUG,stdout

And finally in your code just declare and retrieve your logger like this

public static Logger log = Logger.getLogger(YourApplication.class);

You should be able to print some informations to the console

If your propertie file is not called with an other name, then u’ll have to declare something in your web.xml file to register it in order log4j look for the configuration

some piece of code (not tested)

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/yourlog4j.properties</param-value>
</context-param>

Regards,

Hello!

In the meantime I have solved the problem.

Actually, I recommend to use
logback
, as log4j has problems when running a Vaadin application in Jetty (
mvn jetty:run
).

With log4j, the log file produced by Jetty contained the log messages of the web server, but not those of my application (and I could not find a solution with reasonable effort).

So I switched to logback and now it works perfectly.

Best regards

Dmitri