Hi everybody,
I was just wondering if its possible to override log4j configuration in spring ApplicationContxt.
thank you,
muhwas
Hi everybody,
I was just wondering if its possible to override log4j configuration in spring ApplicationContxt.
thank you,
muhwas
There are some examples of configuring log4j in the web.xml with Spring in the petclinic example. What exactly are you trying to do?
i have one log4j.xml file in WEB-INF directory. which define logging configuration for different pakages. I want to also diffine configuration inside applicationContext so that when if i want to change loging level i just change in the applicationContext and refresh the applicationContext so i will automatically read the new configuration without restarting the webapp.
You can use JMX to reconfigure log4j at runtime. That might be easier.
You can use the org.springframework.web.util.Log4jConfigListener,
I use my own custom bean which i add to my context:
Context:Code:package com.mad.util.log; import java.io.FileNotFoundException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.util.Log4jConfigurer; /** * @author Daniel Rijkhof (daniel.rijkhof@gmail.com) */ public class Log4jDirectConfigurer implements InitializingBean { private static final Log logger = LogFactory.getLog( Log4jDirectConfigurer.class ); private String location; private long refreshInterval; private String logdirKey = "webapp.logDir"; public void setLocation( String location ) { this.location = location; } public void setRefreshInterval( long refreshInterval ) { this.refreshInterval = refreshInterval; } public void setLogdirKey( String logdirKey ) { this.logdirKey = logdirKey; } public void setLogdirValue( String logdir ) { String value = System.getProperty( logdirKey ); if( value != null && !value.equals( logdir ) ) { throw new IllegalStateException("Logdir system property already set to different value: '" + logdirKey + "' = \"" + value + "\" instead of \"" + logdir + "\" - " + "Choose unique values for 'logdirKey' for every webapp!"); } System.setProperty( logdirKey, logdir ); } public void afterPropertiesSet() { if( location == null ) return; try { if( refreshInterval == 0 ) Log4jConfigurer.initLogging( location ); else Log4jConfigurer.initLogging( location, refreshInterval ); } catch (FileNotFoundException e) { logger.error(e); } } }
The ${env.dir} is a system prop, and the ${webapp.logDir} is filled through a property placeholder.Code:<!-- Log4j Configurer --> <bean id="log4jDirectConfigurer" class="com.mad.util.log.Log4jDirectConfigurer"> <property name="location" value="classpath:com/mad/project/env/${env.dir}/log4j.xml"/> <property name="refreshInterval" value="0"/> <property name="logdirKey" value="webapp.logDir"/> <property name="logdirValue" value="${webapp.logDir}"/> </bean>
I set a env var 'webapp.logDir' that i use in my log4j.xml file:
Code:<param name="File" value="${webapp.logDir}/default.log" />
Can't you use just use the Logger.getLogger(somepackage.class.getName()) to get the package and change the level on the fly. You could also call the root level logger so the entire web app is logging at the same level. I am writing a small support service which will allow the admin from the website to change the log level on the services. I am also going to try and implement a buffer or container which will display the last 10 on the screen and update it with ajax.
hi to all.
I have put that log4j.xml file in classpath at time of loading app.
and then in spring config I am using that one from classpath.
but at runtime I am getting an errorHTML Code:<bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="staticMethod"> <value>org.springframework.util.Log4jConfigurer.initLogging</value> </property> <property name="arguments"> <list> <value>log4j.xml</value> </list> </property> </bean>
could you please help me on this.HTML Code:Loading Spring Context Started log4j:ERROR Could not parse url [file:/app/gbb/dev1/gbbTi/bin/etc/log4j.xml]. java.io.FileNotFoundException: /app/gbb/dev1/gbbTi/bin/etc/log4j.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:106) at java.io.FileInputStream.<init>(FileInputStream.java:66) at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70) at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161) at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:973) at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:184) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:798) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148) at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:250) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:292) at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:146) at org.apache.log4j.xml.DOMConfigurator$2.parse(DOMConfigurator.java:690) at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:789) at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:696) at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:821) at org.springframework.util.Log4jConfigurer.initLogging(Log4jConfigurer.java:70) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:276) at org.springframework.beans.factory.config.MethodInvokingFactoryBean.doInvoke(MethodInvokingFactoryBean.java:160) at org.springframework.beans.factory.config.MethodInvokingFactoryBean.afterPropertiesSet(MethodInvokingFactoryBean.java:150) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409) at java.security.AccessController.doPrivileged(Native Method) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) at com.csfb.gbb.traceinterface.ApplicationContextLookup.loadContext(ApplicationContextLookup.java:51) at com.csfb.gbb.traceinterface.TraceInterfaceClient.main(TraceInterfaceClient.java:37)
You've dug up a pretty old thread. As your error says, it can't find the xml file. It should work if you put "classpath:log4j.xml" instead.
If it's a web app, you can instead put a listener into web.xml:
Code:<!-- location of log4j config file --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/log4j.xml</param-value> </context-param> <!-- applies log4j configuration --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>