Announcement Announcement Module
Collapse
No announcement yet.
Log4J properties injection through Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Log4J properties injection through Spring

    Hello

    I have a simple POJO application (no web or J2EE application) and need to configure Log4J through Spring so I can use properties injection.

    For example if you configure Hibernate through Spring, you can use properties injection, like this:

    <property name="driverClassName">
    <value>${db.driverClass}</value>
    </property>

    Spring will inject the value of the db.driverClass property wherever it finds the ${db.driverClass} expression.


    Is it possible to do the same thing with the Log4J configuration?.

    I have the following Log4J.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >

    <log4j:configuration>

    <appender name="general.appender" class="org.apache.log4j.FileAppender">
    <param name="Append" value="true"/>

    <param name="File" value="application.log"/>

    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d [%p] %m (%F:%L)%n"/>
    </layout>
    </appender>


    <root>
    <level value ="INFO"/>
    <appender-ref ref="general.appender"/>
    </root>

    </log4j:configuration>

    I have a property that stores the location for the log file and need this property to be injected in the log4j configuration, like this:

    <param name="File" value="${log.path}/application.log"/>

    I know Log4J itself can do this kind of injection if log.path were a system property, but that's not the case. The log.path property is a normal property loaded by Spring.

    Can Spring help with this property injection?.

    Any help would be appreciated.

    Thanks

  • #2
    what you are trying to say is "how to load an xml in the spring context"

    http://forum.springframework.org/showthread.php?t=37896
    Last edited by guillermodl; Oct 30th, 2008, 10:53 AM.

    Comment


    • #3
      What I'm trying to ask is:
      How to put a spring-loaded-property into the log4j configuration?.

      About the post you provided:

      The solution is to use the Log4jConfigurer class to set the configuration file location and the refresh period into the Log4J configuration and the location of the log file is set via a System property.

      I need to put the location of the log file in the Log4J configuration without setting a system property.

      Comment


      • #4
        Inject log path to log4j.xml

        This seems like a simple question. Why have we no answer? I have not found a solution elsewhere.

        Comment


        • #5
          It seems like you could use a Log4jConfigurer, but I'm not sure how to manipulate the properties that it manages.

          Code:
          <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
          	<property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
          	<property name="targetMethod" value="initLogging" />
          	<property name="arguments">
          		<list>
          			<value>conf/Log4j.xml</value>
          		</list>
          	</property>
          </bean>

          Comment

          Working...
          X