Community   SpringSource   Projects    Downloads    Documentation    Forums    Training   Exchange   Blogs

Go Back   Spring Community Forums > Core Spring Projects > Core Container

Reply
 
Thread Tools Display Modes
  #1  
Old Apr 24th, 2007, 04:06 PM
muhwas muhwas is offline
Junior Member
 
Join Date: Feb 2007
Posts: 24
Default log4j configuration in applicationContext

Hi everybody,

I was just wondering if its possible to override log4j configuration in spring ApplicationContxt.

thank you,
muhwas
Reply With Quote
  #2  
Old Apr 24th, 2007, 04:17 PM
karldmoore karldmoore is offline
Senior Member
 
Join Date: Sep 2006
Posts: 8,425
Default

There are some examples of configuring log4j in the web.xml with Spring in the petclinic example. What exactly are you trying to do?
Reply With Quote
  #3  
Old Apr 24th, 2007, 04:26 PM
muhwas muhwas is offline
Junior Member
 
Join Date: Feb 2007
Posts: 24
Default

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.
Reply With Quote
  #4  
Old Apr 24th, 2007, 04:34 PM
karldmoore karldmoore is offline
Senior Member
 
Join Date: Sep 2006
Posts: 8,425
Default

You can use JMX to reconfigure log4j at runtime. That might be easier.
Reply With Quote
  #5  
Old Apr 25th, 2007, 03:16 AM
Daniel Rijkhof Daniel Rijkhof is offline
Junior Member
 
Join Date: Jan 2007
Posts: 9
Default Log4jDirectConfigurer

You can use the org.springframework.web.util.Log4jConfigListener,

I use my own custom bean which i add to my 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);
		}
		
		
	}
	
}
Context:
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>
The ${env.dir} is a system prop, and the ${webapp.logDir} is filled through a property placeholder.

I set a env var 'webapp.logDir' that i use in my log4j.xml file:
Code:
<param name="File" value="${webapp.logDir}/default.log" />
Reply With Quote
  #6  
Old Mar 27th, 2008, 01:40 PM
kramik1 kramik1 is offline
Junior Member
 
Join Date: Jan 2008
Posts: 15
Default use setlevel

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.
Reply With Quote
  #7  
Old Feb 8th, 2010, 02:30 PM
ravespandya ravespandya is offline
Junior Member
 
Join Date: Jan 2010
Posts: 19
Default Classpath problem

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.
HTML 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>
but at runtime I am getting an error
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)
could you please help me on this.
Reply With Quote
  #8  
Old Feb 16th, 2010, 08:56 PM
lightbringer lightbringer is offline
Junior Member
 
Join Date: Feb 2010
Posts: 2
Default

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>
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:06 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.