0
votes

I'm running a Vaadin Servlet with a very simple code and I want to log with Log4j when the application starts/ends, and the same for the sessions of the clients. However, only some of the messages that are logged end up being written on the log file itself.

The code of the servlet is the following:

@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class, widgetset = "som.vaadin.AppWidgetSet")
public static class Servlet extends VaadinServlet implements SessionInitListener, SessionDestroyListener {

private static final Logger LOGGER = LogManager.getLogger(Servlet.class);

@Override
protected void servletInitialized() throws ServletException   {
    super.servletInitialized();
    LOGGER.info("App started");
    getService().addSessionInitListener(this);
    getService().addSessionDestroyListener(this);
}

@Override
public void destroy() {
    LOGGER.info("App stopped");
    super.destroy();
}

@Override
public void sessionInit(SessionInitEvent event) {
    LOGGER.info("Session started");
}

@Override
public void sessionDestroy(SessionDestroyEvent event) {
    LOGGER.info("Session expired");
}
}

My log4j log configuration file is the following:

log4j.rootLogger=DEBUG, console, RollingAppender
log4j.appender.RollingAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingAppender.File=c:/temp/log/error_som.log
log4j.appender.RollingAppender.DatePattern='.'yyyy-ww
log4j.appender.RollingAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingAppender.layout.ConversionPattern= [%d{ISO8601}] %5p%6.6r[%t]%x(%F:%L) - %m%n

And finally, what I get in the log file is just:

[2015-06-09 14:17:18,197] INFO 6042 (MainBusiness.java:122) - App started

I have checked by debugging that sessionInit() is called, and the LOGGER.info() call inside it is also performed. However, the log4j Appender only received the message corresponding to the servletInitialized() function.

Any idea on what can be happening?

Thanks, Cris

1
Are you sure the sessionXXX methods are called, and it's realy only the logging not working? I would almost beat, that (for some reason) the sessionXXX methods are not called at all - André Schild
Override finalize method and try log sth there. - kukis
@AndréSchild I'm sure that they are being called. If a go step by step in debug mode, the LOGGER.info() calls are executed. However, the message did not end up in the log file. - crigore
@kukis Same result. I start to think that is something really wrong with the whole application. - crigore

1 Answers

0
votes

Finally, I got what was going on. Though, I cannot understand it.

The issue was caused by conflicting servlet configurations and init-params from the annotations shown in the above code and the contents of the web.xml. The VaadinServlet was define twice, with different configurations including the configuration file for the log4j library. And sometimes, the servlet was using one and sometimes it was using the other one, even inside the same object!

So, I got it solved, but I cannot see why it was really happening.