Announcement Announcement Module
No announcement yet.
Log4j - configurable log4jConfigLocation in web.xml Page Title Module
Move Remove Collapse
Conversation Detail Module
  • 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
    The 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

    and have the production guys add a system property like

    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.



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


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


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

          It currently supports the following two notations

          So maybe a new env could be added?

          java -DsyspropLog4j=c:/whereadminwantthefile/


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


            • #7
              So maybe a new env could be added?
              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?


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


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


                  • #10
                    See SPR-1290 where I have created a patch.


                    • #11
                      Very nice. Thanks for that.


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


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

                          If I specify in my web.xml
                          and i specify a system property
                          Then what happens is Spring prepends the root directory of my webapp and seems to mess things up. I end up getting an exception

                          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&#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/;.
                ; C&#58;\bea_81\weblogic81\domains\mydomain\applications\myapp\myapp-web\file&#58;\c\dev\myapp\dev\config\ &#40;The filename, directory name, or volume label syntax is incorrect&#41;
                                  at;Native Method&#41;
                                  at org.apache.log4j.PropertyConfigurator.doConfigure&#40;;424&#41;
                                  at org.apache.log4j.PropertyConfigurator.configure&#40;;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 could exist outside of the webapp - so I'm still a bit stuck.


                          • #14

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

                            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.


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

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