Re: log4j2 web lookup questions
Hi Ralph, I had a dig around to see what might be triggering the log4j configuration off before the servlet has had a chance to initialise and, I believe it might be the Spring code itself? Our deployments are all Spring 4 and java annotation based. i.e. no xml configuration aside from simple WEB-INF/web.xml file. So, a deployment init and startup is generally kicked off via the following code: import javax.servlet.ServletContext; import javax.servlet.ServletException; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; public class WebInitialiser implements WebApplicationInitializer { public void onStartup(ServletContext servletContext) throws ServletException { // log4j2 web lookup caused by the creation of the rootContext object. AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(ApplicationConfig.class, IntegrationConfig.class, JmsConfig.class, JmxConfig.class); servletContext.addListener(new ContextLoaderListener(rootContext)); } } Now, I think the log4j2's web lookup failure arises because of the creation of the AnnotationConfigWebApplicationContext class object (If I comment the creation of that object out, then the servlet starts up okay and log4j2's web lookup resolves okay too - but of course the servlet is useless as it no longer has our business code loaded behind it!). I had a look inside the AnnotationConfigWebApplicationContext class and, I can see that the class extends org.springframework.context.support.AbstractApplicationContext which has the following line: protected final Log logger = LogFactory.getLog(getClass());. The Log and LogFactory classes are imported as follows: import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; Can this be the reason why the log4j2 configuration is being scanned before the servlet context has had a chance to initialise? And therefore log4j2 is unable to do the web lookup? The spring-web project does have a web-fragment defined in it called spring_web and in my deployment's web.xml I have tried including the following snippet: service-name log4j spring_web But it doesn't solve the web lookup issue. (I am not sure how to prove whether the fragment ordering is actually being followed during startup though...) Any thoughts on any of this would be much appreciated. I have so far not even have any workarounds for this issue... Cheers, PM.
Re: log4j2 web lookup questions
If the code below is yours than I would recommend having it extend Log4jServletContainerInitializer and then have the first line of your onStartup() method do super.onStartup(). That should make sure that Log4j initializes before Spring. You can also remove the Log4jServletContextListener from your web.xml if it is there. Ralph > On Apr 11, 2015, at 4:05 AM, Ponder Muse wrote: > > Hi Ralph, > > I had a dig around to see what might be triggering the log4j configuration > off before the servlet has had a chance to initialise and, I believe it > might be the Spring code itself? > > Our deployments are all Spring 4 and java annotation based. i.e. no xml > configuration aside from simple WEB-INF/web.xml file. So, a deployment init > and startup is generally kicked off via the following code: > > import javax.servlet.ServletContext; > import javax.servlet.ServletException; > > import org.springframework.web.WebApplicationInitializer; > import org.springframework.web.context.ContextLoaderListener; > import > org.springframework.web.context.support.AnnotationConfigWebApplicationContext; > > public class WebInitialiser implements WebApplicationInitializer { > >public void onStartup(ServletContext servletContext) throws > ServletException { > >// log4j2 web lookup caused by the creation of the rootContext > object. >AnnotationConfigWebApplicationContext rootContext = new > AnnotationConfigWebApplicationContext(); > >rootContext.register(ApplicationConfig.class, > IntegrationConfig.class, JmsConfig.class, JmxConfig.class); > >servletContext.addListener(new ContextLoaderListener(rootContext)); > >} > } > > Now, I think the log4j2's web lookup failure arises because of the creation > of the AnnotationConfigWebApplicationContext class object (If I comment the > creation of that object out, then the servlet starts up okay and log4j2's > web lookup resolves okay too - but of course the servlet is useless as it > no longer has our business code loaded behind it!). > > I had a look inside the AnnotationConfigWebApplicationContext class and, I > can see that the class extends > org.springframework.context.support.AbstractApplicationContext which has > the following line: > >protected final Log logger = LogFactory.getLog(getClass());. > > The Log and LogFactory classes are imported as follows: > >import org.apache.commons.logging.Log; >import org.apache.commons.logging.LogFactory; > > Can this be the reason why the log4j2 configuration is being scanned before > the servlet context has had a chance to initialise? And therefore log4j2 is > unable to do the web lookup? > > The spring-web project does have a web-fragment defined in it called > spring_web and in my deployment's web.xml I have tried including the > following snippet: > > service-name > > > log4j >spring_web > > > > But it doesn't solve the web lookup issue. (I am not sure how to prove > whether the fragment ordering is actually being followed during startup > though...) > > Any thoughts on any of this would be much appreciated. I have so far not > even have any workarounds for this issue... > > Cheers, > PM. - To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org For additional commands, e-mail: log4j-user-h...@logging.apache.org
Re: log4j2 web lookup questions
Hello Ralph, Your recommendation for the code has worked well and the web lookup is now working as needed - which is great. Thanks again for all the help! PM. On 11 April 2015 at 17:58, Ralph Goers wrote: > If the code below is yours than I would recommend having it extend > Log4jServletContainerInitializer and then have the first line of your > onStartup() method do super.onStartup(). That should make sure that Log4j > initializes before Spring. You can also remove the > Log4jServletContextListener from your web.xml if it is there. > > Ralph > > > On Apr 11, 2015, at 4:05 AM, Ponder Muse > wrote: > > > > Hi Ralph, > > > > I had a dig around to see what might be triggering the log4j > configuration > > off before the servlet has had a chance to initialise and, I believe it > > might be the Spring code itself? > > > > Our deployments are all Spring 4 and java annotation based. i.e. no xml > > configuration aside from simple WEB-INF/web.xml file. So, a deployment > init > > and startup is generally kicked off via the following code: > > > > import javax.servlet.ServletContext; > > import javax.servlet.ServletException; > > > > import org.springframework.web.WebApplicationInitializer; > > import org.springframework.web.context.ContextLoaderListener; > > import > > > org.springframework.web.context.support.AnnotationConfigWebApplicationContext; > > > > public class WebInitialiser implements WebApplicationInitializer { > > > >public void onStartup(ServletContext servletContext) throws > > ServletException { > > > >// log4j2 web lookup caused by the creation of the rootContext > > object. > >AnnotationConfigWebApplicationContext rootContext = new > > AnnotationConfigWebApplicationContext(); > > > >rootContext.register(ApplicationConfig.class, > > IntegrationConfig.class, JmsConfig.class, JmxConfig.class); > > > >servletContext.addListener(new > ContextLoaderListener(rootContext)); > > > >} > > } > > > > Now, I think the log4j2's web lookup failure arises because of the > creation > > of the AnnotationConfigWebApplicationContext class object (If I comment > the > > creation of that object out, then the servlet starts up okay and log4j2's > > web lookup resolves okay too - but of course the servlet is useless as it > > no longer has our business code loaded behind it!). > > > > I had a look inside the AnnotationConfigWebApplicationContext class and, > I > > can see that the class extends > > org.springframework.context.support.AbstractApplicationContext which has > > the following line: > > > >protected final Log logger = LogFactory.getLog(getClass());. > > > > The Log and LogFactory classes are imported as follows: > > > >import org.apache.commons.logging.Log; > >import org.apache.commons.logging.LogFactory; > > > > Can this be the reason why the log4j2 configuration is being scanned > before > > the servlet context has had a chance to initialise? And therefore log4j2 > is > > unable to do the web lookup? > > > > The spring-web project does have a web-fragment defined in it called > > spring_web and in my deployment's web.xml I have tried including the > > following snippet: > > > > service-name > > > > > > log4j > >spring_web > > > > > > > > But it doesn't solve the web lookup issue. (I am not sure how to prove > > whether the fragment ordering is actually being followed during startup > > though...) > > > > Any thoughts on any of this would be much appreciated. I have so far not > > even have any workarounds for this issue... > > > > Cheers, > > PM. > > > - > To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org > For additional commands, e-mail: log4j-user-h...@logging.apache.org > >