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

  • Log4J properties injection through Spring


    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">

    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" >


    <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"/>

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


    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.


  • #2
    what you are trying to say is "how to load an xml in the spring context"
    Last edited by guillermodl; Oct 30th, 2008, 09:53 AM.


    • #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.


      • #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.


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

          <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
          	<property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
          	<property name="targetMethod" value="initLogging" />
          	<property name="arguments">