Announcement Announcement Module
Collapse
No announcement yet.
Log4j - configurable log4jConfigLocation in web.xml Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Log4j - configurable log4jConfigLocation in web.xml

    Hi all

    When I deploy my app to the production environment, the production people have mandated that log4j config files not be located within my webapp.

    Usually in web.xml I would have
    Code:
      <context-param>
          <param-name>log4jConfigLocation</param-name>
          <param-value>/WEB-INF/log4j.properties</param-value>
       </context-param>
    The log4.properties file location in production is however in a completely external directory. Is there a way to dynamically set log4jConfigLocation by some sort of property substitution. Ideally I'd be able to do something like

    Code:
      <context-param>
          <param-name>log4jConfigLocation</param-name>
          <param-value>&#123;log4j.location&#125;</param-value>
       </context-param>
    and have the production guys add a system property like
    -Dlog4j.location=/anexternal/path/log4j.properties

    How are people handling this sort of situation?

  • #2
    You could make this part of your build script and use substiution in Ant to replace the variable.

    Rob

    Comment


    • #3
      Keep in mind that the system property is a bad idea if there is going to be more than one application deployed, and they need different log4j configs.

      We use the ant replacement technique that Rob mentions to handle this.

      Comment


      • #4
        Thanks for the replies

        Yes, the ant replacement technique is an option but I dont necessarily know the value to specify when doing the build. The location of log4j.properties isnt really known until after I have delivered my webapp and the production guys deploy it somewhere.

        The production setup mandates only one webapp per app server instance so a system property approach if it were possible would be ok.

        The problem I'm having is that I dont think you can use the ${propname} substitution in web.xml can you?

        I guess my broader question would then be 'Is it possible to do any sort of runtime (rather than ant buildtime) property substitution in web.xml?

        Comment


        • #5
          You could try to patch Log4jWebConfigurer.java to support system properties for the location of the log4j.properties file.

          It currently supports the following two notations
          file:c:/somedir/log4j.properties
          classpath:org/mycompany/log4j.properties

          So maybe a new env could be added?
          env:syspropLog4j


          java -DsyspropLog4j=c:/whereadminwantthefile/log4j.properties

          Comment


          • #6
            Se also SPR-624 where Juergen added the file: and classpath: support

            Comment


            • #7
              So maybe a new env could be added?
              env:syspropLog4j
              Thanks Claus, thats exactly the feature I'm looking for.

              I've only been using spring for a week so I'm a bit scared to go messing around with source code.

              Can you recommend an existing spring class I can look at that might help me work out how do do the system property functionality in Log4jWebConfigurer?

              Comment


              • #8
                I guess it requires two classes for you to code:

                1) a new servlet listener just like org.springframework.web.util.Log4jConfigListener

                That just uses your class instead of the Spring one.

                2) Your own Log4jWebConfigurer that supports system properties for the location

                And then change the web.xml to use your servlet listener.

                Comment


                • #9
                  Also remember there is another log4j configuer in org.springframework.util.Log4jConfigurer that operates from standalone environments.

                  Ideal both of these should support system properties defined using the env: syntax.

                  Maybe we should file a JIRA request linking to this thread?

                  Comment


                  • #10
                    See SPR-1290 where I have created a patch.
                    http://opensource2.atlassian.com/pro...rowse/SPR-1290

                    Comment


                    • #11
                      Very nice. Thanks for that.

                      Comment


                      • #12
                        Ashario. It is included in the comming v1.25 of Spring using ${ ... } syntax.
                        Check the SPR-1290.

                        Comment


                        • #13
                          Hmm. I'm not sure the solution they ended up implementing actually works

                          If I specify in my web.xml
                          Code:
                              <context-param>
                                  <param-name>log4jConfigLocation</param-name>
                                  <param-value>$&#123;log4j.configfile&#125;</param-value>
                               </context-param>
                          and i specify a system property
                          Code:
                          -Dlog4j.configfile=file&#58;//c/dev/myapp/log4j.properties
                          Then what happens is Spring prepends the root directory of my webapp and seems to mess things up. I end up getting an exception


                          Code:
                          log4j&#58; Reading configuration from URL file&#58;C&#58;/bea_81/weblogic81/domains/mydomain/applications/myapp/myapp-web/file&#58;/c/dev/myapp/log4j.properties
                          log4j&#58;ERROR Could not read configuration file from URL &#91;file&#58;C&#58;/bea_81/weblogic81/domains/maypp/applications/myapp/myapp-web/file&#58;/c/dev/myapp/log4j.properties&#93;.
                          java.io.FileNotFoundException&#58; C&#58;\bea_81\weblogic81\domains\mydomain\applications\myapp\myapp-web\file&#58;\c\dev\myapp\dev\config\log4j.properties &#40;The filename, directory name, or volume label syntax is incorrect&#41;
                                  at java.io.FileInputStream.open&#40;Native Method&#41;
                                  at java.io.FileInputStream.<init>&#40;FileInputStream.java&#58;103&#41;
                                  at java.io.FileInputStream.<init>&#40;FileInputStream.java&#58;66&#41;
                                  at sun.net.http://www.protocol.file.FileURLConn...va&#58;69&#41;
                                  at sun.net.http://www.protocol.file.FileURLConn...a&#58;156&#41;
                                  at java.net.URL.openStream&#40;URL.java&#58;960&#41;
                                  at org.apache.log4j.PropertyConfigurator.doConfigure&#40;PropertyConfigurator.java&#58;424&#41;
                                  at org.apache.log4j.PropertyConfigurator.configure&#40;PropertyConfigurator.java&#58;327&#41;
                          I wasnt expecting Spring to prepend the real path of the web app to the value of the system property specified. The reason for my original post was to find a way to configure this so log4j.properties could exist outside of the webapp - so I'm still a bit stuck.

                          Comment


                          • #14
                            Hi

                            Have you tried to use an absolute path without the file prefix in the sys property

                            Code:
                            -Dlog4j.configfile=//c/dev/myapp/log4j.properties
                            I don't know if that works?

                            Also have you tried a different container? Tomcat to see if it can find the file using the two styles? Maybe it work different on the various containers.

                            If not I think you should post a new JIRA and point to this forum/topic so the spring team can fix it and make it work using WebLogic.

                            Comment


                            • #15
                              there is another way to do this as well, using org.springframework.util.Log4jConfigurer.
                              set the 'environment' property upon startup.

                              Code:
                               <bean
                                    id	= "log4jConfigurer"
                                    class	= "org.springframework.beans.factory.config.MethodInvokingFactoryBean" >
                              		<property name="targetClass"><value>org.springframework.util.Log4jConfigurer</value></property>
                              		<property name="targetMethod"><value>initLogging</value></property>
                              		<property name="arguments">
                              			<list>
                              				<value>classpath:META-INF/env/${environment}/log4j.xml</value>
                              			</list>
                              		</property>
                                  </bean>

                              Comment

                              Working...
                              X