Hi Jacob, I appreciate your response. Sorry for the delay to reply back since I was out of the town for last 4 days. I am sorry, your solution does not fit my requirement as it is. But it gave me a clue to do it the way I want.
I did it as follows: 1. Set a system environment variable MY_APP=C:\myapp 2. Added a ServletContextListener in my application, and in contextInitialized method I get the value of this environment variable in a variable myAppHome and set this varibale in jvm properties so that I can access it whenever required (I need it for different purposes) as you specified System.setProperty("MY_APP", myAppHome); 3. In the log4j properties file, now I have specified the file path using this property <param name="File" value="${MY_APP}/logs/myLog.log"/> Thanks a lot. I did this because, for some reason I don't want to complicate the application configuration and avoid it from becoming app server dependant e.g. today I am working with Websphere and I don't know how to do Context configuration in that. You had given example for Tomcat. Event though I find out this file in Webshphere, tomorrow if we migrate to WebLogic then the deployer has to look for some other file. And also my installtion document also has to reflect all these issues. Anyway thanks a lot again. But I am afraid, this is not working. The log file is not created. Am I doing something wrong. Attaching log4j.xml file and source files below. Please reply back if you find any flow in my solution. ************************************************************************ ************ ServletContextListener ************************************************************************ ************ public class AppInitializer implements ServletContextListener { public void contextInitialized(ServletContextEvent arg0) { System.out.println( "AppInitializer.contextInitialized started" ); System.out.println( "AppInitializer.contextInitialized: Get the system environment APP_HOME" ); // Get the system environment APP_HOME String homeDir = EnvironmentReader.getEnv( "APP_HOME" ); System.out.println( "AppInitializer.contextInitialized: APP_HOME=" + homeDir ); // Check the trailing seperator, if does not exists append it. if ( !homeDir.endsWith( File.separator ) ) homeDir = homeDir + File.separator; // Set the homeDir in jvm properties, so that it can be accessed later // on directly as System.getProperty System.setProperty( "APP_HOME", homeDir ); System.out.println( "AppInitializer.contextInitialized finished" ); } public void contextDestroyed(ServletContextEvent arg0) {} } ************************************************************************ ************ A test servlet, that I call for testing ************************************************************************ ************ public class TestServlet extends HttpServlet { public void service(HttpServletRequest req,HttpServletResponse res) { TestClass t = new TestClass(); t.hello( "Evenrybody" ); } } ************************************************************************ ************ A test class having some debug log statements ************************************************************************ ************ package myexamples public class TestClass { private static Logger logger = Logger.getLogger( TestClass.class ); public TestClass() { logger.debug( "TestClass constructor called" ); } public String hello( String name ) { logger.debug( "hello started" ); String ret = "Hellow " + name; logger.debug( "hello finished" ); return ret; } } ************************************************************************ ************ Log4j.xml ************************************************************************ ************ <appender name="TEST_LOGFILE" class="org.apache.log4j.DailyRollingFileAppender"> <errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler" /> <param name="File" value="${APP_HOME}/temp/test.log" /> <param name="Append" value="true" /> <param name="Threshold" value="debug" /> <param name="DatePattern" value="'.'yyyy-MM-dd" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p [%c{1}-L] %m%n" /> </layout> </appender> <logger name="myexamples" additivity="false"> <level value="DEBUG" /> <appender-ref ref="TEST_LOGFILE" /> </logger> <root> <appender-ref ref="TEST_LOGFILE" /> </root> ************************************************************************ ************ Log statements found in server log ************************************************************************ ************ [4/25/05 17:47:12:153 GMT+05:30] 5fa397ba ApplicationMg A WSVR0200I: Starting application: DefaultEAR [4/25/05 17:47:12:231 GMT+05:30] 5fa397ba WebContainer A SRVE0169I: Loading Web Module: HelloWorldWebClient. [4/25/05 17:47:12:231 GMT+05:30] 5fa397ba WebGroup I SRVE0180I: [HelloWorldWebClient] [/HelloWorldWebClient] [Servlet.LOG]: JSP 1.2 Processor: init [4/25/05 17:47:12:247 GMT+05:30] 5fa397ba WebGroup I SRVE0180I: [HelloWorldWebClient] [/HelloWorldWebClient] [Servlet.LOG]: SimpleFileServlet: init [4/25/05 17:47:12:247 GMT+05:30] 5fa397ba WebGroup I SRVE0180I: [HelloWorldWebClient] [/HelloWorldWebClient] [Servlet.LOG]: InvokerServlet: init [4/25/05 17:47:12:263 GMT+05:30] 5fa397ba SystemOut O AppInitializer.contextInitialized started [4/25/05 17:47:12:263 GMT+05:30] 5fa397ba SystemOut O AppInitializer.contextInitialized: Get the system environment GATEWAY_HOME [4/25/05 17:47:12:294 GMT+05:30] 5fa397ba SystemOut O AppInitializer.contextInitialized: GATEWAY_HOME=C:\eHubBridge [4/25/05 17:47:12:294 GMT+05:30] 5fa397ba SystemOut O AppInitializer.contextInitialized finished [4/25/05 17:47:12:294 GMT+05:30] 5fa397ba ApplicationMg A WSVR0221I: Application started: DefaultEAR As seen from the server log, the solution is working fine and the propery is set before staring the application. So why it is not creating the log file? If I call the servlet it processes the request without any error. But I cant see any log statements written in TestClass anywhere. Please help. Jitendra -----Original Message----- From: Jacob Kjome [mailto:[EMAIL PROTECTED] Sent: Wednesday, April 20, 2005 7:38 PM To: Log4J Users List Subject: RE: How to specify log file path using env variable Quoting Jitendra Kharche <[EMAIL PROTECTED]>: > Hi Jake, > > Thanks. The way you have suggested is another good way. But my problem > is little different. I want to use a system variable in the path of > log file. If I do using your method, it will still be a hardcoded path > because once I code it in the ServletContextListener, I can not change > it on the fly during the installation of my product. How so?... In web.xml... <context-param> <param-name>log-path</param-name> <param-value>/path/to/log/directory</param-value> <description> Log Path picked up by Log4jServletContextListener </description> </context-param> Now in your context listener, you just do... System.setProperty("log.path", context.getInitParameter("log-path")); In log4j.xml... <param name="File" value="${log.path}/myLog.log"/> You can override the web.xml <context-param> using server-specific deployment descriptors such as Tomcat's Context Configuration File... <Context ....> <Parameter name="log-path" value="/path/to/deployer-specified/log/directory" override="false" description="Override web.xml value so that deployer has control over context param, not the developer. Note that 'override=false' means 'don't allow web.xml to override the value I'm setting here, not the other way around'"/> </Context> > I don't want to > keep my log files in the webserver deployment directory. The log files > are kept somewhere else e.g. C:\myapp\logs. For this, as I have > explained in my first mail, I set a system variable MY_APP=C:\myapp > and this path depends on the installation directory which e.g. on unix > can be \home\usr\myapp. I can extend your suggestion to set this path > in a configuration file or in servelt init parameters, read from there > and set it. But this makes redundant information for the variable > MY_APP. It is defined in system variables as well as in my configurations. > I want to use "the system environment variable" to specify the path of > my log file. > Any more suggestion are welcome. > I think what I suggested above should work fine. Notice that even if the log path changes, the deployer has full control over the location of the log file by being able to override web.xml <context-param>'s so there is no need to make changes to the .war file even if the log file path changes. Just have the deployer make the decision at deployment time using the server's proprietary configuration. That's what all the indirection is about in J2EE's configuration scheme; to make things flexible for cases like this. Jake > Regards, > Jitendra > > > -----Original Message----- > From: Jacob Kjome [mailto:[EMAIL PROTECTED] > Sent: Tuesday, April 19, 2005 9:09 PM > To: Log4J Users List > Subject: RE: How to specify log file path using env variable > > Quoting Jitendra Kharche <[EMAIL PROTECTED]>: > > > Hi Paul, > > > > Thanks for the help. > > This is the best way fot standalone java applications. Whereas I am > > having a web application and I don't want to change the startup > > script > > > of webserver. > > > > Is there any other way? > > > > Sure. If you are using a webapp, just create a ServletContextListener > and as the first thing in the contextInitialized() method, set the > system property. > Then perform manual Log4j configuration. Note that you might want to > put a dummy log4j.xml file in WEB-INF/classes to avoid > autoconfiguration finding a Log4j config file elsewhere in the > classpath (assuming that you don't put your log4j.xml file for manual > configuration in the classpath, which probably makes sense since the > system property refrences wouldn't be resolved before autoconfiguration). > > Jake > > > Regards, > > Jitendra > > > > -----Original Message----- > > From: Paul Smith [mailto:[EMAIL PROTECTED] > > Sent: Tuesday, April 19, 2005 12:49 PM > > To: Log4J Users List > > Subject: Re: How to specify log file path using env variable > > > > if you start your application and set the property via the standard > > java system property, it works fine. > > > > eg. > > > > java -classpath <blah> -DMY_APP=c:\myapp .... > > > > Then you can embed the MY_APP in your log4j configuration file. > > > > cheers, > > > > Paul > > > > Jitendra Kharche wrote: > > > > >Hi All, > > > > > >I am using log4j for looging my applocation log on windows. The log > > >file path I have specified in the log4j.xml file is currently a > > >windows > > > > >path e.g. C:\myapp\logs\myapp.log. > > >I want to remove the hardcoded path (C:\myapp) and replace it with > > >a system environment variable MY_APP (=C:\myapp). How can I do that? > > >Furthermore, this will help me in migration my application from > > >windows > > > > >to unix or other OSs. > > > > > >Please help. > > > > > >Regards, > > >Jitendra Kharche > > > > > > > > > > > > > > > > -------------------------------------------------------------------- > > - To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > > -------------------------------------------------------------------- > > - To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]